Spring Security OAuth2 Resource Server Always Returning Invalid Token

后端 未结 3 1032
温柔的废话
温柔的废话 2020-12-08 01:42

I am trying to get a basic in-memory OAuth2 server running using the Spring Libraries. I have been following the sparklr example.

I currently have configured the Ser

3条回答
  •  [愿得一人]
    2020-12-08 02:03

    The problem ended up being that the resource server and the authorization server were not getting the same token store reference. Not sure how the wiring was not working correctly, but using a fixed object in the configuration class worked like a charm. Ultimately, I'll move to a persistence backed token store, which probably would not have had any issues.

    Thanks goes to @OhadR for the answer and the help!

    Ultimately, I simplified the configuration, went thru the same workflow, and it worked out

    @Configuration
    public class Oauth2ServerConfiguration {
    
        private static final String SERVER_RESOURCE_ID = "oauth2-server";
    
        private static InMemoryTokenStore tokenStore = new InMemoryTokenStore();
    
    
        @Configuration
        @EnableResourceServer
        protected static class ResourceServer extends ResourceServerConfigurerAdapter {
    
            @Override
            public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
                resources.tokenStore(tokenStore).resourceId(SERVER_RESOURCE_ID);
            }
    
            @Override
            public void configure(HttpSecurity http) throws Exception {
                http.requestMatchers().antMatchers("/me").and().authorizeRequests().antMatchers("/me").access("#oauth2.hasScope('read')");
            }
        }
    
        @Configuration
        @EnableAuthorizationServer
        protected static class AuthConfig extends AuthorizationServerConfigurerAdapter {
    
            @Autowired
            private AuthenticationManager authenticationManager;
    
    
            @Override
            public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
                endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore).approvalStoreDisabled();
            }
    
            @Override
            public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.inMemory()
                    .withClient("client")
                        .authorizedGrantTypes("authorization_code","refresh_token")
                        .authorities("ROLE_CLIENT")
                        .scopes("read")
                        .resourceIds(SERVER_RESOURCE_ID)
                        .secret("secret")
                ;
            }
        }
    }
    

    Anyone that stumbles upon this post, I recommend looking more at the unit tests for example rather than the full blown sparklr/tonr example, as it has a lot of extra configuration that are not necessarily needed to get started.

提交回复
热议问题