我在连接到mongo database时遇到身份验证失败。我使用GridFsTemplate将一些图像存储到Mongo这里是代码
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("reqId") String reqId, HttpServletRequest request){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile multipartFile = null;
String fileName = null;
Iterator<String> iterator = multipartRequest.getFileNames();
try {
while (iterator.hasNext()) {
String key = (String) iterator.next();
multipartFile = (CommonsMultipartFile) multipartRequest
.getFile(key);
DBObject metaData = new BasicDBObject();
int num = (int) ((Math.random() * 9000000) + 1000000);
metaData.put("reqId", reqId);
InputStream inputStream = null;
if (!multipartFile.isEmpty()) {
File convFile = new File(multipartFile.getOriginalFilename());
multipartFile.transferTo(convFile);
fileName = multipartFile.getOriginalFilename()+""+num;
inputStream = new FileInputStream(convFile);
gridFsTemplate.store(inputStream, fileName,multipartFile.getContentType(), metaData);
}
}
return "You successfully uploaded " + fileName + " into "
+ fileName + "-uploaded !";
}
catch (Exception e) {
return "You failed to upload " + fileName + " => "
+ e.getMessage();
}
弹簧配置
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
<property name="port" value="27017" />
</bean>
<bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="testuser" />
<constructor-arg name="password" value="test" />
</bean>
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="testdb" />
<constructor-arg name="credentials" ref="mongoCredentials"
<constructor-arg name="authenticationDatabaseName" value="admin" />
</bean>
<bean name="gridFsTemplate"
class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg ref="converter" />
</bean>
我在我的lib和构建路径中使用bellow jar
在我的pom.xml中我只有 spring-data-mongodb-1.8.0.RELEASE.jar的任何条目
我得到了以下异常
org.springframework.data.mongodb.CannotGetMongoDbConnectionException:无法对数据库[testdb]进行身份验证,username = [testuser],password = [t ** t] 在org.springframework.data.mongodb.core.MongoDbUtils.doGetDB(MongoDbUtils.java:124) 在org.springframework.data.mongodb.core.MongoDbUtils.getDB(MongoDbUtils.java:81) 在org.springframework.data.mongodb.core.SimpleMongoDbFactory.getDb(SimpleMongoDbFactory.java:145) 在org.springframework.data.mongodb.core.SimpleMongoDbFactory.getDb(SimpleMongoDbFactory.java:134) 在org.springframework.data.mongodb.gridfs.GridFsTemplate.getGridFs(GridFsTemplate.java:264) 在org.springframework.data.mongodb.gridfs.GridFsTemplate.store(GridFsTemplate.java:160) 在com.cisco.asestore.restful.controller.ToolDocsMongoController.handleFileUpload(ToolDocsMongoController.java:158) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330) 在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:201) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 在org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) 在org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) 在org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1517) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1474) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)
我尝试了所有可能的方法有很多关于如何为MongoTemplate配置身份验证的帖子而不是GridFsTemplate请帮我解决问题