Java Code Examples for org.springframework.security.oauth2.provider.token.DefaultTokenServices

The following examples show how to use org.springframework.security.oauth2.provider.token.DefaultTokenServices. These examples are extracted from open source projects. 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
/**
 * 注意,自定义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 Project: lion   Source File: AuthorizationServerConfig.java    License: 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 Project: gemini   Source File: OAuth2AuthorizationServerConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    return defaultTokenServices;
}
 
Example 4
Source Project: spring-boot-demo   Source File: OAuth2ResourceServerConfig.java    License: MIT License 5 votes vote down vote up
@Override
  public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    resources.tokenServices(defaultTokenServices);
//    resources.tokenStore(tokenStore());
  }
 
Example 5
/**
     * 用来配置授权(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 6
@Bean
@Primary
public DefaultTokenServices defaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();

    tokenServices.setTokenStore(tokenStore());
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setClientDetailsService(redisClientDetailsService);
    return tokenServices;
}
 
Example 7
@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 8
Source Project: open-cloud   Source File: AuthorizationServerConfiguration.java    License: 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 9
Source Project: open-cloud   Source File: AuthorizationServerConfiguration.java    License: 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 10
Source Project: syhthems-platform   Source File: AuthorizationServerConfig.java    License: MIT License 5 votes vote down vote up
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(jwtTokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 11
Source Project: Spring-5.0-Projects   Source File: CustomAuthorizationConfig.java    License: 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 12
Source Project: Spring-5.0-Projects   Source File: CustomAuthorizationConfig.java    License: 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 13
Source Project: oauth2lab   Source File: OAuth2AuthorizationServerConfig.java    License: MIT License 5 votes vote down vote up
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 14
Source Project: java-tutorial   Source File: AuthorizationServerConfig.java    License: 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 15
Source Project: oauth2-blog   Source File: TokenStoreConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 16
/**
 * Generate token services.
 * 
 * <p>This method will use the Spring security’s default token services 
 * implementation which tokens will be generated as random UUID values.
 * 
 * @return  The {@code DefaultTokenServices} object.
 */
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 17
@Bean
@ConditionalOnMissingBean(ResourceServerTokenServices.class)
public DefaultTokenServices jwkTokenServices(TokenStore jwkTokenStore) {
	DefaultTokenServices services = new DefaultTokenServices();
	services.setTokenStore(jwkTokenStore);
	return services;
}
 
Example 18
@Bean
@ConditionalOnMissingBean(ResourceServerTokenServices.class)
public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
	DefaultTokenServices services = new DefaultTokenServices();
	services.setTokenStore(jwtTokenStore);
	return services;
}
 
Example 19
@Bean
@ConditionalOnMissingBean(ResourceServerTokenServices.class)
public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
	DefaultTokenServices services = new DefaultTokenServices();
	services.setTokenStore(jwtTokenStore);
	return services;
}
 
Example 20
@Bean
@ConditionalOnMissingBean(AuthorizationServerTokenServices.class)
public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) {
	DefaultTokenServices services = new DefaultTokenServices();
	services.setTokenStore(jwtTokenStore);
	return services;
}
 
Example 21
@Test
public void switchToJwt() {
	TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=FOOBAR").applyTo(this.environment);
	this.context = new SpringApplicationBuilder(ResourceConfiguration.class).environment(this.environment)
			.web(WebApplicationType.NONE).run();
	DefaultTokenServices services = this.context.getBean(DefaultTokenServices.class);
	assertThat(services).isNotNull();
	this.thrown.expect(NoSuchBeanDefinitionException.class);
	this.context.getBean(RemoteTokenServices.class);
}
 
Example 22
@Test
public void asymmetricJwt() {
	TestPropertyValues.of("security.oauth2.resource.jwt.keyValue=" + PUBLIC_KEY).applyTo(this.environment);
	this.context = new SpringApplicationBuilder(ResourceConfiguration.class).environment(this.environment)
			.web(WebApplicationType.NONE).run();
	DefaultTokenServices services = this.context.getBean(DefaultTokenServices.class);
	assertThat(services).isNotNull();
}
 
Example 23
@Test
public void jwkConfiguration() throws Exception {
	TestPropertyValues.of("security.oauth2.resource.jwk.key-set-uri=https://idp.example.com/token_keys")
			.applyTo(this.environment);
	this.context = new SpringApplicationBuilder(ResourceConfiguration.class).environment(this.environment)
			.web(WebApplicationType.NONE).run();
	DefaultTokenServices services = this.context.getBean(DefaultTokenServices.class);
	assertThat(services).isNotNull();
	this.thrown.expect(NoSuchBeanDefinitionException.class);
	this.context.getBean(RemoteTokenServices.class);
}
 
Example 24
@Test
public void testDisablingAuthorizationServer() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	this.context.register(ResourceServerConfiguration.class, MinimalSecureWebApplication.class);
	TestPropertyValues.of("security.oauth2.resource.jwt.keyValue:DEADBEEF").applyTo(this.context);
	ConfigurationPropertySources.attach(this.context.getEnvironment());
	this.context.refresh();
	assertThat(countBeans(RESOURCE_SERVER_CONFIG)).isEqualTo(1);
	assertThat(countBeans(AUTHORIZATION_SERVER_CONFIG)).isEqualTo(0);
	assertThat(countBeans(UserApprovalHandler.class)).isEqualTo(0);
	assertThat(countBeans(DefaultTokenServices.class)).isEqualTo(1);
}
 
Example 25
@Bean
@Primary
public DefaultTokenServices getTokenService() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(this.jwtTokenStore());
    return defaultTokenServices;
}
 
Example 26
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 27
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    MyTokenService tokenService = new MyTokenService(blackListService);
    tokenService.setTokenStore(tokenStore());
    tokenService.setSupportRefreshToken(true);
    tokenService.setTokenEnhancer(accessTokenConverter());
    return tokenService;
}
 
Example 28
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    defaultTokenServices.setTokenEnhancer(accessTokenConverter());
    return defaultTokenServices;
}
 
Example 29
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}
 
Example 30
@Bean
@Primary
public DefaultTokenServices tokenServices() {
    final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    return defaultTokenServices;
}