Explorar o código

解决header获取username中文情况下乱码

DokiYolo %!s(int64=5) %!d(string=hai) anos
pai
achega
a95be9d418

+ 12 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java

@@ -1,10 +1,15 @@
1 1
 package com.ruoyi.common.core.utils;
2 2
 
3 3
 import javax.servlet.http.HttpServletRequest;
4
+
5
+import com.ruoyi.common.core.exception.BaseException;
4 6
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
5 7
 import com.ruoyi.common.core.constant.CacheConstants;
6 8
 import com.ruoyi.common.core.text.Convert;
7 9
 
10
+import java.io.UnsupportedEncodingException;
11
+import java.net.URLDecoder;
12
+
8 13
 /**
9 14
  * 权限获取工具类
10 15
  * 
@@ -17,7 +22,13 @@ public class SecurityUtils
17 22
      */
18 23
     public static String getUsername()
19 24
     {
20
-        return ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME);
25
+        String username = "";
26
+        try {
27
+            username = URLDecoder.decode(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME), "UTF-8");
28
+        } catch (UnsupportedEncodingException e) {
29
+            throw new BaseException("获取username失败");
30
+        }
31
+        return username;
21 32
     }
22 33
 
23 34
     /**

+ 15 - 1
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java

@@ -24,6 +24,8 @@ import com.ruoyi.common.core.utils.StringUtils;
24 24
 import com.ruoyi.common.redis.service.RedisService;
25 25
 import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
26 26
 import reactor.core.publisher.Mono;
27
+import java.io.UnsupportedEncodingException;
28
+import java.net.URLEncoder;
27 29
 
28 30
 /**
29 31
  * 网关鉴权
@@ -68,7 +70,7 @@ public class AuthFilter implements GlobalFilter, Ordered
68 70
         }
69 71
         JSONObject obj = JSONObject.parseObject(userStr);
70 72
         String userid = obj.getString("userid");
71
-        String username = obj.getString("username");
73
+        String username = urlEncode(obj.getString("username"));
72 74
         if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
73 75
         {
74 76
             return setUnauthorizedResponse(exchange, "令牌验证失败");
@@ -104,6 +106,18 @@ public class AuthFilter implements GlobalFilter, Ordered
104 106
     }
105 107
 
106 108
     /**
109
+     * 编码
110
+     */
111
+    private String urlEncode(String value) {
112
+        try {
113
+            value = URLEncoder.encode(value, "UTF-8");
114
+        } catch (UnsupportedEncodingException e) {
115
+            e.printStackTrace();
116
+        }
117
+        return value;
118
+    }
119
+
120
+    /**
107 121
      * 获取请求token
108 122
      */
109 123
     private String getToken(ServerHttpRequest request)