需要用JwtTokenStore的token生成策略
1.JwtTokenStore不管token的具體生成,而是交給JwtAccessTokenConverter去做。
2.在CustomAuthorizationServerConfig中注入jwtAccessTokenConverter並重寫configure(AuthorizationServerEndpointsConfigurer endpoints)方法
3.至此生成的jwt令牌(無狀態)就替換掉了spring默認的token(有狀態,通過UUID產生),如果想添加自定義的額外的信息塞進jwt令牌可通過(TokenEnhancer)
1)創建TokenEnhancerBean
2)在CustomAuthorizationServerConfig中注入TokenEnhancer
3)重寫configure(AuthorizationServerEndpointsConfigurer endpoints)方法
4)自定義CusJwtTokenEnhancer
5)spring oauth並不會去解析我們自定義的additionalInfo,而是只會解析他標准中的字段,如果想要拿到自定義的信息,就要引入jwt
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
代碼中處理
@GetMapping("/me")
public Object getCurrentUser(Authentication user,HttpServletRequest request) throws Exception {
String token = StringUtils.substringAfter(request.getHeader("Authorization"), "bearer ");
Claims claims = Jwts.parser().setSigningKey("cus".getBytes("UTF-8"))
.parseClaimsJws(token).getBody();
String company = (String) claims.get("company");
log.info("---------->"+company);
return user;
}