spring security oauth2 整合 JWT

 前言

在这个基础上,进行整合。

spring security oauth2学习 -- 快速入门_本郡主是喵的博客-CSDN博客

1.jwt的一般使用

先把 reids,common-pools 等依赖删掉。

删掉redis的下相关配置

1.1 导入依赖

		
		
			io.jsonwebtoken
			jjwt
			0.9.1
		

1.2 核心代码

创建 jwtTokenConfig.java

@Configuration
public class jwtTokenConfig {

    @Bean
    public JwtTokenStore jwtTokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    
    // // jwt令牌转换器,将内置token转换成jwt
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        // 配合JWT使用的秘钥
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey("test_key");
        return accessTokenConverter;
    }
}

AuthenticationServer.java 里面新增这些。

 @Autowired
    private JwtTokenStore tokenStore;

    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    /**
     *  密码模式
     * @param
     * @return
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                // 配置令牌存储策略
                .tokenStore(tokenStore)
                .accessTokenConverter(jwtAccessTokenConverter);
    }

 运行,启动!

spring security oauth2 整合 JWT_第1张图片

 复制这个token去官网解析。

spring security oauth2 整合 JWT_第2张图片

 2.扩展jwt的内容

我们新建JwtTokenEnhancer.java,指定扩容的内容。

AuthenticationServer.java ,配置中,加入你的扩容配置,和token转换器。

新建 JwtTokenEnhancer.java 


public class JwtTokenEnhancer implements TokenEnhancer {
    // 往我们的jwt中添加自定义的信息
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        HashMap info = new HashMap<>();
        info.put("enhance","enhance info");
        ((DefaultOAuth2AccessToken)oAuth2AccessToken).setAdditionalInformation(info);
        return oAuth2AccessToken;
    }
}

JwtTokenConfig.java 新加,这个bean 就是我们自定义的bean。

 @Bean
    public JwtTokenEnhancer jwtTokenEnhancer(){
        return new JwtTokenEnhancer();
    }

 往 AuthenticationServer.java 新加

    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 配置令牌扩容策略
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();

        ArrayList delegates = new ArrayList<>();
        delegates.add(jwtTokenEnhancer);
        delegates.add(jwtAccessTokenConverter);
        // 我们扩容的token也是需要令牌转换成jwt
        enhancerChain.setTokenEnhancers(delegates);

        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                // 配置令牌存储策略
                .tokenStore(tokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(enhancerChain); // 使用令牌扩容
    }

用postMan重新访问那个网址。

拿到token解析。

spring security oauth2 整合 JWT_第3张图片

 3.解析jwt的内容

衔接上个博客,修改我们 UserController.java 中的

public Object getCurrentUser(Authentication authentication,HttpServletRequest request){
        // 从请求头中,那jwt token
        String header = request.getHeader("Authorization");
        String token = header.substring(header.indexOf("bearer") + 7);

        Object principal = authentication.getPrincipal();
        return Jwts.parser()
                .setSigningKey("test_key")
                .parseClaimsJws(token)
                .getBody();

    }

 我们还是从授权服务器拿到token,后直接资源服务器拿内容。

spring security oauth2 整合 JWT_第4张图片

 4.开启刷新令牌

往我们授权服务器配置中,

spring security oauth2 整合 JWT_第5张图片

 .authorizedGrantTypes("password","refresh_token","authorization_code"); 
// 开启刷新令牌,貌似传入多个模式参数,能够开启授权服务器多个模式

 打开postMan,访问原先拿token,会发现多了个refresh_token 键值对

spring security oauth2 整合 JWT_第6张图片

 按照下图,这个将你访问的路径复制一份。这个refresh_ token 的value值就是,上图的刷新令牌。spring security oauth2 整合 JWT_第7张图片

你可能感兴趣的:(Spring,spring,java,数学建模)