|
|
@ -3,19 +3,22 @@ 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.interfaces.Claim;
|
|
|
|
|
|
|
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class JwtUtils {
|
|
|
|
public class JwtUtils {
|
|
|
|
|
|
|
|
|
|
|
|
// 盐:服务端本地私有
|
|
|
|
// 盐:服务端本地私有
|
|
|
|
private static final String SIGN = "CPFmsb%!@SSAE";
|
|
|
|
private static final String SIGN = "CPFmsb%!@SSAE";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String TOKEN_KEY = "passengerPhone";
|
|
|
|
|
|
|
|
|
|
|
|
// 提供 公共获取 token字符串
|
|
|
|
// 提供 公共获取 token字符串
|
|
|
|
public static String gerneratorToken(Map<String,String> map){
|
|
|
|
public static String gerneratorToken(String passengerPhone){
|
|
|
|
|
|
|
|
|
|
|
|
// 准备 token过期时间 Date 类型
|
|
|
|
// 准备 token过期时间 Date 类型
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
@ -25,15 +28,8 @@ public class JwtUtils {
|
|
|
|
// 使用 JWT 创建 token合成对象 builder
|
|
|
|
// 使用 JWT 创建 token合成对象 builder
|
|
|
|
JWTCreator.Builder builder = JWT.create();
|
|
|
|
JWTCreator.Builder builder = JWT.create();
|
|
|
|
|
|
|
|
|
|
|
|
// 将 map 中有效数据部份 遍历合成到 builder中
|
|
|
|
// 将 有效数据部份 合成到 builder中
|
|
|
|
if(map == null && map.size() <= 0){// 有效数据为空,直接返回空值
|
|
|
|
builder.withClaim(TOKEN_KEY,passengerPhone);
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map.forEach(
|
|
|
|
|
|
|
|
(k,v)-> {
|
|
|
|
|
|
|
|
builder.withClaim(k,v);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
// 设置 超时时间
|
|
|
|
// 设置 超时时间
|
|
|
|
builder.withExpiresAt(date);
|
|
|
|
builder.withExpiresAt(date);
|
|
|
|
|
|
|
|
|
|
|
@ -43,13 +39,29 @@ public class JwtUtils {
|
|
|
|
return sign;
|
|
|
|
return sign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
public static void main (String [ ] args ) {
|
|
|
|
* 网约车项目 解析和比对 传入token 返回 初始有效数据内容
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
* @param token
|
|
|
|
map.put("name", "zhang san");
|
|
|
|
* @return
|
|
|
|
map.put("age", "18");
|
|
|
|
*/
|
|
|
|
String s = gerneratorToken(map);
|
|
|
|
public static String parseToken(String token){
|
|
|
|
System.out.println("生成的token:" + s);
|
|
|
|
// 通过 JWT 的 required 以某种加密算法校验后 进行比对
|
|
|
|
|
|
|
|
DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
|
|
|
|
|
|
|
|
// 获取有效数据内容
|
|
|
|
|
|
|
|
Claim claim = verify.getClaim(TOKEN_KEY);
|
|
|
|
|
|
|
|
return claim.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static void main (String [ ] args ) {
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// String tmpPhone = "13912345678";
|
|
|
|
|
|
|
|
// String token = gerneratorToken(tmpPhone);
|
|
|
|
|
|
|
|
// System.out.println("号码 "+tmpPhone+",生成的token = " + token);
|
|
|
|
|
|
|
|
// String s = parseToken(token);
|
|
|
|
|
|
|
|
// System.out.println("根据生成token解析出的有效数据是 " + s);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|