Java Code Examples for org.springframework.security.oauth2.provider.token.DefaultTokenServices#setRefreshTokenValiditySeconds()

The following examples show how to use org.springframework.security.oauth2.provider.token.DefaultTokenServices#setRefreshTokenValiditySeconds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SophiaAuthorizationServerConfig.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
/**
 * 注意,自定义TokenServices的时候,需要设置@Primary,否则报错
 */
@Primary
@Bean
public DefaultTokenServices defaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(tokenStore());
    tokenServices.setSupportRefreshToken(true);
    // 这里如果设置为false则不能更新refresh_token,如果需要刷新token的功能需要设置成true
    tokenServices.setSupportRefreshToken(true);
    // 设置上次RefreshToken是否还可以使用 默认为true
    tokenServices.setReuseRefreshToken(false);
    // token有效期自定义设置,默认12小时
    tokenServices.setAccessTokenValiditySeconds(60 * 60 * 6);
    // refresh_token默认30天
    tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 8);
    tokenServices.setTokenEnhancer(tokenEnhancer());
    return tokenServices;
}
 
Example 2
Source File: AuthorizationServerConfig.java    From lion with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

    // 配置tokenServices参数
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(tokenStore());
    /**
     * jwt 无状态方式
     */
    //tokenServices.setTokenEnhancer(jwtAccessTokenConverter());
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setClientDetailsService(clientDetails());
    // 设置access_token有效时长12小时,默认12小时
    tokenServices.setAccessTokenValiditySeconds(60 * 60 * 12);
    // 设置refresh_token有效时长7天,默认30天
    tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);

    endpoints
            .userDetailsService(userDetailsService)
            .authenticationManager(authenticationManager)
            .tokenServices(tokenServices)
            // 自定义认证异常处理类
            .exceptionTranslator(webResponseExceptionTranslator());
}
 
Example 3
Source File: AuthorizationServerConfig.java    From java-tutorial with MIT License 5 votes vote down vote up
/**
 * 注意,自定义TokenServices的时候,需要设置@Primary,否则报错
 *
 * @return
 */
@Primary
@Bean
public DefaultTokenServices defaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(jdbcTokenStore);
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setClientDetailsService(clientDetails());
    // token有效期自定义设置24小时,默认12小时
    tokenServices.setAccessTokenValiditySeconds(60 * 60 * 24);
    //默认30天,这里修改
    tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);

    return tokenServices;
}
 
Example 4
Source File: AuthorizationConfig.java    From springcloud-oauth2 with MIT License votes vote down vote up
@Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 采用token转jwt,并添加一些自定义信息(token增强)(有默认非必须)
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(
                Arrays.asList(jwtAccessTokenConverter(),tokenEnhancer()));
        endpoints.tokenEnhancer(tokenEnhancerChain)
                // 配置token存储,一般配置redis存储
                .tokenStore(tokenStore())
                // 配置认证管理器
                .authenticationManager(authenticationManager)
                // 配置用户详情server,密码模式必须
                .userDetailsService(userDetailsService)
                // 配置授权码模式授权码服务,不配置默认为内存模式
                .authorizationCodeServices(authorizationCodeServices())
                // 配置grant_type模式,如果不配置则默认使用密码模式、简化模式、验证码模式以及刷新token模式,如果配置了只使用配置中,默认配置失效
                // 具体可以查询AuthorizationServerEndpointsConfigurer中的getDefaultTokenGranters方法
                .tokenGranter(tokenGranter(endpoints));
        // 配置TokenServices参数
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setTokenStore(endpoints.getTokenStore());
        // 是否支持刷新Token
        tokenServices.setSupportRefreshToken(true);
        tokenServices.setReuseRefreshToken(true);
        tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
        tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
        // 设置accessToken和refreshToken的默认超时时间(如果clientDetails的为null就取默认的,如果clientDetails的不为null取clientDetails中的)
        tokenServices.setAccessTokenValiditySeconds((int) TimeUnit.HOURS.toSeconds(2));
        tokenServices.setRefreshTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(30));
        endpoints.tokenServices(tokenServices);

    }