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

The following examples show how to use org.springframework.security.oauth2.provider.token.DefaultTokenServices#setTokenEnhancer() . 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: AuthorizationServerConfiguration.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
     * 用来配置授权(authorization)以及令牌(token)的访问端点和令牌服务(token services)
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 配置tokenStore
//    endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore())
//            .accessTokenConverter(accessTokenConverter()).userDetailsService(userDetailsService);
        //指定认证管理器
        endpoints.authenticationManager(authenticationManager);
        //指定token存储位置
        endpoints.tokenStore(tokenStore());

        endpoints.accessTokenConverter(accessTokenConverter());
        endpoints.userDetailsService(userDetailsService);
        //自定义token生成方式
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(customerEnhancer(), accessTokenConverter()));
        endpoints.tokenEnhancer(tokenEnhancerChain);

        // 配置TokenServices参数
        DefaultTokenServices tokenServices = (DefaultTokenServices) endpoints.getDefaultAuthorizationServerTokenServices();
        tokenServices.setTokenStore(endpoints.getTokenStore());
        tokenServices.setSupportRefreshToken(true);
        tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
        tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
        tokenServices.setAccessTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(1));//一天
        endpoints.tokenServices(tokenServices);
    }
 
Example 3
Source File: FebsAuthorizationServerConfigure.java    From FEBS-Cloud with Apache License 2.0 5 votes vote down vote up
@Bean
public ResourceOwnerPasswordTokenGranter resourceOwnerPasswordTokenGranter(AuthenticationManager authenticationManager, OAuth2RequestFactory oAuth2RequestFactory) {
    DefaultTokenServices defaultTokenServices = defaultTokenServices();
    if (properties.getEnableJwt()) {
        defaultTokenServices.setTokenEnhancer(jwtAccessTokenConverter());
    }
    return new ResourceOwnerPasswordTokenGranter(authenticationManager, defaultTokenServices, redisClientDetailsService, oAuth2RequestFactory);
}
 
Example 4
Source File: AuthorizationServerConfiguration.java    From open-cloud with MIT License 5 votes vote down vote up
private DefaultTokenServices createDefaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(tokenStore());
    tokenServices.setTokenEnhancer(tokenEnhancer());
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setReuseRefreshToken(true);
    tokenServices.setClientDetailsService(customClientDetailsService);
    return tokenServices;
}
 
Example 5
Source File: AuthorizationServerConfiguration.java    From open-cloud with MIT License 5 votes vote down vote up
private DefaultTokenServices createDefaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(tokenStore());
    tokenServices.setTokenEnhancer(tokenEnhancer());
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setReuseRefreshToken(true);
    tokenServices.setClientDetailsService(customClientDetailsService);
    return tokenServices;
}
 
Example 6
Source File: CustomAuthorizationConfig.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
@Bean("resourceServerTokenServices")
@Primary
public DefaultTokenServices tokenServices() {
	DefaultTokenServices defaultTokenServices = new
			DefaultTokenServices();
	defaultTokenServices.setTokenStore(tokenStore());
	defaultTokenServices.setSupportRefreshToken(false);
	defaultTokenServices.setAccessTokenValiditySeconds(120);
	defaultTokenServices.setTokenEnhancer(accessTokenConverter());
	return defaultTokenServices;
}
 
Example 7
Source File: CustomAuthorizationConfig.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
@Bean("resourceServerTokenServices")
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(false);
    defaultTokenServices.setAccessTokenValiditySeconds(120);
    defaultTokenServices.setTokenEnhancer(accessTokenConverter());
    return defaultTokenServices;
}
 
Example 8
Source File: AuthorizationServerConfig.java    From Oauth2-Stateless-Authentication-with-Spring-and-JWT-Token with MIT License 5 votes vote down vote up
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    defaultTokenServices.setTokenEnhancer(accessTokenConverter());
    return defaultTokenServices;
}
 
Example 9
Source File: OAuth2Config.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public DefaultTokenServices tokenServices() {
		DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
	defaultTokenServices.setTokenStore(mongoTokenStore);
	defaultTokenServices.setClientDetailsService(clientDetailsService);
	defaultTokenServices.setSupportRefreshToken(true);
       defaultTokenServices.setTokenEnhancer(new CustomTokenEnhancer());

	return defaultTokenServices;
}
 
Example 10
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);

    }