赞
踩
在我们使用SpringOauth2过程中,一般情况下会使用JwtTokenStore来颁发及校验令牌字符串,相比较于JdbcTokenStore这种令牌存储形式来说性能要高很多。
1、spring提供的JwtTokenStore的加密算法默认为HMACSHA256,为了更安全我们如何定制实现HMACSHA512算法?
2、在网关鉴权使用的是io.jsonwebtoken.jjwt,使用JwtTokenStore生成的令牌如何与jjwt互通?
// 认证授权服务器
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
@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))); ... } }
这个问题大概意思是说无法识别颁发的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
2.1 两者互相识别的前提肯定得保证双方加密算法一致,由于JwtTokenStore默认使用的是HMACSHA256,需要定制修改为jjwt默认的HMACSHA512算法,代码参考上面
2.2 研究jjwt源码发现加密密钥经过DatatypeConverter.parseBase64Binary这个方法的处理,所以JwtTokenStore的密钥同样需要这样处理,代码参考上面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。