gpt4 book ai didi

java - oauth2 token 存储冲突(JPA 与 inMemory)

转载 作者:行者123 更新时间:2023-12-01 09:19:59 25 4
gpt4 key购买 nike

您好,我在 OAuth2 中刷新 token 时遇到问题。

为了获取新 token ,我使用以下请求:{{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.

当我尝试将刷新 token 放入正文中时:{"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

有趣的是,spring 正在使用 JPA 做一些事情,而在我的配置中我使用的是 inMemory()...Spring 想从 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'

最佳答案

我认为它与任何 JPA 或 InMemory 内容无关。

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

对于第二个错误,我在日志中看到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);

在端点配置的末尾,看看是否有区别。

关于java - oauth2 token 存储冲突(JPA 与 inMemory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40227574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com