Problems redirecting to access token entry point Oauth Token

依然范特西╮ 提交于 2020-01-25 03:19:04

问题


I am having problems with redirecting to access token entry point /oauth/token which will detail bellow. I am hoping someone could give me some light to it as I took a lot of time implementing this.

Also, interesting is the fact that I cannot test with with SoapUI 5.0 community edition even following their instructions. It gest the authorization code but fails later as you need to set the redirect URI as "urn:ietf:wg:oauth:2.0:oob".

Since Spring-Security-Oauth2 lacks a lot of good documentation and I had spent lots of time debugging and documenting the work I decided to share my comments and configuration code here which might also be helpfull to someone else.

I am using the following dependencies versions on my pom:

<org.springframework-version>4.0.5.RELEASE</org.springframework-version>
<org.springframework.security-version>3.2.5.RELEASE</org.springframework.security-version>
<org.springframework.security.oauth2-version>2.0.3.RELEASE</org.springframework.security.oauth2-version>

Now, the idea was to implement all the clientId objects, UserPrincipal, access, nonce and token stores using Cassandra as a persistency store. All those components are working perfectly and are tested. In fact, it fetches all the authentication/authorization, creates authorization codes.

I've seen a bug recently on testing JDBC stores on Spring Oauth2 github but that was related to testing not the actuall implementation, specially because not using JDBC.

I have wrote a client webapplication to access a REST resource which resides with the OAuth2 servers and Spring Security for logging in. All goes well until I go request an access token to /oauth/token.

When I hit the secure Rest service first it properly starting doing redirections and goes tru DefaultOAuth2RequestFactory createAuthorizationRequest() method. Loads the ClientDetails object perfectly with the secret etc from the store. So it has all the scopes, grants etc for the Oauth2 client. It also validates properly the redirectURIParameter and resolves the redirect. Then it goes to the TokenStoreUserApprovalHandler and create the OAuth2Request object. Then, of course, tries to find an existing access token which does not exist yet on the workflow. It creates the authenticationkey from authenticationKeyGenerator and queries the store which properly returns null at this point. Then it redirects back to /oauth/authorize twice when on the second time it has an authorization code and marks as approved (AuthorizationEndPoint) inside approveOrDeny() method. The authorizationCodeServices creates the code and stores it properly in my Cassandra store.

At this point it calls (AuthorizationEndPoint) getSuccessfulRedirect() where it adds the state parameter to the template.

Then it calls (OAuth2RestTemplate class) getAccessToken() method. Since the access token is fine and valid it then calls acquireAccessToken() which returns an accessTokenRequest with {code=[Q19Y6u], state=[1PyzHf]} . Then it calls the accessTokenProvider to obtain an access token at obtainAccessToken(). Then the calls OAuth2AccessTokenSupport is called at retrieveToken() method. And fails at getRestTemplate. The AuthorizationCodeResourceDetails is created perfectly with grant type authorization_code and it has both authorizationScheme and clientAuthenticationScheme as header. The clientId is correct as the clientSecret. The id of the AuthorizationCodeResourceDetails is oAuth2ClientBean and the userAuthorizationURI is http://myhost.com:8080/MyAPI/oauth/authorize. Headers show as

{Authorization=[Basic .....]}

The extractor is org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport. The form is {grant_type=[authorization_code], code=[Xc7yni], redirect_uri=[http://myhost.com:8080/OAuthClient/support]}

And then the application freezes and it shows on the logs:

DEBUG: org.springframework.security.authentication.DefaultAuthenticationEventPublisher - No event was found for the exception org.springframework.security.authentication.InternalAuthenticationServiceException
DEBUG: org.springframework.security.web.authentication.www.BasicAuthenticationFilter - Authentication request for failed: org.springframework.security.authentication.InternalAuthenticationServiceException

Then I have the following Exception on my client web application:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is error="access_denied", error_description="Error requesting access token."
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)

Now, I believe I have a few things wrong on my xml configuration for OAuth2 and Spring Security. So the configuration file follows.

I do have a few questions concerning it so here are the questions first:

1) <oauth2:authorization-server> I am not sure if I had this configured correctly. Please look at the comments I have on my xml file. I have added the authorization-request-manager-ref parameter which points to my userAuthenticationManager bean, a authentication manager which takes a authentication-provider user-service-ref="userService"

<oauth2:authorization-server client-details-service-ref="webServiceClientService" 
    token-services-ref="tokenServices" user-approval-page="/oauth/userapproval" 
    error-page="/oauth/error" authorization-endpoint-url="/oauth/authorize" 
    token-endpoint-url="/oauth/token" user-approval-handler-ref="userApprovalHandler" 
    redirect-resolver-ref="resolver">
    <oauth2:authorization-code
        authorization-code-services-ref="codes" />
    <oauth2:implicit/>
    <oauth2:refresh-token/>
    <oauth2:client-credentials/>
    <oauth2:password authentication-manager-ref="userAuthenticationManager"/>
    <!-- <oauth2:custom-grant token-granter-ref=""/> -->
</oauth2:authorization-server>

2) authentication-manager oauthClientAuthenticationManager is used when "/oauth/token" is intercepted. This is defined as follows:

<sec:authentication-manager id="oauthClientAuthenticationManager">
    <sec:authentication-provider user-service-ref="clientDetailsUserService">
        <sec:password-encoder ref="passwordEncoder" />
    </sec:authentication-provider>
</sec:authentication-manager>

3) I have the following methodSecurityExpressionHandler bean defined which is used at sec:global-method-security. Not sure if this is correct or not as well.

<beans:bean id="methodSecurityExpressionHandler"
        class="org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler" />

4) I also have a bean "clientCredentialsTokenEndpointFilter" which I believe is not recommended. I use this as a custom-filter for entry point "/oauth/token" but I believe this is wrong.

<beans:bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <beans:property name="authenticationManager" ref="oauthClientAuthenticationManager"/>
</beans:bean>
A filter and authentication endpoint for the OAuth2 Token Endpoint. Allows clients to authenticate using request
parameters if included as a security filter, as permitted by the specification (but not recommended). It is
recommended by the specification that you permit HTTP basic authentication for clients, and not use this filter at
all.

5) Now for the Oauth Token Endpoint: This is the endpoint /oauth/token as I have many questions here:

  1. This is never reached.
  2. Shall I have a custom-filter like clientCredentialsTokenEndpointFilter to it or not?
  3. Do I have to have a http-basic entry point?
  4. Shall I have an access attribute as in IS_AUTHENTICATED_FULLY or can I use an authority I have defined on my UserPrincipal object such as OAUTH_CLIENT I have added there?
  5. What about the session? Shall I say "stateless" or "never"
  6. Shall I add the corsFilter to it as well?
  7. Is the entry point correct? Which is the OAuth2AuthenticationEntryPoint class?
  8. Do I have to add the csrf token? I believe not as it will restrict it more.
  9. Is the expression-handler correct as a org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandle?
  10. The authentication-manager-ref I can change from oauthClientAuthenticationManager to userAuthenticationManager.

<sec:http use-expressions="true" create-session="stateless" authentication-manager-ref="userAuthenticationManager" entry-point-ref="oauthAuthenticationEntryPoint" pattern="/oauth/token"> <sec:intercept-url pattern="/oauth/token" access="hasAuthority('OAUTH_CLIENT')" /> <!-- <sec:intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" /> --> <sec:http-basic entry-point-ref="oauthAuthenticationEntryPoint"/> <!-- <sec:http-basic/> --> <sec:anonymous enabled="false" /> <sec:custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" /> <sec:access-denied-handler ref="oauthAccessDeniedHandler" /> <sec:expression-handler ref="webSecurityExpressionHandler" /> <!-- <sec:custom-filter ref="corsFilter" after="LAST"/> --> </sec:http>

I would like to add the full config file here but there is a limit.


回答1:


The /oauth/token endpoint should be secured with client credentials, and it looks like you wired it to a user authentication manager. You didn't show the configuration or implementation of that, but judging by the InternalAuthenticationServiceException it is failing with an exception that isn't classified as a security exception. Fix those two things and you might be in business.

(The @Configuration style is much more convenient by the way, and I would recommend getting started with that and more of the defaults it provides, until you get the hang of it.)



来源:https://stackoverflow.com/questions/26443582/problems-redirecting-to-access-token-entry-point-oauth-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!