Browse Source

编码解码用户名,防止中文出现乱码

tags/v2.5.0
RuoYi 4 years ago
parent
commit
4cc4e8a8fa
  1. 14
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java
  2. 40
      ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java
  3. 5
      ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
  4. 19
      ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java

14
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java

@ -1,15 +1,10 @@
package com.ruoyi.common.core.utils; package com.ruoyi.common.core.utils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.core.exception.BaseException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.core.text.Convert;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/** /**
* 权限获取工具类 * 权限获取工具类
* *
@ -22,13 +17,8 @@ public class SecurityUtils
*/ */
public static String getUsername() public static String getUsername()
{ {
String username = ""; String username = ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME);
try { return ServletUtils.urlDecode(username);
username = URLDecoder.decode(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new BaseException("获取username失败");
}
return username;
} }
/** /**

40
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java

@ -1,6 +1,9 @@
package com.ruoyi.common.core.utils; package com.ruoyi.common.core.utils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -10,6 +13,7 @@ import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.core.text.Convert;
/** /**
@ -173,4 +177,40 @@ public class ServletUtils
} }
return false; return false;
} }
/**
* 内容编码
*
* @param str 内容
* @return 编码后的内容
*/
public static String urlEncode(String str)
{
try
{
return URLEncoder.encode(str, Constants.UTF8);
}
catch (UnsupportedEncodingException e)
{
return "";
}
}
/**
* 内容解码
*
* @param str 内容
* @return 解码后的内容
*/
public static String urlDecode(String str)
{
try
{
return URLDecoder.decode(str, Constants.UTF8);
}
catch (UnsupportedEncodingException e)
{
return "";
}
}
} }

5
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java

@ -20,7 +20,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.ServletUtils; import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.ip.IpUtils; import com.ruoyi.common.core.utils.ip.IpUtils;
@ -93,8 +93,7 @@ public class LogAspect
operLog.setJsonResult(JSON.toJSONString(jsonResult)); operLog.setJsonResult(JSON.toJSONString(jsonResult));
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
HttpServletRequest request = ServletUtils.getRequest(); String username = SecurityUtils.getUsername();
String username = request.getHeader(CacheConstants.DETAILS_USERNAME);
if (StringUtils.isNotBlank(username)) if (StringUtils.isNotBlank(username))
{ {
operLog.setOperName(username); operLog.setOperName(username);

19
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java

@ -20,12 +20,11 @@ import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties; import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/** /**
* 网关鉴权 * 网关鉴权
@ -70,7 +69,7 @@ public class AuthFilter implements GlobalFilter, Ordered
} }
JSONObject obj = JSONObject.parseObject(userStr); JSONObject obj = JSONObject.parseObject(userStr);
String userid = obj.getString("userid"); String userid = obj.getString("userid");
String username = urlEncode(obj.getString("username")); String username = obj.getString("username");
if (StringUtils.isBlank(userid) || StringUtils.isBlank(username)) if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
{ {
return setUnauthorizedResponse(exchange, "令牌验证失败"); return setUnauthorizedResponse(exchange, "令牌验证失败");
@ -80,7 +79,7 @@ public class AuthFilter implements GlobalFilter, Ordered
redisService.expire(getTokenKey(token), EXPIRE_TIME); redisService.expire(getTokenKey(token), EXPIRE_TIME);
// 设置用户信息到请求 // 设置用户信息到请求
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid) ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
.header(CacheConstants.DETAILS_USERNAME, username).build(); .header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build(); ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
return chain.filter(mutableExchange); return chain.filter(mutableExchange);
@ -106,18 +105,6 @@ public class AuthFilter implements GlobalFilter, Ordered
} }
/** /**
* 编码
*/
private String urlEncode(String value) {
try {
value = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
/**
* 获取请求token * 获取请求token
*/ */
private String getToken(ServerHttpRequest request) private String getToken(ServerHttpRequest request)

Loading…
Cancel
Save