简体   繁体   English

Spring Security OAuth2:InsufficientAuthenticationException

[英]Spring Security OAuth2: InsufficientAuthenticationException

First of all, I disabled basic auth:首先,我禁用了基本身份验证:

security.basic.enabled=false

Then I access the authorization page:然后我访问授权页面:

http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com

I got following exception:我得到以下异常:

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:138) ~[spring-security-oauth2-2.0.10.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at ...

I don't understand why I have to do authentication first before OAuth?我不明白为什么我必须在 OAuth 之前先进行身份验证?

The flow for the authorization code grant goes like this:授权码授权流程如下:

  1. Client redirects the user to auth server's authorization page.客户端将用户重定向到身份验证服务器的授权页面。 Hence the http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com因此http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com
  2. If the user were already logged in, the user would be immediately shown an authorization page where he can approve the authorization request.如果用户已经登录,用户将立即看到一个授权页面,他可以在其中批准授权请求。 If the user is not yet logged in, he should be redirected to a login page first to authenticate himself to let Spring Security know who is giving the authorization.如果用户尚未登录,则应首先将其重定向到登录页面以对自己进行身份验证,让 Spring Security 知道是谁授予了授权。

What you probably need to do is secure the authorization endpoint by requiring a role granted like this in xml:您可能需要做的是通过要求在 xml 中授予这样的角色来保护授权端点:

<security:http disable-url-rewriting="true"
               use-expressions="true"
               entry-point-ref="loginEntryPoint">
    ...

    <security:intercept-url pattern="/oauth/authorize" access="hasRole('ROLE_USER')"/>
    ...
</security:http>

If the user is not yet logged in, this will trigger Spring Security to redirect the user to login as configured in your loginEntryPoint .如果用户尚未登录,这将触发 Spring Security 将用户重定向到loginEntryPoint配置的登录。 Typically, you would redirect the user to a login page.通常,您会将用户重定向到登录页面。 After successfully authenticating, the user will return to the authorization endpoint.认证成功后,用户将返回到授权端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM