java - Spring Security OAuth2 不使用属性中的 token 过期值

标签 java spring spring-security oauth-2.0 spring-security-oauth2

我正在尝试将我的应用程序配置为从属性文件中提取访问权限和刷新 token 过期时间,而不是在 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。

对于 ClientDetailsS​​ervice 的 Autowiring ,它有一个异常(exception):

Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate com.sun.proxy.$Proxy135.toString()

所以clientDetailsS​​ervice的值为null。然后它将使用默认值,因此您在配置类中的值设置不起作用。但如果您在 application.yml 中执行此操作,它将设置此值而不检查 clientDetailsS​​ervice,因此它可以工作。

我已经向团队报告了这个问题,希望有人能够解决这个错误。 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;
}

关于java - Spring Security OAuth2 不使用属性中的 token 过期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47292961/

相关文章:

java - Java 中的序列号是什么?

java - 从 Java 中查找所有更改给定目录的提交

java - EditText 上的“完成”按钮的监听器?

json - 如果类的对象是 Rest Controller ,如何确保它由 Spring boot 自动编码

java - 传递给 CompletableFuture SupplyAsync Executors.newFixedThreadPool(10) 或 new ForkJoinPool(10) 哪个执行器更好

spring - 如何在请求上传期间处理客户端中止?

java - @Scheduler 是否启动一个新线程?

java - 如何检查方法级别的 spring 安全性

spring - 如何使用 SSL 配置 Spring RestTemplate(在 Spring @MVC 中)

java - Spring Security 动态登录