可以在该网站上获得解析结果:https://jwt.io/
该模块只是对oauth2-server模块的扩展,直接复制过来扩展下下即可。
在上一节中我们都是把令牌存储在内存中的,这样如果部署多个服务,就会导致无法使用令牌的问题。 Spring Cloud Security中有两种存储令牌的方式可用于解决该问题,一种是使用Redis来存储,另一种是使用JWT来存储。
使用Redis存储令牌
在pom.xml中添加Redis相关依赖:
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-oauth2
org.springframework.cloud
spring-cloud-starter-security
在application.yml中添加redis相关配置:
server:
port: 9401
spring:
application:
name: oauth2-jwt-server
redis:
redis相关配置
host: 10.172.0.201
database: 0
添加在Redis中存储令牌的配置:
@Configuration
public class RedisTokenStoreConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore redisTokenStore() {
return new RedisTokenStore(redisConnectionFactory);
}
}
在授权服务器配置中指定令牌的存储策略为Redis:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserService userService;
@Autowired
@Qualifier(“redisTokenStore”)
private TokenStore tokenStore;
/**
- 使用密码模式需要配置
*/
@Override
public void configure(Aut