All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.macrocloud.kernel.auth.utils.AuthUtil Maven / Gradle / Ivy

package org.macrocloud.kernel.auth.utils;

import io.jsonwebtoken.Claims;
import org.macrocloud.kernel.auth.MacroUser;
import org.macrocloud.kernel.launch.constant.TokenConstant;
import org.macrocloud.kernel.toolkit.constant.RoleConstant;
import org.macrocloud.kernel.toolkit.utils.Func;
import org.macrocloud.kernel.toolkit.utils.ObjectUtil;
import org.macrocloud.kernel.toolkit.utils.StringPool;
import org.macrocloud.kernel.toolkit.utils.StringUtil;
import org.macrocloud.kernel.toolkit.utils.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Auth工具类
 *
 */
public class AuthUtil {
    private static final String MACRO_USER_REQUEST_ATTR = "_MACRO_USER_REQUEST_ATTR_";

    private final static String HEADER = TokenConstant.HEADER;
    private final static String ACCOUNT = TokenConstant.ACCOUNT;
    private final static String USER_NAME = TokenConstant.USER_NAME;
    private final static String NICK_NAME = TokenConstant.NICK_NAME;
    private final static String AVATAR = TokenConstant.AVATAR;
    private final static String USER_ID = TokenConstant.USER_ID;
    private final static String DEPT_ID = TokenConstant.DEPT_ID;
    private final static String POST_ID = TokenConstant.POST_ID;
    private final static String ROLE_ID = TokenConstant.ROLE_ID;
    private final static String ROLE_NAME = TokenConstant.ROLE_NAME;
    private final static String TENANT_ID = TokenConstant.TENANT_ID;
    private final static String OAUTH_ID = TokenConstant.OAUTH_ID;
    private final static String CLIENT_ID = TokenConstant.CLIENT_ID;

    /**
     * 获取用户信息
     *
     * @return MacroUser
     */
    public static MacroUser getUser() {
        HttpServletRequest request = WebUtil.getRequest();
        if (request == null) {
            return null;
        }
        // 优先从 request 中获取
        Object MacroUser = request.getAttribute(MACRO_USER_REQUEST_ATTR);
        if (MacroUser == null) {
            MacroUser = getUser(request);
            if (MacroUser != null) {
                // 设置到 request 中
                request.setAttribute(MACRO_USER_REQUEST_ATTR, MacroUser);
            }
        }
        return (MacroUser) MacroUser;
    }

    /**
     * 获取用户信息
     *
     * @param claims claims
     * @return MacroUser
     */
    public static MacroUser getUser(Claims claims) {
        if (claims == null) {
            return null;
        }
        String clientId = Func.toStr(claims.get(AuthUtil.CLIENT_ID));
        String userId = Func.toStr(claims.get(AuthUtil.USER_ID));
        String tenantId = Func.toStr(claims.get(AuthUtil.TENANT_ID));
        String oauthId = Func.toStr(claims.get(AuthUtil.OAUTH_ID));
        String deptId = Func.toStrWithEmpty(claims.get(AuthUtil.DEPT_ID), StringPool.MINUS_ONE);
        String postId = Func.toStrWithEmpty(claims.get(AuthUtil.POST_ID), StringPool.MINUS_ONE);
        String roleId = Func.toStrWithEmpty(claims.get(AuthUtil.ROLE_ID), StringPool.MINUS_ONE);
        String account = Func.toStr(claims.get(AuthUtil.ACCOUNT));
        String roleName = Func.toStr(claims.get(AuthUtil.ROLE_NAME));
        String userName = Func.toStr(claims.get(TokenConstant.REAL_NAME));
        String nickName = Func.toStr(claims.get(AuthUtil.NICK_NAME));
        String avatar = Func.toStr(claims.get(AuthUtil.AVATAR));
        MacroUser macroUser = new MacroUser();
        macroUser.setClientId(clientId);
        macroUser.setUserId(userId);
        macroUser.setTenantId(tenantId);
        macroUser.setOauthId(oauthId);
        macroUser.setAccount(account);
        macroUser.setDeptId(deptId);
        macroUser.setPostId(postId);
        macroUser.setRoleId(roleId);
        macroUser.setRoleName(roleName);
        macroUser.setUserName(userName);
        macroUser.setRealName(userName);
        macroUser.setNickName(nickName);
        macroUser.setAvatar(avatar);
        return macroUser;
    }

    /**
     * 获取用户信息
     *
     * @param request request
     * @return MacroUser
     */
    public static MacroUser getUser(HttpServletRequest request) {
        Claims claims = getClaims(request);
        return getUser(claims);
    }

    /**
     * 是否为超管
     *
     * @return boolean
     */
    public static boolean isAdministrator() {
        return StringUtil.containsAny(getUserRole(), RoleConstant.ADMINISTRATOR);
    }
    /**
     * 是否为超管或者管理员
     *
     * @return boolean
     */
    public static boolean isAdmin() {
        return StringUtil.containsAny(getUserRole(), RoleConstant.ADMINISTRATOR,RoleConstant.ADMIN);
    }

    /**
     * 获取用户id
     *
     * @return userId
     */
    public static String getUserId() {
        MacroUser user = getUser();
        return (null == user) ? "-1" : user.getUserId();
    }

    /**
     * 获取用户id
     *
     * @param request request
     * @return userId
     */
    public static String getUserId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? "-1" : user.getUserId();
    }

    /**
     * 获取用户账号
     *
     * @return userAccount
     */
    public static String getUserAccount() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getAccount();
    }

    /**
     * 获取用户账号
     *
     * @param request request
     * @return userAccount
     */
    public static String getUserAccount(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getAccount();
    }

    /**
     * 获取用户名
     *
     * @return userName
     */
    public static String getUserName() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getUserName();
    }

    /**
     * 获取用户名
     *
     * @param request request
     * @return userName
     */
    public static String getUserName(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getUserName();
    }

    /**
     * 获取昵称
     *
     * @return userName
     */
    public static String getNickName() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getNickName();
    }

    /**
     * 获取昵称
     *
     * @param request request
     * @return userName
     */
    public static String getNickName(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getNickName();
    }

    /**
     * 获取用户部门
     *
     * @return userName
     */
    public static String getDeptId() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getDeptId();
    }

    /**
     * 获取用户部门
     *
     * @param request request
     * @return userName
     */
    public static String getDeptId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getDeptId();
    }

    /**
     * 获取用户岗位
     *
     * @return userName
     */
    public static String getPostId() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getPostId();
    }
    /**
     * 获取手机号
     *
     * @return tenantId
     */
    public static String getPhone() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getPhone();
    }

    /**
     * 获取用户岗位
     *
     * @param request request
     * @return userName
     */
    public static String getPostId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getPostId();
    }

    /**
     * 获取用户角色
     *
     * @return userName
     */
    public static String getUserRole() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getRoleName();
    }

    /**
     * 获取用角色
     *
     * @param request request
     * @return userName
     */
    public static String getUserRole(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getRoleName();
    }

    /**
     * 获取租户ID
     *
     * @return tenantId
     */
    public static String getTenantId() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getTenantId();
    }

    /**
     * 获取租户ID
     *
     * @param request request
     * @return tenantId
     */
    public static String getTenantId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getTenantId();
    }

    /**
     * 获取第三方认证ID
     *
     * @return tenantId
     */
    public static String getOauthId() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getOauthId();
    }

    /**
     * 获取第三方认证ID
     *
     * @param request request
     * @return tenantId
     */
    public static String getOauthId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getOauthId();
    }

    /**
     * 获取客户端id
     *
     * @return clientId
     */
    public static String getClientId() {
        MacroUser user = getUser();
        return (null == user) ? StringPool.EMPTY : user.getClientId();
    }

    /**
     * 获取客户端id
     *
     * @param request request
     * @return clientId
     */
    public static String getClientId(HttpServletRequest request) {
        MacroUser user = getUser(request);
        return (null == user) ? StringPool.EMPTY : user.getClientId();
    }

    public static String getToken() {
        HttpServletRequest request = WebUtil.getRequest();
        String auth = request.getHeader(AuthUtil.HEADER);
        String token;
        // 获取 Token 参数
        if (StringUtil.isNotBlank(auth)) {
            token = JwtUtil.getToken(auth);
        } else {
            String parameter = request.getParameter(AuthUtil.HEADER);
            token = JwtUtil.getToken(parameter);
        }
        return token;
    }

    /**
     * 获取Claims
     *
     * @param request request
     * @return Claims
     */
    public static Claims getClaims(HttpServletRequest request) {
        String auth = request.getHeader(AuthUtil.HEADER);
        Claims claims = null;
        String token;
        // 获取 Token 参数
        if (StringUtil.isNotBlank(auth)) {
            token = JwtUtil.getToken(auth);
        } else {
            String parameter = request.getParameter(AuthUtil.HEADER);
            token = JwtUtil.getToken(parameter);
        }
        // 获取 Token 值
        if (StringUtil.isNotBlank(token)) {
            claims = AuthUtil.parseJWT(token);
        }
        // 判断 Token 状态
        if (ObjectUtil.isNotEmpty(claims)) {
            String userId = Func.toStr(claims.get(AuthUtil.USER_ID));
            if (StringUtil.isBlank(userId)) {
                return null;
            }
        }
        return claims;
    }

    /**
     * 获取请求头
     *
     * @return header
     */
    public static String getHeader() {
        return getHeader(Objects.requireNonNull(WebUtil.getRequest()));
    }

    /**
     * 获取请求头
     *
     * @param request request
     * @return header
     */
    public static String getHeader(HttpServletRequest request) {
        return request.getHeader(HEADER);
    }

    /**
     * 解析jsonWebToken
     *
     * @param jsonWebToken jsonWebToken
     * @return Claims
     */
    public static Claims parseJWT(String jsonWebToken) {
        return JwtUtil.parseJWT(jsonWebToken);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy