You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.1 KiB
116 lines
4.1 KiB
package com.ccsens.util;
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import io.jsonwebtoken.*;
|
|
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import java.security.SignatureException;
|
|
import java.util.Date;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* @Author: __zHangSan
|
|
* @Description:
|
|
* @Date: Created in 17:40 2018/2/1
|
|
*/
|
|
public class JwtUtil {
|
|
/**
|
|
* Jwt Error Constant
|
|
*/
|
|
public static class JwtError{
|
|
public static final int TOKEN_ERRCODE_OK = 200;
|
|
public static final String TOKEN_ERRCODE_OK_PHASE = "Token ok.";
|
|
public static final int TOKEN_ERRCODE_NOTFOUND = 401;
|
|
public static final String TOKEN_ERRCODE_NOTFOUND_PHASE = "Missing or invalid Authorization header.";
|
|
public static final int TOKEN_ERRCODE_SIGNATURE_INVALIDATE = 400;
|
|
public static final String TOKEN_ERRCODE_SIGNATURE_INVALIDATE_PHASE = "Token signature encoding error.";
|
|
public static final int TOKEN_ERRCODE_EXPIRE = 402;
|
|
public static final String TOKEN_ERRCODE_EXPIRE_PHASE = "Token Expire.";
|
|
public static final int TOKEN_ERRCODE_STUB_NOT_FOUND = 403;
|
|
public static final String TOKEN_ERRCODE_STUB_NOT_FOUND_PHASE = "Token stub not found.";
|
|
public static final int TOKEN_ERRCODE_USER_DISABLED = 405;
|
|
public static final String TOKEN_ERRCODE_USER_DISABLED_PHASE = "User disabled,Please concact the System Administrator.";
|
|
public static final int TOKEN_ERRCODE_FAILED = 406;
|
|
public static final String TOKEN_ERRCODE_FAILED_PHASE = "Token failed.";
|
|
}
|
|
|
|
public static SecretKey generalKey(String secret) {
|
|
byte[] encodedKey = Base64.decode(secret);
|
|
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
|
|
return key;
|
|
}
|
|
|
|
/**
|
|
* 签发JWT
|
|
*
|
|
* @param subject
|
|
* @param ttlMillis
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static String createJWT(String subject, Map extraClaimMap, long ttlMillis,String secret) {
|
|
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
|
long nowMillis = System.currentTimeMillis();
|
|
Date now = new Date(nowMillis);
|
|
SecretKey secretKey = generalKey(secret);
|
|
JwtBuilder builder = Jwts.builder()
|
|
.setIssuedAt(now)
|
|
.signWith(signatureAlgorithm, secretKey);
|
|
if(!StrUtil.isEmpty(subject))
|
|
builder.setSubject(subject);
|
|
if(extraClaimMap != null){
|
|
Set<Map.Entry<String,Object>> entrys = extraClaimMap.entrySet();
|
|
for(Map.Entry<String,Object> entry:entrys){
|
|
builder.claim(entry.getKey(),entry.getValue());
|
|
}
|
|
}
|
|
if (ttlMillis >= 0) {
|
|
long expMillis = nowMillis + ttlMillis;
|
|
Date expDate = new Date(expMillis);
|
|
builder.setExpiration(expDate);
|
|
}
|
|
return builder.compact();
|
|
}
|
|
|
|
/**
|
|
* 签发JWT
|
|
*
|
|
* @param ttlMillis
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static String createJWT(Claims claims, long ttlMillis, String secret) {
|
|
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
|
long nowMillis = System.currentTimeMillis();
|
|
Date now = new Date(nowMillis);
|
|
SecretKey secretKey = generalKey(secret);
|
|
JwtBuilder builder = Jwts.builder()
|
|
.setClaims(claims)
|
|
.setIssuedAt(now)
|
|
.signWith(signatureAlgorithm, secretKey);
|
|
if (ttlMillis >= 0) {
|
|
long expMillis = nowMillis + ttlMillis;
|
|
Date expDate = new Date(expMillis);
|
|
builder.setExpiration(expDate);
|
|
}
|
|
return builder.compact();
|
|
}
|
|
|
|
/**
|
|
* 解析JWT字符串
|
|
*
|
|
* @param jwt
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static Claims parseJWT(String jwt, String secret)throws ExpiredJwtException,SignatureException,Exception{
|
|
SecretKey secretKey = generalKey(secret);
|
|
return Jwts.parser()
|
|
.setSigningKey(secretKey)
|
|
.parseClaimsJws(jwt)
|
|
.getBody();
|
|
}
|
|
}
|
|
|