• springboot-jjwt HS256加解密(PS:验证就是解密)


    最近项目需要用到类似access token进行加解密、验签的需求,本人在此做个小笔记记录一下,以供他人参考。

    一共会用到2中加解密,HS256 和 RS256,本文只是对 HS256做个备注,好了直接上代码,先引入jar包

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>

    核心 Util 类代码如下:

    public class JwtUtil {
    
        /**
         * token 过期时间, 单位: 秒. 这个值表示 30 天
         */
        private static final long TOKEN_EXPIRED_TIME = 30 * 24 * 60 * 60;
    
        /**
         * jwt 加密解密密钥, 这里请自行赋值,本人暂且使用随机数16位
         */
        private static final String JWT_SECRET = "1AsdadSAS123daXX";
    
        public static final String jwtId = "tokenId";
        /**
         * 创建JWT
    * @param claims 这个是payLoad的部分内容, jwt格式:jwtHeader.jwtPayLoad.Signature */ public static String createJWT(Map<String, Object> claims, Long time) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; //指定签名的时候使用的签名算法,也就是header那部分,jjwt已经将这部分内容封装好了。 Date now = new Date(System.currentTimeMillis()); SecretKey secretKey = generalKey(); long nowMillis = System.currentTimeMillis();//生成JWT的时间 //下面就是在为payload添加各种标准声明和私有声明了 JwtBuilder builder = Jwts.builder() //这里其实就是new一个JwtBuilder,设置jwt的body .setClaims(claims) //如果有私有声明,一定要先设置这个自己创建的私有的声明,这个是给builder的claim赋值,一旦写在标准的声明赋值之后,就是覆盖了那些标准的声明的 //.setId(jwtId) //设置jti(JWT ID):是JWT的唯一标识,根据业务需要,这个可以设置为一个不重复的值,主要用来作为一次性token,从而回避重放攻击。 //.setIssuedAt(now) //iat: jwt的签发时间
    .setHeader("alg", "HS256") //设置header .signWith(signatureAlgorithm, secretKey);//设置签名使用的签名算法和签名使用的秘钥 if (time >= 0) { long expMillis = nowMillis + time; Date exp = new Date(expMillis); builder.setExpiration(exp); //设置过期时间 } return builder.compact(); } /** * 验证jwt */ public static Claims verifyJwt(String token) { //签名秘钥,和生成的签名的秘钥一模一样 SecretKey key = generalKey(); Claims claims; try { claims = Jwts.parser() //得到DefaultJwtParser .setSigningKey(key) //设置签名的秘钥 .parseClaimsJws(token).getBody(); } catch (Exception e) { claims = null; }//设置需要解析的jwt return claims; } /** * 由字符串生成加密key * * @return */ public static SecretKey generalKey() { String stringKey = JWT_SECRET; byte[] encodedKey = Base64.decodeBase64(stringKey); SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "HS256"); return key; } /** * 根据userId和openid生成token */ public static String generateToken() { Map<String, Object> map = new HashMap<>(); map.put("key1", “123ssaa”); map.put("ke2", "123321213"); return createJWT(map, TOKEN_EXPIRED_TIME); } }
  • 相关阅读:
    更改 vsftpd 的端口号
    使用.NET FrameWork获取CPU,内存使用率以及磁盘空间
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/jimmyshan-study/p/10879159.html
Copyright © 2020-2023  润新知