DokiYolo
2021-01-06 a95be9d41827d970a26d0c89b92780fab1f52e62
解决header获取username中文情况下乱码
2 files modified
29 ■■■■■ changed files
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java 13 ●●●●● patch | view | raw | blame | history
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java 16 ●●●●● patch | view | raw | blame | history
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java
@@ -1,9 +1,14 @@
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;
/**
 * 权限获取工具类
@@ -17,7 +22,13 @@
     */
    public static String getUsername()
    {
        return ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME);
        String username = "";
        try {
            username = URLDecoder.decode(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USERNAME), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new BaseException("获取username失败");
        }
        return username;
    }
    /**
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java
@@ -24,6 +24,8 @@
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
import reactor.core.publisher.Mono;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
 * 网关鉴权
@@ -68,7 +70,7 @@
        }
        JSONObject obj = JSONObject.parseObject(userStr);
        String userid = obj.getString("userid");
        String username = obj.getString("username");
        String username = urlEncode(obj.getString("username"));
        if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
        {
            return setUnauthorizedResponse(exchange, "令牌验证失败");
@@ -104,6 +106,18 @@
    }
    /**
     * 编码
     */
    private String urlEncode(String value) {
        try {
            value = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return value;
    }
    /**
     * 获取请求token
     */
    private String getToken(ServerHttpRequest request)