|
|
|
@ -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.internal.dto.TokenResult;
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
@ -15,12 +16,15 @@ public class JwtUtils {
|
|
|
|
|
//盐
|
|
|
|
|
private static final String SIGN = "CPFTAXI0908%$";
|
|
|
|
|
|
|
|
|
|
private static final String JWT_KEY = "passengerPhone";
|
|
|
|
|
private static final String JWT_KEY_PHONE = "passengerPhone";
|
|
|
|
|
|
|
|
|
|
private static final String JWT_KEY_IDENTITY = "identity";
|
|
|
|
|
|
|
|
|
|
//生成token
|
|
|
|
|
public static String generatorToken(String passengerPhone) {
|
|
|
|
|
public static String generatorToken(String passengerPhone,String identity) {
|
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
|
map.put(JWT_KEY,passengerPhone);
|
|
|
|
|
map.put(JWT_KEY_PHONE,passengerPhone);
|
|
|
|
|
map.put(JWT_KEY_IDENTITY,identity);
|
|
|
|
|
//token过期时间
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.add(Calendar.DATE, 1);
|
|
|
|
@ -43,19 +47,23 @@ public class JwtUtils {
|
|
|
|
|
// Map<String, String> map = new HashMap<>();
|
|
|
|
|
// map.put("name", "zhang san");
|
|
|
|
|
// map.put("age", "19");
|
|
|
|
|
String s = generatorToken("1312312312");
|
|
|
|
|
String s = generatorToken("1312312312","1");
|
|
|
|
|
System.out.println("生成的token: " + s);
|
|
|
|
|
String jwtStr = parseToken(s);
|
|
|
|
|
System.out.println("解析Token:" + jwtStr);
|
|
|
|
|
TokenResult tokenResult = parseToken(s);
|
|
|
|
|
System.out.println("解析Token:" + tokenResult.getIdentity());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//解析Token
|
|
|
|
|
public static String parseToken(String token) {
|
|
|
|
|
public static TokenResult parseToken(String token) {
|
|
|
|
|
DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SIGN))
|
|
|
|
|
.build().verify(token);
|
|
|
|
|
Claim claim = decodedJWT.getClaim(JWT_KEY);
|
|
|
|
|
return claim != null ? claim.toString() : "";
|
|
|
|
|
Claim claimPhone = decodedJWT.getClaim(JWT_KEY_PHONE);
|
|
|
|
|
Claim claimIdentity = decodedJWT.getClaim(JWT_KEY_IDENTITY);
|
|
|
|
|
TokenResult tokenResult = new TokenResult();
|
|
|
|
|
tokenResult.setIdentity(claimIdentity.toString());
|
|
|
|
|
tokenResult.setPassengerPhone(claimPhone.toString());
|
|
|
|
|
return tokenResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|