Java Code Examples for org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore

The following examples show how to use org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: oauth-boot   Source File: OAuth2AuthorizationServerConfig.java    License: MIT License 6 votes vote down vote up
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

    endpoints
            // token 存储方式
            .tokenStore(tokenStore)
            .authenticationManager(authenticationManager)
            // 不配置会导致token无法刷新
            .userDetailsService(userDetailsService)
            .allowedTokenEndpointRequestMethods(HttpMethod.POST,HttpMethod.GET);

    // 判断当前是否使用jwt
    if(!(tokenStore instanceof RedisTokenStore) && this.converter!=null){
        endpoints.accessTokenConverter(converter);
    }


    // 处理 ExceptionTranslationFilter 抛出的异常
    endpoints.exceptionTranslator(bootWebResponseExceptionTranslator);

    endpoints.pathMapping("/oauth/confirm_access","/custom/confirm_access");
}
 
Example 2
Source Project: spring-boot-demo   Source File: AuthorizationServerConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 认证服务端点配置
 */
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints
            //用户管理
            .userDetailsService(userDetailsService)
            //token存到redis
            .tokenStore(new RedisTokenStore(redisConnectionFactory))
            //启用oauth2管理
            .authenticationManager(authenticationManager)
            //接收GET和POST
            .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
 
Example 3
@Bean
public TokenStore tokenStore() {
    if (properties.getEnableJwt()) {
        return new JwtTokenStore(jwtAccessTokenConverter());
    } else {
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        // 解决每次生成的 token都一样的问题
        redisTokenStore.setAuthenticationKeyGenerator(oAuth2Authentication -> UUID.randomUUID().toString());
        return redisTokenStore;
    }
}
 
Example 4
Source Project: cloud-service   Source File: AuthorizationServerConfig.java    License: MIT License 5 votes vote down vote up
/**
 * 令牌存储
 */
@Bean
public TokenStore tokenStore() {
    if (storeWithJwt) {
        return new JwtTokenStore(accessTokenConverter());
    }
    RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
    // 解决同一username每次登陆access_token都相同的问题
    redisTokenStore.setAuthenticationKeyGenerator(new RandomAuthenticationKeyGenerator());

    return redisTokenStore;
}
 
Example 5
Source Project: open-cloud   Source File: OpenHelper.java    License: MIT License 5 votes vote down vote up
/**
 * 构建资源服务器RedisToken服务类
 *
 * @return
 */
public static ResourceServerTokenServices buildRedisTokenServices(RedisConnectionFactory redisConnectionFactory) throws Exception {
    OpenRedisTokenService tokenServices = new OpenRedisTokenService();
    // 这里的签名key 保持和认证中心一致
    RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
    tokenServices.setTokenStore(redisTokenStore);
    log.info("buildRedisTokenServices[{}]", tokenServices);
    return tokenServices;
}
 
Example 6
/**
     * token store
     */
    @Bean
    public TokenStore tokenStore() {
//        return new InMemoryTokenStore();
        return new RedisTokenStore(redisConnectionFactory);
//        return new JwtTokenStore(jwtAccessTokenConverter());
//        return new JdbcTokenStore(dataSource);
    }
 
Example 7
/**
     * token store
     */
    @Bean
    public TokenStore tokenStore() {
//        return new InMemoryTokenStore();
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        redisTokenStore.setPrefix(GlobalsConstants.PROJECT_PREFIX+GlobalsConstants.OAUTH_PREFIX);
        return redisTokenStore;
//        return new JwtTokenStore(jwtAccessTokenConverter());
//        return new JdbcTokenStore(dataSource);
    }
 
Example 8
/**
 * token store
 */
@Bean
public TokenStore tokenStore() {
    RedisTokenStore redisTokenStore = new RedisTokenStore(connectionFactory);
    redisTokenStore.setPrefix(GlobalsConstants.PROJECT_PREFIX+ GlobalsConstants.OAUTH_PREFIX);
    return redisTokenStore;
           // return new JwtTokenStore(accessTokenConverter());
}
 
Example 9
/**
     * token store
     */
    @Bean
    public TokenStore tokenStore() {
//        return new InMemoryTokenStore();
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        redisTokenStore.setPrefix(GlobalsConstants.PROJECT_PREFIX+GlobalsConstants.OAUTH_PREFIX);
        return redisTokenStore;
//        return new JwtTokenStore(jwtAccessTokenConverter());
//        return new JdbcTokenStore(dataSource);
    }
 
Example 10
/**
 * token store
 */
@Bean
public TokenStore tokenStore() {
    RedisTokenStore redisTokenStore = new RedisTokenStore(connectionFactory);
    redisTokenStore.setPrefix(GlobalsConstants.PROJECT_PREFIX+GlobalsConstants.OAUTH_PREFIX);
    return redisTokenStore;
           // return new JwtTokenStore(accessTokenConverter());
}
 
Example 11
Source Project: cola-cloud   Source File: AuthorizationServerConfiguration.java    License: MIT License 5 votes vote down vote up
@Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
                .tokenStore(new RedisTokenStore(redisConnectionFactory))
//                .accessTokenConverter(jwtAccessTokenConverter())
                .authenticationManager(authenticationManager)
                .exceptionTranslator(webResponseExceptionTranslator)
                .reuseRefreshTokens(false)
                .userDetailsService(integrationUserDetailsService);
    }
 
Example 12
Source Project: fw-cloud-framework   Source File: FwAuthorizationConfiguration.java    License: MIT License 5 votes vote down vote up
/**
 * tokenstore 定制化处理 1. 如果使用的 redis-cluster 模式请使用 FwRedisTokenStore FwRedisTokenStore tokenStore = new
 * FwRedisTokenStore();
 * tokenStore.setRedisTemplate(redisTemplate);
 */
@Bean
public TokenStore redisTokenStore() {
	RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
	tokenStore.setPrefix(SecurityConstant.PREFIX);
	return tokenStore;
}
 
Example 13
Source Project: lion   Source File: AuthorizationServerConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public TokenStore tokenStore() {
    /**
     * redis 存储有状态方式
     */
    return new RedisTokenStore(redisConnectionFactory);
    /**
     * jwt 无状态方式
     */
    //return new JwtTokenStore(jwtAccessTokenConverter());
}
 
Example 14
Source Project: lion   Source File: ResourceServerConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources
            /**
             * redis 存储有状态方式
             */
            .tokenStore(new RedisTokenStore(redisConnectionFactory))
            /**
             * jwt 无状态方式
             */
            //.tokenStore(new JwtTokenStore(accessTokenConverter()));
            //.authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
            .authenticationEntryPoint(new CustomAuthenticationEntryPoint())
            .accessDeniedHandler(new CustomAccessDeniedHandler());
}
 
Example 15
@Bean
    public TokenStore tokenStore() {
        // 基于 JDBC 实现,令牌保存到数据库
//        return new JdbcTokenStore(dataSource());
        return new RedisTokenStore(redisConnectionFactory);
    }
 
Example 16
Source Project: spring-cloud-study   Source File: RedisTokenStoreConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public TokenStore redisTokenStore (){
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 17
Source Project: spring-cloud-study   Source File: RedisTokenStoreConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public TokenStore redisTokenStore (){
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 18
@Bean
public TokenStore tokenStore(){
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 19
@Bean
public TokenStore tokenStore(){
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 20
public TokenStore redisTokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 21
/**
 * Redis操作
 * @return
 */
@Bean
public RedisTokenStore tokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 22
Source Project: open-cloud   Source File: ResourceServerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public RedisTokenStore redisTokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 23
Source Project: open-cloud   Source File: ResourceServerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public RedisTokenStore redisTokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 24
Source Project: open-cloud   Source File: ResourceServerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
    // 自定义oauth2 认证, 使用redis读取token,而非jwt方式
    JsonAuthenticationEntryPoint entryPoint = new JsonAuthenticationEntryPoint(accessLogService);
    JsonAccessDeniedHandler accessDeniedHandler = new JsonAccessDeniedHandler(accessLogService);
    AccessManager accessManager = new AccessManager(apiresourceLocator, apiProperties);
    AuthenticationWebFilter oauth2 = new AuthenticationWebFilter(new RedisAuthenticationManager(new RedisTokenStore(redisConnectionFactory)));
    oauth2.setServerAuthenticationConverter(new ServerBearerTokenAuthenticationConverter());
    oauth2.setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(entryPoint));
    oauth2.setAuthenticationSuccessHandler(new ServerAuthenticationSuccessHandler() {
        @Override
        public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
            ServerWebExchange exchange = webFilterExchange.getExchange();
            SecurityContextServerWebExchange securityContextServerWebExchange = new SecurityContextServerWebExchange(exchange, ReactiveSecurityContextHolder.getContext().subscriberContext(
                    ReactiveSecurityContextHolder.withAuthentication(authentication)
            ));
            return webFilterExchange.getChain().filter(securityContextServerWebExchange);
        }
    });
    http
            .httpBasic().disable()
            .csrf().disable()
            .authorizeExchange()
            .pathMatchers("/").permitAll()
            // 动态权限验证
            .anyExchange().access(accessManager)
            .and().exceptionHandling()
            .accessDeniedHandler(accessDeniedHandler)
            .authenticationEntryPoint(entryPoint).and()
            // 日志前置过滤器
            .addFilterAt(new PreRequestFilter(), SecurityWebFiltersOrder.FIRST)
            // 跨域过滤器
            .addFilterAt(corsFilter(), SecurityWebFiltersOrder.CORS)
            // 签名验证过滤器
            .addFilterAt(new PreSignatureFilter(baseAppServiceClient,apiProperties, new JsonSignatureDeniedHandler(accessLogService)), SecurityWebFiltersOrder.CSRF)
            // 访问验证前置过滤器
            .addFilterAt(new PreCheckFilter(accessManager, accessDeniedHandler), SecurityWebFiltersOrder.CSRF)
            // oauth2认证过滤器
            .addFilterAt(oauth2, SecurityWebFiltersOrder.AUTHENTICATION)
            // 日志过滤器
            .addFilterAt(new AccessLogFilter(accessLogService), SecurityWebFiltersOrder.SECURITY_CONTEXT_SERVER_WEB_EXCHANGE);
    return http.build();
}
 
Example 25
Source Project: fw-spring-cloud   Source File: TokenStoreConfig.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 用于存放token
 * @return
 */
@Bean
@ConditionalOnProperty(prefix = "fw.security.oauth2", name = "storeType", havingValue = "redis")
public TokenStore redisTokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 26
Source Project: cola   Source File: AuthorizationServerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public TokenStore tokenStore() {
	return new RedisTokenStore(redisConnectionFactory);
}
 
Example 27
/**
 * token store
 */
@Bean
public TokenStore tokenStore() {
    return new RedisTokenStore(connectionFactory);
}
 
Example 28
@Bean
public TokenStore tokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 29
Source Project: spring-cloud-shop   Source File: OAuth2SecurityConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public TokenStore tokenStore() {
    return new RedisTokenStore(redisConnectionFactory);
}
 
Example 30
Source Project: OAuth-2.0-Cookbook   Source File: OAuth2ResourceServer.java    License: MIT License 4 votes vote down vote up
@Bean
public TokenStore tokenStore() {
    return new RedisTokenStore(connectionFactory);
}