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

This commit is contained in:
RuoYi
2021-01-06 12:00:41 +08:00
parent dee4653b9b
commit 4cc4e8a8fa
4 changed files with 47 additions and 31 deletions

View File

@@ -1,15 +1,10 @@
package com.ruoyi.common.core.utils;
import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.core.exception.BaseException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.ruoyi.common.core.constant.CacheConstants;
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()
{
String username = "";
try {
username = URLDecoder.decode(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new BaseException("获取username失败");
}
return username;
String username = ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME);
return ServletUtils.urlDecode(username);
}
/**

View File

@@ -1,6 +1,9 @@
package com.ruoyi.common.core.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.LinkedHashMap;
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.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.text.Convert;
/**
@@ -173,4 +177,40 @@ public class ServletUtils
}
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 "";
}
}
}