2016-07-26 98 views
0

我使用以下命令,創建一個Spring-REST-蒙戈項目春季啓動MongoDB的連接 - 憑證與URI

蒙戈3.2.7春節4.3.1釋放彈簧引導1.3.6

我有問題用下面的權限創建下面的用戶。

> use admin 
switched to db admin 
> db.getUser("UserAdminNew") 
{ 
     "_id" : "admin.UserAdminNew", 
     "user" : "UserAdminNew", 
     "db" : "admin", 
     "roles" : [ 
       { 
         "role" : "readWrite", 
         "db" : "admin" 
       }, 
       { 
         "role" : "root", 
         "db" : "admin" 
       }, 
       { 
         "role" : "readWrite", 
         "db" : "NEWTest" 
       } 
     ] 
} 

Application.properties

#spring.data.mongodb.uri=mongodb://UserAdminNew:[email protected]:27017/NEWTest 

按照春季啓動文件beyong蒙戈3.x中,我們可以連接通的URI通過提供相應的憑證來的MongoDB。

Application.java

package main.java.springmongodbdatarest; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

Product.java

package main.java.springmongodbdatarest; 

import org.springframework.data.annotation.Id; 

public class Product { 

    @Id 
    private String id; 

    private String name; 
    private String title; 
    private String description; 
    private String imageUrl; 
    private double price; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 
    public String getImageUrl() { 
     return imageUrl; 
    } 
    public void setImageUrl(String imageUrl) { 
     this.imageUrl = imageUrl; 
    } 
    public double getPrice() { 
     return price; 
    } 
    public void setPrice(double price) { 
     this.price = price; 
    } 



} 

ProductRepository.java

package main.java.springmongodbdatarest; 

import org.springframework.data.mongodb.repository.MongoRepository; 
import org.springframework.data.rest.core.annotation.RepositoryRestResource; 

@RepositoryRestResource(collectionResourceRel = "products", path = "products") 
public interface ProductRepository extends MongoRepository<Product, String> { 

} 

有一次,我跑我的Maven項目&打網址在我的地方,我得到以下響應

http://localhost:8080/

{ 
    "_links" : { 
    "products" : { 
     "href" : "http://localhost:8080/products{?page,size,sort}", 
     "templated" : true 
    }, 
    "profile" : { 
     "href" : "http://localhost:8080/alps" 
    } 
    } 

}

http://localhost:8080/products

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Tue Jul 26 11:12:02 IST 2016 

There was an unexpected error (type=Internal Server Error, status=500). 

{ "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on test to execute command { count: \"product\", query: {} }" , "code" : 13} 

,你可以即使數據庫我哈哈已經提到的是在application.properties中的NEWTest錯誤消息明確指定無法插入默認數據庫「測試」

我可能會忽略的東西,因爲它沒有采取指定的db n憑據。

請建議。謝謝提前。

錯誤堆棧

2016-07-26 12:16:48.300 ERROR 8468 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Reque 
rocessing failed; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on test to execute command { count: \"produc 
query: {} }" , "code" : 13}] with root cause 

com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on test to execute command { count: \"product\", query: {} }" , "code" : 13} 
     at com.mongodb.CommandResult.getException(CommandResult.java:76) 
     at com.mongodb.CommandResult.throwOnError(CommandResult.java:131) 
     at com.mongodb.DBCollection.getCount(DBCollection.java:1198) 
     at com.mongodb.DBCollection.getCount(DBCollection.java:1153) 
     at com.mongodb.DBCollection.getCount(DBCollection.java:1137) 
     at com.mongodb.DBCollection.getCount(DBCollection.java:1106) 
     at com.mongodb.DBCollection.count(DBCollection.java:1027) 
     at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.count(SimpleMongoRepository.java:146) 
     at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:216) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:498) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:436) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:421) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:393) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:506) 
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
     at com.sun.proxy.$Proxy76.findAll(Unknown Source) 
     at org.springframework.data.rest.core.invoke.PagingAndSortingRepositoryInvoker.invokeFindAll(PagingAndSortingRepositoryInvoker.java:66) 
     at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:172) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:498) 
     at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 
     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) 
     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) 
     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) 
     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 
     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668) 
     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521) 
     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
     at java.lang.Thread.run(Thread.java:745) 

添加連接時使用了--auth一切MONGO工作正常&可以執行取後刪除。

> use NEWTest 
switched to db NEWTest 
> show collections 
Friends 
product 
users 
usersD 
> db.product.find().pretty(); 
{ 
     "_id" : ObjectId("5797350f0206e6b7ef7a118a"), 
     "_class" : "main.java.springmongodbdatarest.Product", 
     "name" : "NEW GLASS", 
     "title" : "Beatiful Blue Glass ", 
     "imageUrl" : "http://img.com", 
     "price" : 5 
} 
> 

回答

0

你應該在你的應用程序中使用spring.data.mongodb.usernamespring.data.mongodb.password

完全application.properties例如:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

+0

我曾嘗試已經與沒有在下面的鏈接,這顯然是工作到蒙戈2.x的地方,因爲我haveMongo 3工作out.Noticed。x 您可以設置spring.data.mongodb.uri屬性來更改URL並配置其他設置(例如副本集):但同時也採用默認測試分區spring.data.mongodb.uri = mongodb:// user:[email protected]:12345,mongo2.example.com:23456/test http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql .html – Noob101

+0

你可以使用其他類似nodejs項目來檢查mongo數據庫連接嗎? (例如:https://scotch.io/tutorials/an-introduction-to-mongodb#using-in-a-nodejs-application)或mongodb GUI管理工具,如https://robomongo.org/ –

+0

我可以連接&執行後得到刪除,如果我連接到monogo db與出證書。只有當我的mongodb運行在--auth&我試圖通過屬性文件提供證書,我面臨這個問題。 因此,我認爲我必須是我的一個配置問題,如何提供用戶名和密碼。 – Noob101