|
|
|
@ -17,66 +17,27 @@ public class TokenUtil {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* token秘钥
|
|
|
|
* token秘钥
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static final String TOKEN_SECRET = "apiManager";
|
|
|
|
private static String TOKEN_SECRET;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置过期时间
|
|
|
|
* 设置过期时间
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static final long EXPIRE_DATE = 30 * 60 * 100000;
|
|
|
|
private static long EXPIRE_DATE;
|
|
|
|
|
|
|
|
|
|
|
|
public static String token(String secreteKey) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String token = "";
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
String SystemType = PropertiesUtil.getValue("SystemType");
|
|
|
|
//过期时间
|
|
|
|
if (SystemType == null || SystemType.equals("0") || SystemType.equals("2")) {
|
|
|
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
|
|
|
|
TOKEN_SECRET = "apiManager";
|
|
|
|
//秘钥及加密算法
|
|
|
|
} else if (SystemType.equals("1")) {
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
TOKEN_SECRET = "5267915";
|
|
|
|
//设置头部信息
|
|
|
|
|
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
|
|
|
|
|
header.put("typ", "JWT");
|
|
|
|
|
|
|
|
header.put("alg", "HS256");
|
|
|
|
|
|
|
|
//携带username,password信息,生成签名
|
|
|
|
|
|
|
|
token = JWT.create()
|
|
|
|
|
|
|
|
.withHeader(header)
|
|
|
|
|
|
|
|
.withClaim("secreteKey", secreteKey)
|
|
|
|
|
|
|
|
.withExpiresAt(date)
|
|
|
|
|
|
|
|
.sign(algorithm);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("获取token异常", e);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
String tokenOutTime = PropertiesUtil.getValue("tokenOutTime");
|
|
|
|
}
|
|
|
|
if (tokenOutTime.isEmpty()) {
|
|
|
|
|
|
|
|
tokenOutTime = "30";
|
|
|
|
public static boolean verify(String token, HttpServletRequest request) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @desc 验证token,通过返回true
|
|
|
|
|
|
|
|
* @params [token]需要校验的串
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
|
|
|
|
JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
|
|
|
|
DecodedJWT jwt = verifier.verify(token);
|
|
|
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
|
|
|
String Man = jwt.getClaim("Man").toString().replaceAll( "///" ,"");
|
|
|
|
|
|
|
|
String Manid = jwt.getClaim("Manid").toString().replaceAll( "///" ,"");
|
|
|
|
|
|
|
|
String Manno = jwt.getClaim("Manno").toString().replaceAll( "///" ,"");
|
|
|
|
|
|
|
|
if (Man == null || Man.equals("") ||
|
|
|
|
|
|
|
|
Manid == null || Manid.equals("") ||
|
|
|
|
|
|
|
|
Manno == null || Manno.equals("")) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
session.setAttribute("Man", Man);
|
|
|
|
|
|
|
|
session.setAttribute("Manid", Manid);
|
|
|
|
|
|
|
|
session.setAttribute("Manno", Manno);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("验证token异常", e);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPIRE_DATE = Long.parseLong(tokenOutTime) * 60 * 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean verify(String token) {
|
|
|
|
public static boolean verify(String token) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @desc 验证token,通过返回true
|
|
|
|
* @desc 验证token,通过返回true
|
|
|
|
@ -85,14 +46,15 @@ public class TokenUtil {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
DecodedJWT jwt = verifier.verify(token);
|
|
|
|
verifier.verify(token);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("验证token异常", e);
|
|
|
|
log.error("验证token异常", e);
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static String reToken(String token) {
|
|
|
|
|
|
|
|
|
|
|
|
public static String reToken(String token, HttpServletRequest request) {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @desc 验证token,通过返回true
|
|
|
|
* @desc 验证token,通过返回true
|
|
|
|
* @params [token]需要校验的串
|
|
|
|
* @params [token]需要校验的串
|
|
|
|
@ -101,9 +63,9 @@ public class TokenUtil {
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
JWTVerifier verifier = JWT.require(algorithm).build();
|
|
|
|
DecodedJWT jwt = verifier.verify(token);
|
|
|
|
DecodedJWT jwt = verifier.verify(token);
|
|
|
|
String Man = jwt.getClaim("Man").toString().replaceAll( "///" ,"");;
|
|
|
|
String Man = jwt.getClaim("Man").toString().replaceAll("\\\"", "");
|
|
|
|
String Manid = jwt.getClaim("Manid").toString().replaceAll( "///" ,"");;
|
|
|
|
String Manid = jwt.getClaim("Manid").toString().replaceAll("\\\"", "");
|
|
|
|
String Manno = jwt.getClaim("Manno").toString().replaceAll( "///" ,"");;
|
|
|
|
String Manno = jwt.getClaim("Manno").toString().replaceAll("\\\"", "");
|
|
|
|
//过期时间
|
|
|
|
//过期时间
|
|
|
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
|
|
|
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
|
|
|
|
//秘钥及加密算法
|
|
|
|
//秘钥及加密算法
|
|
|
|
@ -111,7 +73,6 @@ public class TokenUtil {
|
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
|
Map<String, Object> header = new HashMap<>();
|
|
|
|
header.put("typ", "JWT");
|
|
|
|
header.put("typ", "JWT");
|
|
|
|
header.put("alg", "HS256");
|
|
|
|
header.put("alg", "HS256");
|
|
|
|
//携带username,password信息,生成签名
|
|
|
|
|
|
|
|
token = JWT.create()
|
|
|
|
token = JWT.create()
|
|
|
|
.withHeader(header)
|
|
|
|
.withHeader(header)
|
|
|
|
.withClaim("Man", Man)
|
|
|
|
.withClaim("Man", Man)
|
|
|
|
@ -119,6 +80,11 @@ public class TokenUtil {
|
|
|
|
.withClaim("Manno", Manno)
|
|
|
|
.withClaim("Manno", Manno)
|
|
|
|
.withExpiresAt(date)
|
|
|
|
.withExpiresAt(date)
|
|
|
|
.sign(algorithm);
|
|
|
|
.sign(algorithm);
|
|
|
|
|
|
|
|
HttpSession session = request.getSession();
|
|
|
|
|
|
|
|
session.setAttribute("token", token);
|
|
|
|
|
|
|
|
session.setAttribute("Man", Man);
|
|
|
|
|
|
|
|
session.setAttribute("Manid", Manid);
|
|
|
|
|
|
|
|
session.setAttribute("Manno", Manno);
|
|
|
|
return token;
|
|
|
|
return token;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("获取token异常", e);
|
|
|
|
log.error("获取token异常", e);
|
|
|
|
|