|
@@ -1,64 +1,60 @@
|
|
|
package com.jkcredit.query.record.util;
|
|
|
|
|
|
+import com.jkcredit.query.record.constant.CommonConstant;
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
-import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
+import org.jose4j.json.JsonUtil;
|
|
|
+import org.jose4j.jwk.RsaJsonWebKey;
|
|
|
+import org.jose4j.jws.AlgorithmIdentifiers;
|
|
|
+import org.jose4j.jws.JsonWebSignature;
|
|
|
+import org.jose4j.jwt.JwtClaims;
|
|
|
+import org.jose4j.jwt.NumericDate;
|
|
|
+import org.jose4j.lang.JoseException;
|
|
|
|
|
|
-import java.io.InputStream;
|
|
|
-import java.security.KeyStore;
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.PublicKey;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class JwtTokenUtil {
|
|
|
-
|
|
|
- private static InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jwt.jks"); // 寻找证书文件
|
|
|
- private static PrivateKey privateKey = null;
|
|
|
- private static PublicKey publicKey = null;
|
|
|
- private static Map<String, Object> claims = new HashMap<>();
|
|
|
-
|
|
|
- static { // 将证书文件里边的私钥公钥拿出来
|
|
|
+ public static String generateToken(String subject, Map<String, Object> user) {
|
|
|
+ JwtClaims claims = new JwtClaims();
|
|
|
+ claims.setGeneratedJwtId();
|
|
|
+ claims.setIssuedAtToNow();
|
|
|
+ //过期时间一定要设置,并且小于7天
|
|
|
+ NumericDate date = NumericDate.now();
|
|
|
+ date.addSeconds(60*60);
|
|
|
+ claims.setExpirationTime(date);
|
|
|
+ claims.setSubject(subject);
|
|
|
+ //添加自定义参数,所有值请都使用String类型
|
|
|
+ claims.setClaim("client_key", user.get("client_key"));
|
|
|
+ claims.setClaim("client_secret", user.get("client_secret"));
|
|
|
+
|
|
|
+ JsonWebSignature jws = new JsonWebSignature();
|
|
|
+ jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
|
|
|
+ //必须设置
|
|
|
+ jws.setKeyIdHeaderValue(CommonConstant.KEY_ID);
|
|
|
+ jws.setPayload(claims.toJson());
|
|
|
+ String jwtResult = "";
|
|
|
try {
|
|
|
- KeyStore keyStore = KeyStore.getInstance("JKS"); // java key store 固定常量
|
|
|
- keyStore.load(inputStream, "123456".toCharArray());
|
|
|
-// privateKey = (PrivateKey) keyStore.getKey("jwt", "123456".toCharArray()); // jwt 为 命令生成整数文件时的别名
|
|
|
-// publicKey = keyStore.getCertificate("jwt").getPublicKey();
|
|
|
- } catch (Exception e) {
|
|
|
+ PrivateKey privateKey = new RsaJsonWebKey(JsonUtil.parseJson(CommonConstant.TOKEN_PRIVATE_KEY)).getPrivateKey();
|
|
|
+ jws.setKey(privateKey);
|
|
|
+ jwtResult = jws.getCompactSerialization();
|
|
|
+ } catch (JoseException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- claims.put("client_key", "deb7cf03360064d7");
|
|
|
- claims.put("client_secret", "259ae98a84367d817aaf220b601b7f62");
|
|
|
- }
|
|
|
-
|
|
|
- public static String generateToken(String subject,Map<String, Object> claims, int expirationSeconds, String salt) {
|
|
|
- return Jwts.builder()
|
|
|
- .setClaims(claims)
|
|
|
- .setSubject(subject)
|
|
|
- .setExpiration(new Date(System.currentTimeMillis() + expirationSeconds * 1000))
|
|
|
- .signWith(SignatureAlgorithm.HS512, salt) // 不使用公钥私钥
|
|
|
- .compact();
|
|
|
+ return jwtResult;
|
|
|
}
|
|
|
|
|
|
- public static String parseToken(String token, String salt) {
|
|
|
+ public static String parseToken(String token, PublicKey salt) {
|
|
|
String subject = null;
|
|
|
try {
|
|
|
Claims claims = Jwts.parser()
|
|
|
- .setSigningKey(salt) // 不使用公钥私钥
|
|
|
+ .setSigningKey(salt)
|
|
|
.parseClaimsJws(token).getBody();
|
|
|
subject = claims.getSubject();
|
|
|
} catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
return subject;
|
|
|
}
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
- claims.put("client_key", "deb7cf03360064d7");
|
|
|
- claims.put("client_secret", "259ae98a84367d817aaf220b601b7f62");
|
|
|
-
|
|
|
- System.out.println(generateToken("sub", claims, 3000, "fhw4u543rth"));
|
|
|
- System.out.println(parseToken("eyJhbGciOiJIUzUxMiJ9.eyJjbGllbnRfa2V5IjoiZGViN2NmMDMzNjAwNjRkNyIsInN1YiI6InN1YiIsImNsaWVudF9zZWNyZXQiOiIyNTlhZTk4YTg0MzY3ZDgxN2FhZjIyMGI2MDFiN2Y2MiIsImV4cCI6MTYyODUwNTQ3MH0.019SN9u2-0ZahHeguNWRGYwi5HxYQrzIScrP31vQci2FE7dekLg3dFM5Yn5Edzp-rLOnEHWffhiwvXJjf3F0VQ", "fhw4u543rth"));
|
|
|
- }
|
|
|
}
|