【fix sonar】JwtUtil文件
This commit is contained in:
+12
-14
@@ -27,10 +27,15 @@ import com.jero.common.util.oConvertUtils;
|
||||
* @Desc JWT工具类
|
||||
**/
|
||||
public class JwtUtil {
|
||||
private JwtUtil(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Token过期时间24小时(用户登录过期时间是此时间的两倍,以token在reids缓存时间为准)
|
||||
public static final long EXPIRE_TIME = 1440 * 60 * 1000;
|
||||
// public static final long EXPIRE_TIME = 2000;
|
||||
public static final long EXPIRE_TIME = 1440 * 60 * 1000L;
|
||||
|
||||
private static final String USERNAME_FIELD = "username";
|
||||
|
||||
/**
|
||||
* 校验token是否正确
|
||||
@@ -43,9 +48,9 @@ public class JwtUtil {
|
||||
try {
|
||||
// 根据密码生成JWT效验器
|
||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
||||
JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
|
||||
JWTVerifier verifier = JWT.require(algorithm).withClaim(USERNAME_FIELD, username).build();
|
||||
// 效验TOKEN
|
||||
DecodedJWT jwt = verifier.verify(token);
|
||||
verifier.verify(token);
|
||||
return true;
|
||||
} catch (Exception exception) {
|
||||
return false;
|
||||
@@ -60,7 +65,7 @@ public class JwtUtil {
|
||||
public static String getUsername(String token) {
|
||||
try {
|
||||
DecodedJWT jwt = JWT.decode(token);
|
||||
return jwt.getClaim("username").asString();
|
||||
return jwt.getClaim(USERNAME_FIELD).asString();
|
||||
} catch (JWTDecodeException e) {
|
||||
return null;
|
||||
}
|
||||
@@ -77,7 +82,7 @@ public class JwtUtil {
|
||||
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
||||
Algorithm algorithm = Algorithm.HMAC256(secret);
|
||||
// 附带username信息
|
||||
return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
|
||||
return JWT.create().withClaim(USERNAME_FIELD, username).withExpiresAt(date).sign(algorithm);
|
||||
|
||||
}
|
||||
|
||||
@@ -146,8 +151,6 @@ public class JwtUtil {
|
||||
//针对特殊标示处理#{sysOrgCode},判断替换
|
||||
if (key.contains("#{")) {
|
||||
key = key.substring(2,key.indexOf("}"));
|
||||
} else {
|
||||
key = key;
|
||||
}
|
||||
//替换为系统登录用户帐号
|
||||
if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_CODE_TABLE)) {
|
||||
@@ -202,7 +205,7 @@ public class JwtUtil {
|
||||
//update-begin-author:taoyan date:20210330 for:多租户ID作为系统变量
|
||||
else if (key.equals(DataBaseConstant.TENANT_ID) || key.toLowerCase().equals(DataBaseConstant.TENANT_ID_TABLE)){
|
||||
returnValue = sysUser.getRelTenantIds();
|
||||
if(oConvertUtils.isEmpty(returnValue) || (returnValue!=null && returnValue.indexOf(",")>0)){
|
||||
if(oConvertUtils.isEmpty(returnValue) || (returnValue!=null && returnValue.contains(","))){
|
||||
returnValue = SpringContextUtils.getHttpServletRequest().getHeader(CommonConstant.TENANT_ID);
|
||||
}
|
||||
}
|
||||
@@ -210,9 +213,4 @@ public class JwtUtil {
|
||||
if(returnValue!=null){returnValue = returnValue + moshi;}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUzMzY1MTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.xjhud_tWCNYBOg_aRlMgOdlZoWFFKB_givNElHNw3X0";
|
||||
// System.out.println(JwtUtil.getUsername(token));
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user