• JWT简洁版


    添加Constant类

     1 package top.bigking.constant;
     2 
     3 /**
     4  * @Author ABKing
     5  * @since 2020/5/14 下午5:58
     6  **/
     7 
     8 public class Constant {
     9 
    10     /**
    11      * 数据请求返回码
    12      */
    13     public static final int RESCODE_SUCCESS = 1000;             //成功
    14     public static final int RESCODE_SUCCESS_MSG = 1001;          //成功(有返回信息)
    15     public static final int RESCODE_EXCEPTION = 1002;        //请求抛出异常
    16     public static final int RESCODE_NOLOGIN = 1003;             //未登陆状态
    17     public static final int RESCODE_NOEXIST = 1004;             //查询结果为空
    18     public static final int RESCODE_NOAUTH = 1005;          //无操作权限
    19     public static final int RESCODE_LOGINEXPIRE = 1006;             //登录过期
    20     /**
    21      * token
    22      */
    23     public static final int JWT_ERRCODE_EXPIRE = 1007;//Token过期
    24     public static final int JWT_ERRCODE_FAIL = 1008;//验证不通过
    25 
    26     /**
    27      * jwt
    28      */
    29     public static final String JWT_ID = "jwt-ABKing";//jwt签发者
    30     public static final String JWT_SECRET = "Isi5Ob9OfvJt+4IHoMJlHkS1ttg=";//密匙
    31     public static final int JWT_TTL = 60*60*1000; // 60*60*1000;  //millisecond
    32     public static final int JWT_REFRESH_INTERVAL = 18*1000; //55*60*1000;  //millisecond
    33     public static final int JWT_REFRESH_TTL = 60*1000; // 12*60*60*1000;  //millisecond
    34 
    35 }

    添加JwtUtil类

     1 package top.bigking.util;
     2 
     3 /**
     4  * @Author ABKing
     5  * @since 2020/5/21 下午6:30
     6  **/
     7 
     8 import java.util.Date;
     9 
    10 import javax.crypto.SecretKey;
    11 import javax.crypto.spec.SecretKeySpec;
    12 
    13 import org.apache.commons.codec.binary.Base64;
    14 
    15 
    16 import io.jsonwebtoken.Claims;
    17 import io.jsonwebtoken.ExpiredJwtException;
    18 import io.jsonwebtoken.JwtBuilder;
    19 import io.jsonwebtoken.Jwts;
    20 import io.jsonwebtoken.MalformedJwtException;
    21 import io.jsonwebtoken.SignatureAlgorithm;
    22 import io.jsonwebtoken.SignatureException;
    23 import io.jsonwebtoken.UnsupportedJwtException;
    24 
    25 /**
    26  * JWTUtils工具类,生成jwt和解析jwt
    27  * JSON WEB TOKEN 结构组成:
    28  * (1)Header(头部):包含加密算法,通常直接使用 HMAC SHA256
    29  * (2)Payload(负载):存放有效信息,比如消息体、签发者、过期时间、签发时间等
    30  * (3)Signature(签名):由header(base64后的)+payload(base64后的)+secret(秘钥)三部分组合,然后通过head中声明的算法进行加密
    31  * @author sixmonth
    32  * @date 2019年3月20日
    33  *
    34  */
    35 public class JwtUtil {
    36 
    37     static String SECRETKEY = "KJHUhjjJYgYUllVbXhKDHXhkSyHjlNiVkYzWTBac1Yxkjhuad";
    38 
    39     /**
    40      * 由字符串生成加密key
    41      *
    42      * @return
    43      */
    44     public static SecretKey generalKey(String stringKey) {
    45         byte[] encodedKey = Base64.decodeBase64(stringKey);
    46         SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "HmacSHA256");
    47         return key;
    48     }
    49 
    50     /**
    51      * 创建jwt
    52      * @param id 唯一id,uuid即可
    53      * @param subject json形式字符串或字符串,增加用户非敏感信息存储,如用户id或用户账号,与token解析后进行对比,防止乱用
    54      * @param expirationDate  生成jwt的有效期,单位秒
    55      * @return jwt token
    56      * @throws Exception
    57      */
    58     public static String createJWT(String uuid, String subject, long expirationDate) throws Exception {
    59         SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
    60         long nowMillis = System.currentTimeMillis();
    61         Date now = new Date(nowMillis);
    62         SecretKey key = generalKey(SECRETKEY);
    63         JwtBuilder builder = Jwts.builder().setIssuer("").setId(uuid).setIssuedAt(now).setSubject(subject)
    64                 .signWith(signatureAlgorithm, key);
    65         if (expirationDate >= 0) {
    66             long expMillis = nowMillis + expirationDate*1000;
    67             Date exp = new Date(expMillis);
    68             builder.setExpiration(exp);
    69         }
    70         return builder.compact();
    71     }
    72 
    73     /**
    74      * 解密jwt,获取实体
    75      * @param jwt
    76      */
    77     public static Claims parseJWT(String jwt) throws ExpiredJwtException, UnsupportedJwtException,
    78             MalformedJwtException, SignatureException, IllegalArgumentException {
    79         SecretKey key = generalKey(SECRETKEY);
    80         Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(jwt).getBody();
    81         return claims;
    82     }
    83 
    84 }

    pom.xml

     1     <!--  json web token  -->
     2     <dependency>
     3       <groupId>io.jsonwebtoken</groupId>
     4       <artifactId>jjwt-api</artifactId>
     5       <version>0.10.5</version>
     6     </dependency>
     7     <dependency>
     8       <groupId>io.jsonwebtoken</groupId>
     9       <artifactId>jjwt-impl</artifactId>
    10       <version>0.10.5</version>
    11       <scope>runtime</scope>
    12     </dependency>
    13     <dependency>
    14       <groupId>io.jsonwebtoken</groupId>
    15       <artifactId>jjwt-jackson</artifactId>
    16       <version>0.10.5</version>
    17       <scope>runtime</scope>
    18     </dependency>
    19     <dependency>
    20       <groupId>com.alibaba</groupId>
    21       <artifactId>fastjson</artifactId>
    22       <version>1.2.56</version>
    23     </dependency>
    24     <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    25     <dependency>
    26       <groupId>commons-codec</groupId>
    27       <artifactId>commons-codec</artifactId>
    28       <version>1.10</version>
    29     </dependency>
    金麟岂是池中物,一遇风云便化龙!
  • 相关阅读:
    count(*) 和 count(1)和count(列名)区别
    网页横向滚动条
    发送公众号模板消息
    tp中S与session()
    php 判断sql执行时间
    thinkphp联查
    php 获取当前时间
    微信分享
    测试用手机奇怪问题
    翻译|多少植物才能净化室内空气?
  • 原文地址:https://www.cnblogs.com/ABKing/p/12934300.html
Copyright © 2020-2023  润新知