spring cloud oauth2 jwt 自定义拓展

短命女 2023-07-04 04:47 31阅读 0赞

spring cloud oauth2 jwt 自定义拓展

**************************

相关类及接口

TokenEnhancer:token增强接口

  1. public interface TokenEnhancer {
  2. OAuth2AccessToken enhance(OAuth2AccessToken var1, OAuth2Authentication var2);
  3. }

OAuth2AccessToken

  1. public interface OAuth2AccessToken {
  2. String BEARER_TYPE = "Bearer";
  3. String OAUTH2_TYPE = "OAuth2";
  4. String ACCESS_TOKEN = "access_token";
  5. String TOKEN_TYPE = "token_type";
  6. String EXPIRES_IN = "expires_in";
  7. String REFRESH_TOKEN = "refresh_token";
  8. String SCOPE = "scope";
  9. Map<String, Object> getAdditionalInformation();
  10. Set<String> getScope();
  11. OAuth2RefreshToken getRefreshToken();
  12. String getTokenType();
  13. boolean isExpired();
  14. Date getExpiration();
  15. int getExpiresIn();
  16. String getValue();
  17. }

DefaultOAuth2AccessToken:默认的token实现类

  1. public class DefaultOAuth2AccessToken implements Serializable, OAuth2AccessToken {
  2. private static final long serialVersionUID = 914967629530462926L;
  3. private String value;
  4. private Date expiration;
  5. private String tokenType;
  6. private OAuth2RefreshToken refreshToken;
  7. private Set<String> scope;
  8. private Map<String, Object> additionalInformation;
  9. *************
  10. 构造方法
  11. public DefaultOAuth2AccessToken(String value) {
  12. this.tokenType = "Bearer".toLowerCase();
  13. this.additionalInformation = Collections.emptyMap();
  14. this.value = value;
  15. }
  16. public DefaultOAuth2AccessToken(OAuth2AccessToken accessToken) {
  17. *************
  18. 普通方法
  19. public void setValue(String value) {
  20. public void setExpiration(Date expiration) {
  21. public void setTokenType(String tokenType) {
  22. public void setRefreshToken(OAuth2RefreshToken refreshToken) {
  23. public void setScope(Set<String> scope) {
  24. public void setAdditionalInformation(Map<String, Object> additionalInformation) {
  25. public String getValue() {
  26. public int getExpiresIn() {
  27. public Date getExpiration() {
  28. public String getTokenType() {
  29. public OAuth2RefreshToken getRefreshToken() {
  30. public Set<String> getScope() {
  31. public Map<String, Object> getAdditionalInformation() {
  32. public boolean isExpired() {
  33. public static OAuth2AccessToken valueOf(Map<String, String> tokenParams) {

OAuth2Authentication:认证信息

  1. public class OAuth2Authentication extends AbstractAuthenticationToken {
  2. private static final long serialVersionUID = -4809832298438307309L;
  3. private final OAuth2Request storedRequest;
  4. private final Authentication userAuthentication;
  5. *************
  6. 构造方法
  7. public OAuth2Authentication(OAuth2Request storedRequest, Authentication userAuthentication) {
  8. *************
  9. 普通方法
  10. public Object getPrincipal() {
  11. public boolean isClientOnly() {
  12. public OAuth2Request getOAuth2Request() {
  13. public Authentication getUserAuthentication() {
  14. public boolean isAuthenticated() {
  15. public void eraseCredentials() {

TokenEnhancerChain:可添加多个tokenEnhancer

  1. public class TokenEnhancerChain implements TokenEnhancer {
  2. private List<TokenEnhancer> delegates = Collections.emptyList();
  3. public TokenEnhancerChain() {
  4. }
  5. public void setTokenEnhancers(List<TokenEnhancer> delegates) {
  6. this.delegates = delegates;
  7. }
  8. public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
  9. OAuth2AccessToken result = accessToken;
  10. TokenEnhancer enhancer;
  11. for(Iterator var4 = this.delegates.iterator(); var4.hasNext(); result = enhancer.enhance(result, authentication)) {
  12. enhancer = (TokenEnhancer)var4.next();
  13. }
  14. return result;
  15. }
  16. }

**************************

示例

资源服务器配置参spring cloud oauth2 jwt 使用示例

********************

认证服务器

JwtTokenEnhancer

  1. @Component
  2. public class JwtTokenEnhancer implements TokenEnhancer {
  3. @Override
  4. public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
  5. Map<String,Object> map=new HashMap<>();
  6. map.put("extension","jwt 拓展信息");
  7. ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(map);
  8. return oAuth2AccessToken;
  9. }
  10. }

OAuth2ServerConfiguration:认证服务器配置

  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class OAuth2ServerConfiguration extends AuthorizationServerConfigurerAdapter {
  4. @Resource
  5. private AuthenticationManager authenticationManager;
  6. @Resource
  7. private BCryptPasswordEncoder passwordEncoder;
  8. @Resource
  9. private UserService userService;
  10. @Resource
  11. private JwtTokenStore jwtTokenStore;
  12. @Resource
  13. private JwtAccessTokenConverter jwtAccessTokenConverter;
  14. @Resource
  15. private JwtTokenEnhancer jwtTokenEnhancer;
  16. public TokenEnhancerChain initTokenEnhancerChain(){
  17. TokenEnhancerChain tokenEnhancerChain=new TokenEnhancerChain();
  18. List<TokenEnhancer> list=new ArrayList<>();
  19. list.add(jwtTokenEnhancer); //添加自定义tokenEnhancer
  20. list.add(jwtAccessTokenConverter); //将token转换为jwt
  21. tokenEnhancerChain.setTokenEnhancers(list);
  22. return tokenEnhancerChain;
  23. }
  24. @Override
  25. public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
  26. authorizationServerEndpointsConfigurer
  27. .tokenStore(jwtTokenStore)
  28. .accessTokenConverter(jwtAccessTokenConverter) //添加tokenConverter
  29. .tokenEnhancer(initTokenEnhancerChain()) //添加tokenEnhancerChain
  30. .authenticationManager(authenticationManager)
  31. .userDetailsService(userService);
  32. }
  33. @Override
  34. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  35. clients.inMemory().withClient("user")
  36. .secret(passwordEncoder.encode("123456"))
  37. .authorizedGrantTypes("authorization_code","refresh_token")
  38. .redirectUris("http://localhost:8082/redirect")
  39. .accessTokenValiditySeconds(3000)
  40. .autoApprove(true)
  41. .scopes("user");
  42. }
  43. @Override
  44. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  45. security.allowFormAuthenticationForClients()
  46. .tokenKeyAccess("isAuthenticated()") //获取token
  47. .checkTokenAccess("isAuthenticated()"); //验证token
  48. }
  49. }

**************************

使用测试

********************

获取jwt

localhost:8081/oauth/authorize

查询参数:client_id=user&response_type=code&redirect_uri=http://localhost:8082/redirect

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkzMTYyNQ_size_16_color_FFFFFF_t_70][]

token解码

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkzMTYyNQ_size_16_color_FFFFFF_t_70 1][]

说明:自定义拓展信息extension已经添加到token中

发表评论

表情:
评论列表 (有 0 条评论,31人围观)

还没有评论,来说两句吧...

相关阅读