【fix sonar】JwtUtil文件

This commit is contained in:
梁琦涛
2023-03-03 14:51:04 +08:00
parent 2dff6ad29b
commit 8bacbb5cb0
@@ -27,10 +27,15 @@ import com.jero.common.util.oConvertUtils;
* @Desc JWT工具类 * @Desc JWT工具类
**/ **/
public class JwtUtil { public class JwtUtil {
private JwtUtil(){
}
// Token过期时间24小时(用户登录过期时间是此时间的两倍,以token在reids缓存时间为准) // Token过期时间24小时(用户登录过期时间是此时间的两倍,以token在reids缓存时间为准)
public static final long EXPIRE_TIME = 1440 * 60 * 1000; public static final long EXPIRE_TIME = 1440 * 60 * 1000L;
// public static final long EXPIRE_TIME = 2000;
private static final String USERNAME_FIELD = "username";
/** /**
* 校验token是否正确 * 校验token是否正确
@@ -43,9 +48,9 @@ public class JwtUtil {
try { try {
// 根据密码生成JWT效验器 // 根据密码生成JWT效验器
Algorithm algorithm = Algorithm.HMAC256(secret); 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 // 效验TOKEN
DecodedJWT jwt = verifier.verify(token); verifier.verify(token);
return true; return true;
} catch (Exception exception) { } catch (Exception exception) {
return false; return false;
@@ -60,7 +65,7 @@ public class JwtUtil {
public static String getUsername(String token) { public static String getUsername(String token) {
try { try {
DecodedJWT jwt = JWT.decode(token); DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("username").asString(); return jwt.getClaim(USERNAME_FIELD).asString();
} catch (JWTDecodeException e) { } catch (JWTDecodeException e) {
return null; return null;
} }
@@ -77,13 +82,13 @@ public class JwtUtil {
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
Algorithm algorithm = Algorithm.HMAC256(secret); Algorithm algorithm = Algorithm.HMAC256(secret);
// 附带username信息 // 附带username信息
return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm); return JWT.create().withClaim(USERNAME_FIELD, username).withExpiresAt(date).sign(algorithm);
} }
/** /**
* 根据request中的token获取用户账号 * 根据request中的token获取用户账号
* *
* @param request * @param request
* @return * @return
* @throws JeroBootException * @throws JeroBootException
@@ -96,7 +101,7 @@ public class JwtUtil {
} }
return username; return username;
} }
/** /**
* 从session中获取变量 * 从session中获取变量
* @param key * @param key
@@ -121,7 +126,7 @@ public class JwtUtil {
if(returnValue!=null){returnValue = returnValue + moshi;} if(returnValue!=null){returnValue = returnValue + moshi;}
return returnValue; return returnValue;
} }
/** /**
* 从当前用户中获取变量 * 从当前用户中获取变量
* @param key * @param key
@@ -134,10 +139,10 @@ public class JwtUtil {
user = JeroDataAutorUtils.loadUserInfo(); user = JeroDataAutorUtils.loadUserInfo();
} }
//#{sys_user_code}% //#{sys_user_code}%
// 获取登录用户信息 // 获取登录用户信息
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
String moshi = ""; String moshi = "";
if(key.indexOf("}")!=-1){ if(key.indexOf("}")!=-1){
moshi = key.substring(key.indexOf("}")+1); moshi = key.substring(key.indexOf("}")+1);
@@ -146,8 +151,6 @@ public class JwtUtil {
//针对特殊标示处理#{sysOrgCode},判断替换 //针对特殊标示处理#{sysOrgCode},判断替换
if (key.contains("#{")) { if (key.contains("#{")) {
key = key.substring(2,key.indexOf("}")); key = key.substring(2,key.indexOf("}"));
} else {
key = key;
} }
//替换为系统登录用户帐号 //替换为系统登录用户帐号
if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_CODE_TABLE)) { if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_CODE_TABLE)) {
@@ -165,7 +168,7 @@ public class JwtUtil {
returnValue = user.getSysUserName(); returnValue = user.getSysUserName();
} }
} }
//替换为系统用户登录所使用的机构编码 //替换为系统用户登录所使用的机构编码
else if (key.equals(DataBaseConstant.SYS_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_ORG_CODE_TABLE)) { else if (key.equals(DataBaseConstant.SYS_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_ORG_CODE_TABLE)) {
if(user==null) { if(user==null) {
@@ -202,7 +205,7 @@ public class JwtUtil {
//update-begin-author:taoyan date:20210330 for:多租户ID作为系统变量 //update-begin-author:taoyan date:20210330 for:多租户ID作为系统变量
else if (key.equals(DataBaseConstant.TENANT_ID) || key.toLowerCase().equals(DataBaseConstant.TENANT_ID_TABLE)){ else if (key.equals(DataBaseConstant.TENANT_ID) || key.toLowerCase().equals(DataBaseConstant.TENANT_ID_TABLE)){
returnValue = sysUser.getRelTenantIds(); 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); returnValue = SpringContextUtils.getHttpServletRequest().getHeader(CommonConstant.TENANT_ID);
} }
} }
@@ -210,9 +213,4 @@ public class JwtUtil {
if(returnValue!=null){returnValue = returnValue + moshi;} if(returnValue!=null){returnValue = returnValue + moshi;}
return returnValue; return returnValue;
} }
// public static void main(String[] args) {
// String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUzMzY1MTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.xjhud_tWCNYBOg_aRlMgOdlZoWFFKB_givNElHNw3X0";
// System.out.println(JwtUtil.getUsername(token));
// }
} }