Kaynağa Gözat

update security config

deason 6 yıl önce
ebeveyn
işleme
fd1903ece9

+ 75 - 9
examcloud-core-print-starter/src/main/java/cn/com/qmth/examcloud/core/print/config/ExamCloudResourceManager.java

@@ -1,27 +1,43 @@
 /*
  * *************************************************
  * Copyright (c) 2019 QMTH. All Rights Reserved.
- * Created by Deason on 2019-05-05 15:18:42.
+ * Created by Deason on 2019-05-07 11:02:35.
  * *************************************************
  */
 
 package cn.com.qmth.examcloud.core.print.config;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.AccessApp;
+import cn.com.qmth.examcloud.api.commons.security.bean.Role;
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.api.commons.security.bean.UserType;
+import cn.com.qmth.examcloud.api.commons.security.enums.RoleMeta;
+import cn.com.qmth.examcloud.commons.util.PathUtil;
+import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import cn.com.qmth.examcloud.commons.util.RegExpUtil;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.AppCacheBean;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.security.ResourceManager;
 import cn.com.qmth.examcloud.web.support.ApiInfo;
+import com.google.common.collect.Sets;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+import java.util.Set;
+
 @Component
 public class ExamCloudResourceManager implements ResourceManager {
     @Autowired
     private RedisClient redisClient;
 
+    static {
+        PropertiesUtil.configure(PathUtil.getResoucePath("security.properties"));
+    }
+
     @Override
     public AccessApp getAccessApp(Long appId) {
         AppCacheBean appCacheBean = CacheHelper.getApp(appId);
@@ -40,14 +56,12 @@ public class ExamCloudResourceManager implements ResourceManager {
             return true;
         }
 
-        if (null != apiInfo) {
-            Integer id = apiInfo.getId();
+        if (mapping.matches(".*swagger.*")) {
+            return true;
+        }
 
-            if (null != id) {
-                if (id.equals(103)) {
-                    return true;
-                }
-            } else {
+        if (null != apiInfo) {
+            if (apiInfo.isNaked()) {
                 return true;
             }
         }
@@ -57,7 +71,59 @@ public class ExamCloudResourceManager implements ResourceManager {
 
     @Override
     public boolean hasPermission(User user, ApiInfo apiInfo, String mapping) {
-        return true;
+
+        // 学生鉴权
+        if (user.getUserType().equals(UserType.STUDENT)) {
+            String key = "[s]" + mapping;
+            return PropertiesUtil.getBoolean(key, false);
+        }
+
+        List<Role> roleList = user.getRoleList();
+
+        if (CollectionUtils.isEmpty(roleList)) {
+            return false;
+        }
+
+        for (Role role : roleList) {
+            if (role.getRoleCode().equals(RoleMeta.SUPER_ADMIN.name())) {
+                return true;
+            }
+        }
+
+        // 权限组集合
+        String privilegeGroups = PropertiesUtil.getString(mapping);
+        if (StringUtils.isBlank(privilegeGroups)) {
+            return true;
+        }
+
+        // 用户权限集合
+        Set<String> rolePrivilegeList = Sets.newHashSet();
+        Long rootOrgId = user.getRootOrgId();
+        for (Role role : roleList) {
+            String key = "$_P_" + rootOrgId + "_" + role.getRoleId();
+            String rolePrivileges = redisClient.get(key, String.class);
+
+            List<String> rpList = RegExpUtil.findAll(rolePrivileges, "\\w+");
+            rolePrivilegeList.addAll(rpList);
+        }
+
+        List<String> privilegeGroupList = RegExpUtil.findAll(privilegeGroups, "[^\\;]+");
+
+        for (String pg : privilegeGroupList) {
+            pg = pg.trim();
+            if (StringUtils.isBlank(pg)) {
+                continue;
+            }
+
+            List<String> pList = RegExpUtil.findAll(pg, "[^\\,]+");
+            if (rolePrivilegeList.containsAll(pList)) {
+                return true;
+            } else {
+                continue;
+            }
+        }
+
+        return false;
     }
 
 }

+ 7 - 6
examcloud-core-print-starter/src/main/java/cn/com/qmth/examcloud/core/print/config/ExamCloudWebMvcConfigurer.java

@@ -1,7 +1,7 @@
 /*
  * *************************************************
  * Copyright (c) 2019 QMTH. All Rights Reserved.
- * Created by Deason on 2019-05-05 15:18:42.
+ * Created by Deason on 2019-05-07 11:02:35.
  * *************************************************
  */
 
@@ -27,12 +27,12 @@ public class ExamCloudWebMvcConfigurer implements WebMvcConfigurer {
 
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
-        String[] excludes = new String[]{"/", "/init", "/doc.html"};
-        registry.addInterceptor(new FirstInterceptor()).addPathPatterns("/api/**").excludePathPatterns(excludes);
-        registry.addInterceptor(new RpcInterceptor(resourceManager)).addPathPatterns("/api/**").excludePathPatterns(excludes);
+        String[] excludes = new String[]{"/", "/error", "/webjars/**", "/doc.html"};
+        registry.addInterceptor(new FirstInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
+        registry.addInterceptor(new RpcInterceptor(resourceManager)).addPathPatterns("/**").excludePathPatterns(excludes);
 
         RequestPermissionInterceptor permissionInterceptor = new RequestPermissionInterceptor(resourceManager, redisClient);
-        registry.addInterceptor(permissionInterceptor).addPathPatterns("/api/**").excludePathPatterns(excludes);
+        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**").excludePathPatterns(excludes);
     }
 
     @Override
@@ -40,8 +40,9 @@ public class ExamCloudWebMvcConfigurer implements WebMvcConfigurer {
         registry.addMapping("/**")
                 .allowedOrigins("*")
                 .allowCredentials(false)
-                .allowedMethods("POST")
+                .allowedMethods("*")
                 .maxAge(3600);
+
     }
 
 }

+ 2 - 2
examcloud-core-print-starter/src/main/resources/security-exclusions.conf

@@ -1,8 +1,8 @@
 regexp:.*swagger.*
 [][/doc.html][GET]
-[][/test][GET]
-[][/init][GET]
 [][/][GET]
+[][/init][GET]
+[][/test][GET]
 
 [${$rmp.ctrl.print}/common][/upload][POST]
 [${$rmp.ctrl.print}/common][/download][GET]

+ 0 - 0
examcloud-core-print-starter/src/main/resources/security-mapping.properties → examcloud-core-print-starter/src/main/resources/security.properties