Spring 安全 OAuth2 未使用属性中的令牌过期值
Spring Security OAuth2 not using token expire values from properties
我正在尝试将我的应用程序配置为从我的属性文件中提取访问和刷新令牌过期时间,而不是在 java 配置中设置它们。但是,它没有拾取它们,而是恢复为默认值。
这是我的 Java 配置示例,我在其中手动设置了过期值。当我这样做时,效果很好。
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
....
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("myclient")
.secret("mysecret")
.authorizedGrantTypes("password", "refresh_token")
.scopes("my-app")
.autoApprove("my-app")
.accessTokenValiditySeconds(30)
.refreshTokenValiditySeconds(3200);
}
}
然而,当我尝试在我的 application.properties
文件中像这样设置它们时,它不起作用。
# Security
security.oauth2.client.access-token-validity-seconds=60
security.oauth2.client.refresh-token-validity-seconds=3200
希望这个回复还不算太晚...
我遇到了同样的问题,后来发现这是一个bug。
对于 ClientDetailsService 的自动装配,它有一个例外:
Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate com.sun.proxy.$Proxy135.toString()
所以clientDetailsService的值为空。然后它将使用默认值,因此您在配置 class 中的值设置不起作用。但是如果你在 application.yml 中这样做,它会在不检查 clientDetailsService 的情况下设置这个值,所以它有效。
我已经向团队报告了这个问题,希望有人能解决这个错误。
https://github.com/spring-projects/spring-security-oauth/issues/1448
一个可能的解决方案是在 application.yml 文件中设置值,或者像这样在 DefaultTokenServices 中设置值:
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(this.tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setTokenEnhancer(this.accessTokenConverter());
defaultTokenServices.setAccessTokenValiditySeconds(100);
return defaultTokenServices;
}
也在搜索这个答案并尝试了 DeezCashews 提出的解决方案。但它对我不起作用,因为有一部分代码首先检查此值是否设置在列 access_token_validity table oauth_client_details 中,然后才从 tokenServices 中获取值。因此,如果您的“expires_in”设置在oauth_client_details table,那么您需要在那里进行更改。
在 db 中检查有效性 属性 的代码:
protected int getAccessTokenValiditySeconds(OAuth2Request clientAuth) {
if (clientDetailsService != null) {
ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
Integer validity = client.getAccessTokenValiditySeconds();
if (validity != null) {
return validity;
}
}
return accessTokenValiditySeconds;
}
我正在尝试将我的应用程序配置为从我的属性文件中提取访问和刷新令牌过期时间,而不是在 java 配置中设置它们。但是,它没有拾取它们,而是恢复为默认值。
这是我的 Java 配置示例,我在其中手动设置了过期值。当我这样做时,效果很好。
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
....
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("myclient")
.secret("mysecret")
.authorizedGrantTypes("password", "refresh_token")
.scopes("my-app")
.autoApprove("my-app")
.accessTokenValiditySeconds(30)
.refreshTokenValiditySeconds(3200);
}
}
然而,当我尝试在我的 application.properties
文件中像这样设置它们时,它不起作用。
# Security
security.oauth2.client.access-token-validity-seconds=60
security.oauth2.client.refresh-token-validity-seconds=3200
希望这个回复还不算太晚...
我遇到了同样的问题,后来发现这是一个bug。
对于 ClientDetailsService 的自动装配,它有一个例外:
Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate com.sun.proxy.$Proxy135.toString()
所以clientDetailsService的值为空。然后它将使用默认值,因此您在配置 class 中的值设置不起作用。但是如果你在 application.yml 中这样做,它会在不检查 clientDetailsService 的情况下设置这个值,所以它有效。
我已经向团队报告了这个问题,希望有人能解决这个错误。 https://github.com/spring-projects/spring-security-oauth/issues/1448
一个可能的解决方案是在 application.yml 文件中设置值,或者像这样在 DefaultTokenServices 中设置值:
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(this.tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
defaultTokenServices.setTokenEnhancer(this.accessTokenConverter());
defaultTokenServices.setAccessTokenValiditySeconds(100);
return defaultTokenServices;
}
也在搜索这个答案并尝试了 DeezCashews 提出的解决方案。但它对我不起作用,因为有一部分代码首先检查此值是否设置在列 access_token_validity table oauth_client_details 中,然后才从 tokenServices 中获取值。因此,如果您的“expires_in”设置在oauth_client_details table,那么您需要在那里进行更改。
在 db 中检查有效性 属性 的代码:
protected int getAccessTokenValiditySeconds(OAuth2Request clientAuth) {
if (clientDetailsService != null) {
ClientDetails client = clientDetailsService.loadClientByClientId(clientAuth.getClientId());
Integer validity = client.getAccessTokenValiditySeconds();
if (validity != null) {
return validity;
}
}
return accessTokenValiditySeconds;
}