数据库
首页 > 数据库> > OAuth2.0基于redis存储Token

OAuth2.0基于redis存储Token

作者:互联网

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

修改application.yaml

spring:
  redis:
    host: 127.0.0.1
    database: 0

编写redis配置类

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    @Bean
    public TokenStore tokenStore(){
        return new RedisTokenStore(redisConnectionFactory);
    }
}

在授权服务器配置中指定令牌的存储策略为Redis

@Autowired
private TokenStore tokenStore;
 
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManagerBean) //使用密码模式需要配置
        .tokenStore(tokenStore)  //指定token存储到redis
        .reuseRefreshTokens(false)  //refresh_token是否重复使用
        .userDetailsService(userService) //刷新令牌授权包含对用户信息的检查
        .allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST); //支持GET,POST请求
}

标签:存储,redisConnectionFactory,redis,private,tokenStore,Token,OAuth2.0,public
来源: https://blog.csdn.net/qq_41489540/article/details/122813519