JJwt生成Token

JJwt生成token

java中通过jjwt生成token

package com.zom.statistics.tools;

import com.zom.statistics.DTO.JwtParams;
import com.zom.statistics.DTO.RtvConsoleUser;
import com.zom.statistics.exception.LogonException;
import io.jsonwebtoken.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

public class JwtTokenUtil {

    public static final String AUTH_HEADER_KEY = "Authorization";

    public static final String TOKEN_PREFIX = "Bearer ";

    private static Logger log = LoggerFactory.getLogger(JwtTokenUtil.class);

    /**
     * 解析jwt
     * @param jsonWebToken
     * @param base64Security
     * @return
     */
    public static Claims parseJWT(String jsonWebToken, String base64Security) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(DatatypeConverter.parseBase64Binary(base64Security))
                    .parseClaimsJws(jsonWebToken).getBody();
            return claims;
        } catch (ExpiredJwtException eje) {
            log.error("===== Token过期 =====", eje);
            throw new LogonException("Token过期");
        } catch (Exception e){
            log.error("===== token解析异常 =====", e);
            throw new LogonException("token解析异常");
        }
    }

    /**
     * 构建jwt
     * @param user        用户
     * @param msg         用户信息
     * @param role        角色
     * @param expire      过期时间
     * @return
     */
    public static String createJWT(RtvConsoleUser user, String msg, String role, String expire) {
        try {
            // 使用HS256加密算法
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

            long nowMillis = System.currentTimeMillis();
            Date now = new Date(nowMillis);

            //生成签名密钥
            byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(JwtParams.SECRET);
            Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

            //添加构成JWT的参数
            JwtBuilder builder = Jwts.builder().setId(String.valueOf(user.getId()))
                    .setHeaderParam("typ", "JWT")
                    // 可以将基本不重要的对象信息放到claims
                    .claim("role", role)
                    .claim("uniqueId", user.getDepartmentId())
                    .claim("corpId", user.getCorpId())
                    .claim("userId", user.getId())
                    .setSubject(msg)           // 代表这个JWT的主体,即它的所有人
                    .setIssuedAt(new Date())        // 是一个时间戳,代表这个JWT的签发时间;
                    .signWith(signatureAlgorithm, signingKey);
            //添加Token过期时间
            int TTLMillis = Integer.parseInt(expire) * 1000;
            if (TTLMillis >= 0) {
                long expMillis = nowMillis + TTLMillis;
                Date exp = new Date(expMillis);
                builder.setExpiration(exp)  // 是一个时间戳,代表这个JWT的过期时间;
                        .setNotBefore(now); // 是一个时间戳,代表这个JWT生效的开始时间,意味着在这个时间之前验证JWT是会失败的
            }
            //生成JWT
            return builder.compact();
        } catch (Exception e) {
            log.error("签名失败", e);
            throw new RuntimeException("签名失败");
        }
    }

    /**
     * 从token中获取corpId
     * @param token
     * @return
     */
    public static Integer getCorpId(String token){
        return parseJWT(token.substring(7), JwtParams.SECRET).get("corpId", Integer.class);
    }

    /**
     * 从token中获取用户ID
     * @param token
     * @return
     */
    public static Integer getUserId(String token){
        Integer userId = parseJWT(token.substring(7), JwtParams.SECRET).get("userId", Integer.class);
        return userId;
    }

    /**
     * 是否已过期
     * @param token
     * @return
     */
    public static boolean isExpiration(String token) {
        return parseJWT(token.substring(7), JwtParams.SECRET).getExpiration().before(new Date());
    }


}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值