RuoYi
2021-10-09 c20c2c221fd4af5266dd1286abca9ab13f01ddd5
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java
@@ -1,14 +1,9 @@
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.constant.SecurityConstants;
import com.ruoyi.common.core.text.Convert;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/**
 * 权限获取工具类
@@ -22,13 +17,8 @@
     */
    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(SecurityConstants.DETAILS_USERNAME);
        return ServletUtils.urlDecode(username);
    }
    /**
@@ -36,7 +26,7 @@
     */
    public static Long getUserId()
    {
        return Convert.toLong(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
        return Convert.toLong(ServletUtils.getRequest().getHeader(SecurityConstants.DETAILS_USER_ID));
    }
    /**
@@ -52,10 +42,18 @@
     */
    public static String getToken(HttpServletRequest request)
    {
        String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER);
        if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
        String token = request.getHeader(SecurityConstants.TOKEN_AUTHENTICATION);
        return replaceTokenPrefix(token);
    }
    /**
     * 替换token前缀
     */
    public static String replaceTokenPrefix(String token)
    {
        if (StringUtils.isNotEmpty(token) && token.startsWith(SecurityConstants.TOKEN_PREFIX))
        {
            token = token.replace(CacheConstants.TOKEN_PREFIX, "");
            token = token.replace(SecurityConstants.TOKEN_PREFIX, "");
        }
        return token;
    }