当前位置:   article > 正文

SpringOauth2(一):JwtTokenStore使用HMACSHA512算法令牌、与jjwt令牌互相可识别_java-jwt 、jjwt 可以互相替换吗

java-jwt 、jjwt 可以互相替换吗

SpringOauth2(一):JwtTokenStore使用HMACSHA512算法令牌、与jjwt令牌互相可识别

在我们使用SpringOauth2过程中,一般情况下会使用JwtTokenStore来颁发及校验令牌字符串,相比较于JdbcTokenStore这种令牌存储形式来说性能要高很多。

  • 本文重点内容

1、spring提供的JwtTokenStore的加密算法默认为HMACSHA256,为了更安全我们如何定制实现HMACSHA512算法?

2、在网关鉴权使用的是io.jsonwebtoken.jjwt,使用JwtTokenStore生成的令牌如何与jjwt互通?

  • 我这里不具体介绍怎么使用SpringOauth2了,以下是本人多年经验封装的可用于实战的Spring+security+Oauth2授权/认证服务器的Starter,代码自取哦
// 认证授权服务器
https://gitee.com/yeeevip/yeee-memo/tree/master/memo-parent/memo-common/common-auth/common-platform-auth-server
https://github.com/yeeevip/yeee-memo/tree/master/memo-parent/memo-common/common-auth/common-platform-auth-server
// 认证授权客户端
https://gitee.com/yeeevip/yeee-memo/tree/master/memo-parent/memo-common/common-auth/common-platform-auth-client
https://github.com/yeeevip/yeee-memo/tree/master/memo-parent/memo-common/common-auth/common-platform-auth-client
// 只需在项目pom中引入starter依赖配置文件加一下就可以直接用了,使用案例
https://gitee.com/yeeevip/yeee-memo/tree/master/spring-cloud/auth-sso
https://github.com/yeeevip/yeee-memo/tree/master/spring-cloud/auth-sso
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1 JwtTokenStore使用HMACSHA512算法

看代码

@Configuration
public class JwtTokenStoreConfig {

    private final static String SIGN_ALGORITHM = "HMACSHA512";

    @Bean
    public AccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //配置JWT使用的秘钥
        jwtAccessTokenConverter.setSigningKey(authProperties.getJwtSecret());
        // 重点!!!
        jwtAccessTokenConverter.setSigner(new MacSigner(SIGN_ALGORITHM
                , new SecretKeySpec(DatatypeConverter.parseBase64Binary(authProperties.getJwtSecret()), SIGN_ALGORITHM)));
        jwtAccessTokenConverter.setVerifier(new MacSigner(SIGN_ALGORITHM
                , new SecretKeySpec(DatatypeConverter.parseBase64Binary(authProperties.getJwtSecret()), SIGN_ALGORITHM)));
        ...
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

常见问题

  • 1 提示 invalid_token - Cannot convert access token to JSON

这个问题大概意思是说无法识别颁发的token,检查有没有setVerifier,参考上面代码看有没有加.setVerifier(new MacSigner)

  • 完整代码
https://gitee.com/yeeevip/yeee-memo/blob/master/memo-parent/memo-base/base-security-oauth2/src/main/java/vip/yeee/memo/base/websecurityoauth2/configure/JwtTokenStoreConfig.java
https://github.com/yeeevip/yeee-memo/blob/master/memo-parent/memo-base/base-security-oauth2/src/main/java/vip/yeee/memo/base/websecurityoauth2/configure/JwtTokenStoreConfig.java
  • 1
  • 2

2 JwtTokenStore的令牌与jjwt互相识别

2.1 两者互相识别的前提肯定得保证双方加密算法一致,由于JwtTokenStore默认使用的是HMACSHA256,需要定制修改为jjwt默认的HMACSHA512算法,代码参考上面

2.2 研究jjwt源码发现加密密钥经过DatatypeConverter.parseBase64Binary这个方法的处理,所以JwtTokenStore的密钥同样需要这样处理,代码参考上面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/449997
推荐阅读
相关标签
  

闽ICP备14008679号