|
|
@ -3,16 +3,12 @@ package com.mashibing.internalcommon.util;
|
|
|
|
import com.auth0.jwt.JWT;
|
|
|
|
import com.auth0.jwt.JWT;
|
|
|
|
import com.auth0.jwt.JWTCreator;
|
|
|
|
import com.auth0.jwt.JWTCreator;
|
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
|
|
|
|
|
|
|
|
import com.auth0.jwt.exceptions.SignatureVerificationException;
|
|
|
|
|
|
|
|
import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
|
|
|
|
|
|
import com.auth0.jwt.interfaces.Claim;
|
|
|
|
|
|
|
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
import com.mashibing.internalcommon.dto.ResponseResult;
|
|
|
|
|
|
|
|
import com.mashibing.internalcommon.dto.TokenResult;
|
|
|
|
import com.mashibing.internalcommon.dto.TokenResult;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public class JwtUtils {
|
|
|
|
public class JwtUtils {
|
|
|
|
|
|
|
|
|
|
|
@ -21,28 +17,36 @@ public class JwtUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String JWT_KEY_PHONE = "phone";
|
|
|
|
private static final String JWT_KEY_PHONE = "phone";
|
|
|
|
private static final String JWT_KEY_INDENTINY = "indentiny";
|
|
|
|
private static final String JWT_KEY_IDENTITY = "identity";
|
|
|
|
private static final String JWT_TOKEN_TYPE = "tokenType";
|
|
|
|
private static final String JWT_TOKEN_TYPE = "tokenType";
|
|
|
|
|
|
|
|
private static final String JWT_TOKEN_TIME = "tokenTime";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取 token字符串
|
|
|
|
* 获取 token字符串
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String gerneratorToken(String passengerPhone,String indentiny,String tokenType){
|
|
|
|
public static String gerneratorToken(String passengerPhone,String identity,String tokenType){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String,String> map = new HashMap<>();
|
|
|
|
|
|
|
|
map.put(JWT_KEY_PHONE,passengerPhone);
|
|
|
|
|
|
|
|
map.put(JWT_KEY_IDENTITY, identity);
|
|
|
|
|
|
|
|
map.put(JWT_TOKEN_TYPE, tokenType);
|
|
|
|
|
|
|
|
// 防止每次生成的token一样。
|
|
|
|
|
|
|
|
map.put(JWT_TOKEN_TIME, Calendar.getInstance().getTime().toString());
|
|
|
|
|
|
|
|
// 使用 JWT 创建 token合成对象 builder
|
|
|
|
|
|
|
|
JWTCreator.Builder builder = JWT.create();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将 map集合中有效数据部份 合成到 builder中
|
|
|
|
|
|
|
|
map.forEach(
|
|
|
|
|
|
|
|
(k,v) -> {
|
|
|
|
|
|
|
|
builder.withClaim(k,v);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// // 准备 token过期时间 Date 类型 -移交服务端进行控制
|
|
|
|
// // 准备 token过期时间 Date 类型 -移交服务端进行控制
|
|
|
|
// Calendar calendar = Calendar.getInstance();
|
|
|
|
// Calendar calendar = Calendar.getInstance();
|
|
|
|
// calendar.add(Calendar.DATE,1);
|
|
|
|
// calendar.add(Calendar.DATE,1);
|
|
|
|
// Date date = calendar.getTime();
|
|
|
|
// Date date = calendar.getTime();
|
|
|
|
|
|
|
|
// // 设置 超时时间 -移交服务端进行控制
|
|
|
|
// 使用 JWT 创建 token合成对象 builder
|
|
|
|
|
|
|
|
JWTCreator.Builder builder = JWT.create();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将 有效数据部份 合成到 builder中
|
|
|
|
|
|
|
|
builder.withClaim(JWT_KEY_PHONE,passengerPhone);
|
|
|
|
|
|
|
|
builder.withClaim(JWT_KEY_INDENTINY,indentiny);
|
|
|
|
|
|
|
|
builder.withClaim(JWT_TOKEN_TYPE,tokenType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 超时时间 -移交服务端进行控制
|
|
|
|
|
|
|
|
// builder.withExpiresAt(date);
|
|
|
|
// builder.withExpiresAt(date);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 builder对象的 sign 方法生成 token
|
|
|
|
// 使用 builder对象的 sign 方法生成 token
|
|
|
@ -61,11 +65,11 @@ public class JwtUtils {
|
|
|
|
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
|
|
|
|
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
|
|
|
|
// 获取有效数据内容
|
|
|
|
// 获取有效数据内容
|
|
|
|
String phone = verify.getClaim(JWT_KEY_PHONE).asString();
|
|
|
|
String phone = verify.getClaim(JWT_KEY_PHONE).asString();
|
|
|
|
String indentiny = verify.getClaim(JWT_KEY_INDENTINY).asString();
|
|
|
|
String indentiny = verify.getClaim(JWT_KEY_IDENTITY).asString();
|
|
|
|
|
|
|
|
|
|
|
|
TokenResult result = new TokenResult();
|
|
|
|
TokenResult result = new TokenResult();
|
|
|
|
result.setPhone(phone);
|
|
|
|
result.setPhone(phone);
|
|
|
|
result.setIndentiny(indentiny);
|
|
|
|
result.setIdentity(indentiny);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -93,7 +97,7 @@ public class JwtUtils {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return tokenResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|