User must be authenticated with Spring Security before authorization can be completed.解决方法

Posted 秋9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了User must be authenticated with Spring Security before authorization can be completed.解决方法相关的知识,希望对你有一定的参考价值。

【现象】

springcloud集成OAuth时,访问/oauth/authorize时,报如下错误:

org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed.
	at org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(AuthorizationEndpoint.java:143)
	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:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)

【定位】

在这个方法中,如果principal为null时即生成User must be authenticated with Spring Security before authorization can be completed.异常。

principal在org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver类的resolveArgument方法中生成的。在resolveArgument方法中第162行中 Principal userPrincipal = request.getUserPrincipal();如果request中不存在用户信息,则Principal 无法取得,造成参数为Null情况。
 

抛出InsufficientAuthenticationException异常后由过滤器拦截,跳转到登录界面,报这个错误的原因为无法跳转到登录界面。 

【解决方法】

设置正确的登录界面,下面举例如下:

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
                .formLogin().loginPage("/login")
                .permitAll()
                .and().authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable().cors()
                .and().addFilterAt(ignoreLogoutFilter, LogoutFilter.class)
                .addFilterAt(customBasicAuthenticationFilter(), BasicAuthenticationFilter.class);
    

修改为:

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
                .formLogin().loginPage("/sso/login")
                .permitAll()
                .and().authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable().cors()
                .and().addFilterAt(ignoreLogoutFilter, LogoutFilter.class)
                .addFilterAt(customBasicAuthenticationFilter(), BasicAuthenticationFilter.class);
    

以上是关于User must be authenticated with Spring Security before authorization can be completed.解决方法的主要内容,如果未能解决你的问题,请参考以下文章

Foxmail 邮箱只能收邮件不能发邮件,提示:501 mail from address must be same as authorization user ?

参数或变量中有语法错误。 服务器响应为: mail from address must be same as authorization user

Android异常篇 Cause: buildOutput.apkData must not be null

ansible playbook error:UNREACHABLE argument must be an int, or have a fileno() method

Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’

Django Cannot assign "A1": "B1" must be a "C1" instance.