935090232@qq.com
2022-02-22 bbe0a2fd03063316e50cf141986bda984599bbda
zq-erp/src/main/java/com/matrix/system/app/authority/AppAuthorityManager.java
@@ -1,10 +1,6 @@
package com.matrix.system.app.authority;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.matrix.component.redis.RedisClient;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.StringUtils;
@@ -15,6 +11,7 @@
import com.matrix.system.common.bean.SysFunction;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.init.LocalCache;
import com.matrix.system.common.service.SysFunctionService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,8 +33,6 @@
    private static final int DEFAULT_2 = 2;
    @Autowired
    RedisClient redisClient;
    @Autowired
    SysFunctionService sysFunctionService;
@@ -103,22 +98,13 @@
        String redisKey = USER_POWER_REDISKEY_APP + SecureUtil.md5(user.getSuId()+"");
        String cachedValue = redisClient.getCachedValue(redisKey);
        if (StringUtils.isNotBlank(cachedValue)) {
            //从缓存中获取用户权限
            JSONObject powerMap = JSONUtil.parseObj(cachedValue);
            String userFunctionMapStr = powerMap.get(USERFUNCTION).toString();
            JSONObject userFunctionMap = JSONUtil.parseObj(userFunctionMapStr);
            Set<String> userFunctionMapKeys = userFunctionMap.keySet();
            userFunctionMapKeys.forEach(key -> {
                userFunction.put(key, userFunctionMap.get(key, SysFunction.class));
            });
        Map<String, Object> cachePowerMap = LocalCache.get(redisKey);
        if (Objects.nonNull(cachePowerMap)) {
            String userUrlMappingListStr = powerMap.get(USER_URL_MAPPING).toString();
            JSONArray userUrlMappingArray = JSONUtil.parseArray(userUrlMappingListStr);
            for (int i = 0; i < userUrlMappingArray.size(); i++) {
                userUrlMapping.add(userUrlMappingArray.get(i, String.class));
            }
            userFunction = (Map<String, SysFunction>) cachePowerMap.get(USERFUNCTION);
            userUrlMapping = (List<String>) cachePowerMap.get(USER_URL_MAPPING);
        } else {
            // 获取用户所有权限
            getUserFunction(user,userFunction, userUrlMapping);
@@ -126,11 +112,9 @@
            Map<String ,Object> powerMap=new HashMap<>();
            powerMap.put(USERFUNCTION, userFunction);
            powerMap.put(USER_URL_MAPPING, userUrlMapping);
            redisClient.saveValue(redisKey,JSONUtil.parseObj(powerMap,true));
            LocalCache.save(redisKey,powerMap);
        }
        // TODO 这里的用户权限应该放到redis缓存中,在拦截器中做权限拦截
        WebUtil.setSessionAttribute(USERFUNCTION, userFunction);
        WebUtil.setSessionAttribute(USER_URL_MAPPING, userUrlMapping);
        result.putInMap(USERFUNCTION, userFunction);