您好,我在OAuth2中刷新令牌时遇到问题。
为了获取新令牌,我正在使用以下请求:
{{host}} / oauth / token?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 [org.hibernate.jpa.internal.EntityManagerImpl@54164e7a] 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.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@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.web.authentication.preauth.PreAuthenticatedAuthenticationToken@7a6d6be4: Principal: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@95dbc1ee: Principal: org.springframework.security.core.userdetails.User@1c27dc33: 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 [org.hibernate.jpa.internal.EntityManagerImpl@54164e7a]
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 [org.hibernate.jpa.internal.EntityManagerImpl@54164e7a] 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 [org.hibernate.jpa.internal.EntityManagerImpl@386e5658] 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.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@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 [org.hibernate.jpa.internal.EntityManagerImpl@386e5658]
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 [org.hibernate.jpa.internal.EntityManagerImpl@386e5658] 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()
... Spring从数据库中想要什么?这是我的配置类:
@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'
最佳答案
我认为这与任何JPA或InMemory无关。
对于UserDetailService的第一个错误,根据您发布的日志,仅表示您没有注入适当的UserDetailsService实现。 UserDetailsService只是接口。我不知道您还对配置做了什么,但是错误肯定表明Spring找不到适当的UserDetailsService注入。在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);
在端点配置的末尾,看看是否有所不同。