jyy
2021-06-09 ec87cf1ee2b9382bf9d089711f0ff82ae8e7077c
zq-erp/src/main/java/com/matrix/system/app/authority/AppAuthorityManager.java
@@ -1,5 +1,10 @@
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;
@@ -25,13 +30,18 @@
 * @email 935090232@qq.com
 * @date 2017年12月6日
 */
@Component("defaultAuthorityManager")
@Component
public class AppAuthorityManager implements AuthorityManager {
    private static final int DEFAULT_2 = 2;
    @Autowired
    RedisClient redisClient;
    @Autowired
    SysFunctionService sysFunctionService;
    public static final String USER_POWER_REDISKEY_APP = "USER_POWER_APP";
    public static final String USERFUNCTION = "userFunction";
    /** 用户所有路径权限的记录 **/
@@ -91,8 +101,32 @@
        List<String> userUrlMapping = new ArrayList<>();
        // 获取用户所有权限
        getUserFunction(user,userFunction, userUrlMapping);
        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));
            });
            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));
            }
        } else {
            // 获取用户所有权限
            getUserFunction(user,userFunction, userUrlMapping);
            Map<String ,Object> powerMap=new HashMap<>();
            powerMap.put(USERFUNCTION, userFunction);
            powerMap.put(USER_URL_MAPPING, userUrlMapping);
            redisClient.saveValue(redisKey,JSONUtil.parseObj(powerMap,true));
        }
        // TODO 这里的用户权限应该放到redis缓存中,在拦截器中做权限拦截