0

我有一个 webServer,它有一个 web 界面,用户使用 Spring 安全性进行身份验证,但我需要使用相同的引擎对来自我的移动应用程序的登录请求的用户进行身份验证,并且我需要对他们进行身份验证,我一直在查看身份验证类,但我似乎无法将它们正确,这是我第一次这样做,我希望有人可以帮助我,因为我有点迷路

@Post
@Path("/mobile/session")
public void login(String username, String password) {
    Usuario usuario = usuarioService.loadByUserName(username);
    UsernamePasswordAuthenticationToken usernamePassAuthToken = new UsernamePasswordAuthenticationToken(usuario,EncriptarMD5.encriptar(password));
    Authentication auth = authenticationManager.authenticate(usernamePassAuthToken);
    if(auth.isAuthenticated()){
        result.use(Results.json()).from(RequestContextHolder.currentRequestAttributes().getSessionId().toString(), "token").serialize();
        
    } else {
        result.use(Results.http()).setStatusCode(401);
    }
}

我收到以下异常

br.com.caelum.vraptor.InterceptionException:引发异常,请检查根本原因以获取详细信息:org.springframework.security.authentication.BadCredentialsException:br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java: 96) 在 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) 在 br.com.caelum 的 br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) .vraptor.core.LazyInterceptorHandler.execute(LazyInterceptorHandler.java:61) 在 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) 在 br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept (ParametersInstantiatorInterceptor.java:93) 在 br.com.caelum.vraptor.core.LazyInterceptorHandler。在 br.com.otgmobile.trackgoweb.util.CustomInterceptor.intercept(CustomInterceptor.java:24) 处执行 (LazyInterceptorHandler.java:59)在 br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) 在 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) 在 br.com.caelum.vraptor .core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor .java:83) 在 br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) 在 br.com.caelum.vraptor.core。DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:71) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java: 54) 在 br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:48) 在 br.com.caelum 的 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) .vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) 在 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) 在 br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept (ResourceLookupInterceptor.java:69) 在 br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler。在 br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:54) 在 br.com.caelum.vraptor.core.EnhancedRequestExecution.execute(EnhancedRequestExecution.java:44) 执行(ToInstantiateInterceptorHandler.java:54)在 br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:91) 在 br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:58) 在 br.com.caelum。 vraptor.VRaptor.doFilter(VRaptor.java:88) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)在 org.sitemesh.webapp.contentfilter.ContentBufferingFilter.bufferAndPostProcess(ContentBufferingFilter.java:169) 在 org.sitemesh.webapp.contentfilter.ContentBufferingFilter.doFilter(ContentBufferingFilter.java:126) at org.sitemesh.config.ConfigurableSiteMeshFilter.doFilter(ConfigurableSiteMeshFilter.java:163) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain .doFilter(ApplicationFilterChain.java:210) 在 org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.springframework.security.web.FilterChainProxy.doFilterInternal( FilterChainProxy.java:186) 在 org.springframework.security.web。FilterChainProxy.doFilter(FilterChainProxy.java:160) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) at org .apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve .java:222) 位于 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 的 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)。 core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve。在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) 调用(ErrorReportValve.java:99) .catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java :589) 在 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent。 ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 原因:org.springframework.security.authentication.BadCredentialsException: Bad credentialsjava:99) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector .CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker。在 java.lang.Thread.run(Thread.java:722) 处运行(ThreadPoolExecutor.java:603) 原因:org.springframework.security.authentication.BadCredentialsException:凭据错误java:99) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector .CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker。在 java.lang.Thread.run(Thread.java:722) 处运行(ThreadPoolExecutor.java:603) 原因:org.springframework.security.authentication.BadCredentialsException:凭据错误Valves.AccessLogValve.invoke(AccessLogValve.java:931) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net.JIoEndpoint $SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 在 java. lang.Thread.run(Thread.java:722) 原因:org.springframework.security.authentication.BadCredentialsException: Bad credentialsValves.AccessLogValve.invoke(AccessLogValve.java:931) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net.JIoEndpoint $SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 在 java. lang.Thread.run(Thread.java:722) 原因:org.springframework.security.authentication.BadCredentialsException: Bad credentialsapache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor. java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util .concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 原因: org.springframework.security.authentication.BadCredentialsException:错误的凭据apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor. java:1004) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util .concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) at java.lang.Thread.run(Thread.java:722) 原因: org.springframework.security.authentication.BadCredentialsException:错误的凭据org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 上 org.apache.coyote.AbstractProtocol 的服务(CoyoteAdapter.java:407) .tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:603) at java.lang.Thread.run(Thread.java:722) 原因:org.springframework.security.authentication.BadCredentialsException: Bad credentialsorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 上 org.apache.coyote.AbstractProtocol 的服务(CoyoteAdapter.java:407) .tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:603) at java.lang.Thread.run(Thread.java:722) 原因:org.springframework.security.authentication.BadCredentialsException: Bad credentialsutil.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 603) 在 java.lang.Thread.run(Thread.java:722) 引起:org.springframework.security.authentication.BadCredentialsException:错误的凭据util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 603) 在 java.lang.Thread.run(Thread.java:722) 引起:org.springframework.security.authentication.BadCredentialsException:错误的凭据

4

1 回答 1

1

我认为您缺少的一件是在您进行身份验证后将身份验证传递给 SecurityContextHolder 以将其附加到线程。

SecurityContextHolder.getContext().setAuthentication(authentication);

此外,在进行身份验证时,不要将加密值传递给 Auth 令牌。

UsernamePasswordAuthenticationToken usernamePassAuthToken = new UsernamePasswordAuthenticationToken(usuario,password);

身份验证管理器将负责为您加密它。

于 2013-01-24T19:40:34.403 回答