|
|
|
@ -5,6 +5,7 @@ import com.auth0.jwt.JWTCreator;
|
|
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
|
import com.auth0.jwt.interfaces.Claim;
|
|
|
|
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
|
import com.mashibing.internalcommon.dto.TokenResult;
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
@ -15,10 +16,15 @@ public class JwtUtils {
|
|
|
|
|
private static final String SIGN = "CPFmsb%!@SSAE";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String TOKEN_KEY = "passengerPhone";
|
|
|
|
|
private static final String JWT_KEY_PHONE = "phone";
|
|
|
|
|
|
|
|
|
|
// 提供 公共获取 token字符串
|
|
|
|
|
public static String gerneratorToken(String passengerPhone){
|
|
|
|
|
private static final String JWT_KEY_INDENTINY = "indentiny";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 token字符串
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public static String gerneratorToken(String passengerPhone,String indentiny){
|
|
|
|
|
|
|
|
|
|
// 准备 token过期时间 Date 类型
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
@ -29,7 +35,9 @@ public class JwtUtils {
|
|
|
|
|
JWTCreator.Builder builder = JWT.create();
|
|
|
|
|
|
|
|
|
|
// 将 有效数据部份 合成到 builder中
|
|
|
|
|
builder.withClaim(TOKEN_KEY,passengerPhone);
|
|
|
|
|
builder.withClaim(JWT_KEY_PHONE,passengerPhone);
|
|
|
|
|
builder.withClaim(JWT_KEY_INDENTINY,indentiny);
|
|
|
|
|
|
|
|
|
|
// 设置 超时时间
|
|
|
|
|
builder.withExpiresAt(date);
|
|
|
|
|
|
|
|
|
@ -40,16 +48,22 @@ public class JwtUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 网约车项目 解析和比对 传入token 返回 初始有效数据内容
|
|
|
|
|
* 解析 传入token 返回 初始有效数据内容
|
|
|
|
|
* @param token
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String parseToken(String token){
|
|
|
|
|
public static TokenResult parseToken(String token){
|
|
|
|
|
// 通过 JWT 的 required 以某种加密算法校验后 进行比对
|
|
|
|
|
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
|
|
|
|
|
// 获取有效数据内容
|
|
|
|
|
Claim claim = verify.getClaim(TOKEN_KEY);
|
|
|
|
|
return claim.toString();
|
|
|
|
|
String phone = verify.getClaim(JWT_KEY_PHONE).toString();
|
|
|
|
|
String indentiny = verify.getClaim(JWT_KEY_INDENTINY).toString();
|
|
|
|
|
|
|
|
|
|
TokenResult result = new TokenResult();
|
|
|
|
|
result.setPhone(phone);
|
|
|
|
|
result.setIndentiny(indentiny);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -57,11 +71,9 @@ public class JwtUtils {
|
|
|
|
|
// public static void main (String [ ] args ) {
|
|
|
|
|
//
|
|
|
|
|
// String tmpPhone = "13912345678";
|
|
|
|
|
// String token = gerneratorToken(tmpPhone);
|
|
|
|
|
// String token = gerneratorToken(tmpPhone,"1");
|
|
|
|
|
// System.out.println("号码 "+tmpPhone+",生成的token = " + token);
|
|
|
|
|
// String s = parseToken(token);
|
|
|
|
|
// System.out.println("根据生成token解析出的有效数据是 " + s);
|
|
|
|
|
//
|
|
|
|
|
// System.out.println("根据生成token解析出的有效数据是 " + parseToken(token));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|