2016-10-24 81 views
0

您好,我在OAuth2中刷新令牌时遇到问题。oauth2令牌存储冲突(JPA vs inMemory)

为了获得新鲜的令牌中,我使用以下请求: {{主机}} /的OAuth /令牌grant_type = refresh_token & refresh_token = [refresh_token_obtained_from_authorization]

其中我得到的响应为:

{ 
    "error": "server_error", 
    "error_description": "UserDetailsService is required." 
} 

在日志中我可以看到:

2016-10-24 23:01:41.166 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Creating new transaction with name [org.springframework.security.oauth2.provider.token.DefaultTokenServices.refreshAccessToken]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',+org.springframework.security.oauth2.common.exceptions.InvalidTokenException,+org.springframework.security.oauth2.common.exceptions.InvalidGrantException 
2016-10-24 23:01:41.166 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Opened new EntityManager [[email protected]] for JPA transaction 
2016-10-24 23:01:41.166 DEBUG 48384 --- [nio-8080-exec-7] o.h.e.t.internal.TransactionImpl   : begin 
2016-10-24 23:01:41.166 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Exposing JPA transaction as JDBC transaction [org.springframewo[email protected]5ad1aa88] 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.s.s.authentication.ProviderManager  : Authentication attempt using org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] p.PreAuthenticatedAuthenticationProvider : PreAuthenticated authentication request: org.springframework.security[email protected]7a6d6be4: Principal: org.springframew[email protected]95dbc1ee: Principal: [email protected]: Username: forkflit; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: FORK_FLIT; Credentials: [PROTECTED]; Authenticated: true; Details: {grant_type=password, scope=trust, username=forkflit}; Granted Authorities: FORK_FLIT; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: FORK_FLIT 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Initiating transaction rollback 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Rolling back JPA transaction on EntityManager [[email protected]] 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.h.e.t.internal.TransactionImpl   : rolling back 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager  : Closing JPA EntityManager [[email protected]] after transaction 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.IllegalStateException: UserDetailsService is required. 
2016-10-24 23:01:41.167 DEBUG 48384 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.exceptions.OAuth2Exception> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.handleException(java.lang.Exception) throws java.lang.Exception 
2016-10-24 23:01:41.167 INFO 48384 --- [nio-8080-exec-7] o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: IllegalStateException, UserDetailsService is required. 

当我尝试把刷新令牌体内:{"refresh_token":"[refresh_token]"}回应我:

{ 
    "error": "server_error", 
    "error_description": null 
} 

,并在日志中:

2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Creating new transaction with name [org.springframework.security.oauth2.provider.token.DefaultTokenServices.refreshAccessToken]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',+org.springframework.security.oauth2.common.exceptions.InvalidTokenException,+org.springframework.security.oauth2.common.exceptions.InvalidGrantException 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Opened new EntityManager [[email protected]] for JPA transaction 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.h.e.t.internal.TransactionImpl   : begin 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Exposing JPA transaction as JDBC transaction [org.springframewo[email protected]36a9af25] 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Initiating transaction rollback 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Rolling back JPA transaction on EntityManager [[email protected]] 
2016-10-24 23:05:19.449 DEBUG 48384 --- [nio-8080-exec-8] o.h.e.t.internal.TransactionImpl   : rolling back 
2016-10-24 23:05:19.450 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.JpaTransactionManager  : Closing JPA EntityManager [[email protected]] after transaction 
2016-10-24 23:05:19.450 DEBUG 48384 --- [nio-8080-exec-8] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager 
2016-10-24 23:05:19.450 DEBUG 48384 --- [nio-8080-exec-8] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.NullPointerException 
2016-10-24 23:05:19.450 DEBUG 48384 --- [nio-8080-exec-8] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.exceptions.OAuth2Exception> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.handleException(java.lang.Exception) throws java.lang.Exception 
2016-10-24 23:05:19.450 INFO 48384 --- [nio-8080-exec-8] o.s.s.o.provider.endpoint.TokenEndpoint : Handling error: NullPointerException, null 

有趣的是,春天是做什么用JPA,而在我的配置我使用inMemory() ......什么都想春从DB?

这是我的配置类:

@Configuration 
@EnableAuthorizationServer 
@EnableResourceServer 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter { 

    private UsersService service = new UsersService(); 

    @Autowired 
    AuthenticationManagerBuilder authenticationManager; 


    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) { 
     endpoints.authenticationManager(new AuthenticationManager() { 
      @Override 
      public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
       logger.info("Authentication!"); 
       return authenticationManager.getOrBuild().authenticate(authentication); 
      } 
     }); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory().withClient("myApp") 
       .authorizedGrantTypes("password", "refresh_token") 
       .scopes("write", "read", "trust") 
       .secret("123456").accessTokenValiditySeconds(200); 
    } 

    @Bean 
    public UserDetailsService userDetailsService() { 
     return (username) -> { 
      return service.getByName(username).map(account -> new User(account.getUsername(), account.getPassword(), account.getAuthorities())).orElseThrow(
        () -> new RuntimeException("User not found") 
      ); 
     }; 
    } 
} 

在日志中我看到这个Bean创建:

2016-10-24 22:53:49.648 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Creating shared instance of singleton bean 'userDetailsService' 
2016-10-24 22:53:49.648 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'userDetailsService' 
2016-10-24 22:53:49.648 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2016-10-24 22:53:49.648 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'OAuth2Configuration' 
2016-10-24 22:53:49.653 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Eagerly caching bean 'userDetailsService' to allow for resolving potential circular references 
2016-10-24 22:53:49.654 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2016-10-24 22:53:49.654 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
2016-10-24 22:53:49.655 DEBUG 48384 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'userDetailsService' 

回答

1

我不认为它与任何JPA或InMemory东西。

对于UserDetailService的第一个错误,根据您发布的日志,仅仅意味着您没有注入适当的UserDetailsS​​ervice实现。 UserDetailsS​​ervice只是界面。我不知道你还做了什么配置,但错误肯定说Spring找不到合适的UserDetailsS​​ervice注入。在Oauth2Configuration中,我看到你实例化的UsersService,我认为它是你的UserDetailService接口的自定义实现?

对于第二个错误,我在日志中看到org.springframework.web.HttpRequestMethodNotSupportedException,这意味着,我猜你发布了GET请求,但服务器期望POST,反之亦然。检查你正在使用的HTTP方法,以及期望得到的服务器代码。

尝试改变

endpoints.authenticationManager(new AuthenticationManager() { 
     @Override 
     public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
      logger.info("Authentication!"); 
      return authenticationManager.getOrBuild().authenticate(authentication); 
     } 
    }); 

endpoints.authenticationManager(new AuthenticationManager() { 
     @Override 
     public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
      logger.info("Authentication!"); 
      return authenticationManager.getOrBuild().authenticate(authentication); 
      } 
     }) 
    .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST); 

在端点配置结束,看看是否让差异。

+0

我没有一件事......如果我没有UserDetailsS​​ervice,我不应该能够使用用户凭证以及使用refresh_token检索access_token(使用凭据正常工作) –

+0

然后它不应该是由于缺少UserDetailsS​​ervice,只是缺少配置,服务器端不采用客户端请求的HTTP方法? – wonhee