32.Redis存储Token
之前的代码我们讲token直接存储在内存中,这在生产环境中是不合理的,下面我们将其改造成存储在Redis中
1.添加依赖及配置
pom.xml
<!--redis 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--commons-pool2 对象池依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.properties
# Redis配置
spring.redis.host=127.0.0.1
2.编写Redis配置类
RedisConfig.java
/**
* @author etern
* @title: RedisConfig
* @projectName springsecurityoauth2-demo
* @description: TODO Redis 配置
* @date 2022/2/19 15:06
*/
//配置类注解
@Configuration
public class RedisConfig {
//注入token连接工厂
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
//token存储位置
public TokenStore redisTokenStore() {
//返回值放入token的连接工厂
return new RedisTokenStore(redisConnectionFactory);
}
}
3.将redis配置放入AuthorizationServerConfig授权服务器配置中
@Autowired
//将Redis配置中的位置注入进来
@Qualifier("redisTokenStore")
private TokenStore tokenStore;
/**
* @return
* @throws
* @Author etern
* @Description //TODO 使用密码模式所需配置
* @Date 14:29 2022/2/19
* @Param * @param endpoints
**/
//重写configure中参数是endpoints
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//密码授权模式是把密码直接传给授权服务器的
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userService)
//将存放的token传进来
.tokenStore(tokenStore);
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Eternal Night
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果