Pu Zhibing
3 天以前 5dacdee9b54c78372b68140e2b068d03a620eab9
ManagementQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/core/beetl/ShiroExtUtil.java
@@ -15,29 +15,51 @@
 */
package com.stylefeng.guns.core.beetl;
import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.modular.system.warpper.LoginUser;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
@Component
public class ShiroExtUtil {
    private static final String NAMES_DELIMETER = ",";
    private final String NAMES_DELIMETER = ",";
    
    public static Map<String, ShiroUser> map = new HashMap<>();
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 获取当前 Subject
     * 验证当前用户是否属于以下任意一个角色。
     *
     * @return Subject
     * @param roleNames 角色列表
     * @return 属于:true,否则false
     */
    protected static Subject getSubject() {
        return SecurityUtils.getSubject();
    public boolean hasAnyRoles(String roleNames) {
        boolean hasAnyRole = false;
        ShiroUser user = getUser();
        if (user != null && roleNames != null && roleNames.length() > 0) {
            List<Integer> roleList = user.getRoleList();
            for (String role : roleNames.split(NAMES_DELIMETER)) {
                if (roleList.contains(role.trim())) {
                    hasAnyRole = true;
                    break;
                }
            }
        }
        return hasAnyRole;
    }
    /**
@@ -45,13 +67,34 @@
     *
     * @return ShiroUser
     */
    public static ShiroUser getUser() {
        String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();
        ShiroUser shiroUser = map.get(sessionId);
        if(null == shiroUser){
    public ShiroUser getUser() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs != null) {
            HttpServletRequest request = attrs.getRequest();
            HttpSession session = request.getSession();
            String onconParam = edu.yale.its.tp.cas.client.Util.getOnconParam(session);
            try {
                onconParam = new String(Base64.decodeBase64(onconParam), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            LoginUser loginUser = JSON.parseObject(onconParam, LoginUser.class);
            System.out.println("当前登录用户:" + JSON.toJSONString(loginUser));
            String shiroUser = redisTemplate.opsForValue().get(loginUser.getOnconUUID());
            System.out.println("当前登录用户缓存数据:" + shiroUser);
            return JSON.parseObject(shiroUser, ShiroUser.class);
        }
            throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
        }
        return shiroUser;
    /**
     * 与hasRole标签逻辑相反,当用户不属于该角色时验证通过。
     *
     * @param roleName 角色名
     * @return 不属于该角色:true,否则false
     */
    public boolean lacksRole(String roleName) {
        return !hasRole(roleName);
    }
    /**
@@ -66,34 +109,12 @@
    }
    /**
     * 与hasRole标签逻辑相反,当用户不属于该角色时验证通过。
     * 获取当前 Subject
     *
     * @param roleName 角色名
     * @return 不属于该角色:true,否则false
     * @return Subject
     */
    public boolean lacksRole(String roleName) {
        return !hasRole(roleName);
    }
    /**
     * 验证当前用户是否属于以下任意一个角色。
     *
     * @param roleNames 角色列表
     * @return 属于:true,否则false
     */
    public static boolean hasAnyRoles(String roleNames) {
        boolean hasAnyRole = false;
        ShiroUser user = getUser();
        if (user != null && roleNames != null && roleNames.length() > 0) {
            List<Integer> roleList = user.getRoleList();
            for (String role : roleNames.split(NAMES_DELIMETER)) {
                if (roleList.contains(role.trim())) {
                    hasAnyRole = true;
                    break;
                }
            }
        }
        return hasAnyRole;
    protected Subject getSubject() {
        return SecurityUtils.getSubject();
    }
    /**
@@ -122,7 +143,7 @@
     * @param permission 权限名
     * @return 拥有权限:true,否则false
     */
    public static boolean hasPermission(String permission) {
    public boolean hasPermission(String permission) {
        ShiroUser user = getUser();
        if(null == user){
            return false;
@@ -191,7 +212,6 @@
        }
        return "";
    }
    
    
}