DokiYolo
2021-01-06 a95be9d41827d970a26d0c89b92780fab1f52e62
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java
@@ -1,8 +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;
/**
 * 权限获取工具类
@@ -16,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;
    }
    /**
@@ -28,6 +40,27 @@
    }
    /**
     * 获取请求token
     */
    public static String getToken()
    {
        return getToken(ServletUtils.getRequest());
    }
    /**
     * 根据request获取请求token
     */
    public static String getToken(HttpServletRequest request)
    {
        String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER);
        if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
        {
            token = token.replace(CacheConstants.TOKEN_PREFIX, "");
        }
        return token;
    }
    /**
     * 是否为管理员
     * 
     * @param userId 用户ID