我正在尝试配置我的应用程序以从我的属性文件中提取访问权限和刷新令牌过期时间,而不是在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
答案 0 :(得分:2)
我希望这个回复不要太晚...
我遇到了同样的问题,后来我发现这是一个错误。
对于ClientDetailsService的自动连线,有一个例外:
Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate com.sun.proxy.$Proxy135.toString()
因此clientDetailsService的值为null。然后它将使用默认值,因此您在config类中设置的值不起作用。但是,如果您在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;
}
答案 1 :(得分:1)
也在寻找这个答案,并尝试了DeezCashews提出的解决方案。但这对我不起作用,因为有一部分代码首先检查此值是否在access_token_validity表oauth_client_details列中设置,然后才从tokenServices中获取值。因此,如果在oauth_client_details表中设置了“ expires_in”,则需要在其中进行更改。
检查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;
}