浏览代码

权限完成

wuheng 1 年之前
父节点
当前提交
5d0921b492

+ 2 - 0
controller/src/main/resources/application-wheng.yaml

@@ -30,3 +30,5 @@ logging:
     com.koobietech.eas.*: TRACE
 project:
   path: ${user.home}/archivesFiles
+authority:
+  path: ${user.home}/authorityFile/authority.json

+ 3 - 1
controller/src/main/resources/application.yaml

@@ -56,4 +56,6 @@ eas:
     easgroup:
       - /login/getUserInfo
     admin:
-      - /**
+      - /**
+authority:
+  path: ${user.home}/authorityFile/authority.json

+ 17 - 0
security/src/main/java/com/koobietech/eas/security/config/SecurityConfig.java

@@ -1,5 +1,7 @@
 package com.koobietech.eas.security.config;
 
+import com.koobietech.eas.security.exception.EasAuthenticationEntryPoint;
+import com.koobietech.eas.security.exception.EasAuthenticationFailureHandler;
 import com.koobietech.eas.security.filter.EasSecurityFilter;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -44,12 +46,27 @@ public class SecurityConfig {
                 .disable()
                 .sessionManagement()
                 .sessionCreationPolicy(SessionCreationPolicy.NEVER)
+                .and()
+                //处理异常信息
+                .exceptionHandling()
+                .accessDeniedHandler(restfulAccessDeniedHandler())
+                .authenticationEntryPoint(restAuthenticationEntryPoint())
                 .and();
         and.addFilterBefore( easSecurityFilter, UsernamePasswordAuthenticationFilter.class);
 
         return and.build();
     }
 
+    @Bean
+    public EasAuthenticationFailureHandler restfulAccessDeniedHandler() {
+        return new EasAuthenticationFailureHandler();
+    }
+
+    @Bean
+    public EasAuthenticationEntryPoint restAuthenticationEntryPoint() {
+        return new EasAuthenticationEntryPoint();
+    }
+
     @Bean
     public PasswordEncoder passwordEncoder() {
         return new BCryptPasswordEncoder();

+ 26 - 0
security/src/main/java/com/koobietech/eas/security/exception/EasAuthenticationEntryPoint.java

@@ -0,0 +1,26 @@
+package com.koobietech.eas.security.exception;
+
+import cn.hutool.json.JSONUtil;
+import com.koobietech.eas.common.result.JsonResult;
+import org.springframework.security.core.AuthenticationException;
+import org.springframework.security.web.AuthenticationEntryPoint;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author lc
+ */
+public class EasAuthenticationEntryPoint implements AuthenticationEntryPoint {
+    @Override
+    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
+        response.setHeader("Access-Control-Allow-Origin", "*");
+        response.setHeader("Cache-Control","no-cache");
+        response.setCharacterEncoding("UTF-8");
+        response.setContentType("application/json");
+        response.getWriter().println(JSONUtil.parse(JsonResult.fail(authException.getMessage(), 403)));
+        response.getWriter().flush();
+    }
+}

+ 28 - 0
security/src/main/java/com/koobietech/eas/security/exception/EasAuthenticationFailureHandler.java

@@ -0,0 +1,28 @@
+package com.koobietech.eas.security.exception;
+
+import cn.hutool.json.JSONUtil;
+import com.koobietech.eas.common.result.JsonResult;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.security.web.access.AccessDeniedHandler;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author lc
+ */
+public class EasAuthenticationFailureHandler implements AccessDeniedHandler {
+    @Override
+    public void handle(HttpServletRequest request,
+                       HttpServletResponse response,
+                       AccessDeniedException e) throws IOException, ServletException {
+        response.setHeader("Access-Control-Allow-Origin", "*");
+        response.setHeader("Cache-Control","no-cache");
+        response.setCharacterEncoding("UTF-8");
+        response.setContentType("application/json");
+        response.getWriter().println(JSONUtil.parse(JsonResult.fail(e.getMessage(), 403)));
+        response.getWriter().flush();
+    }
+}

+ 5 - 0
service/pom.xml

@@ -8,6 +8,11 @@
     <description>service</description>
 
     <dependencies>
+
+        <dependency>
+            <groupId>com.alibaba.fastjson2</groupId>
+            <artifactId>fastjson2</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>

+ 0 - 4
service/src/main/java/com/koobietech/eas/service/config/EasAuthorityConfig.java

@@ -1,8 +1,6 @@
 package com.koobietech.eas.service.config;
 
 import lombok.Data;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -18,8 +16,6 @@ import java.util.List;
  *
  */
 @Data
-@Component
-@ConfigurationProperties(prefix = "eas.authority")
 public class EasAuthorityConfig {
     private List<String> authorization = new ArrayList<>();
     private List<String> student = new ArrayList<>();

+ 25 - 2
service/src/main/java/com/koobietech/eas/service/impl/EasSysAdminLoginServiceImpl.java

@@ -1,9 +1,11 @@
 package com.koobietech.eas.service.impl;
 
+import com.alibaba.fastjson2.JSON;
 import com.anji.captcha.model.common.ResponseModel;
 import com.koobietech.eas.common.exception.EasException;
 import com.koobietech.eas.common.pojo.JwtUserPojo;
 import com.koobietech.eas.common.result.JsonResult;
+import com.koobietech.eas.common.utils.FileManager;
 import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import com.koobietech.eas.dao.constant.UserType;
@@ -18,10 +20,15 @@ import com.koobietech.eas.mbg.model.EasSysUserinfoExample;
 import com.koobietech.eas.service.EasSysAdminLoginService;
 import com.koobietech.eas.service.EasSysUserLoginRedisService;
 import com.koobietech.eas.service.config.EasAuthorityConfig;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.List;
@@ -41,9 +48,12 @@ public class EasSysAdminLoginServiceImpl implements EasSysAdminLoginService {
     @Resource
     private EasSysUserLoginRedisService loginRedisService;
     @Resource
-    private EasAuthorityConfig  easAuthorityConfig;
-    @Resource
     private JwtManager jwtManager;
+    @Resource
+    FileManager fileManager;
+
+    @Value("${authority.path}")
+    String jsonFilePath;
 
     // token过期时间 单位:s
     private final Integer token_expires = 24 * 60 * 60;
@@ -66,6 +76,19 @@ public class EasSysAdminLoginServiceImpl implements EasSysAdminLoginService {
 
         userDetailPojoInRedis.setDepartments(adminLoginMapper.getUserDepartmentsById(id));
         List<DepartmentPojo> departments = userDetailPojoInRedis.getDepartments();
+
+        //权限配置文件
+        String absolutePath = new File(jsonFilePath).getAbsolutePath();
+        FileInputStream archiveFile = fileManager.getArchiveFile(absolutePath);
+        String jsonStr = "";
+        try {
+            jsonStr = IOUtils.toString(archiveFile, "UTF-8");
+        } catch (IOException ignored) {}
+
+        EasAuthorityConfig easAuthorityConfig = JSON.parseObject(jsonStr, EasAuthorityConfig.class);
+        if (easAuthorityConfig == null) {
+            throw new EasException("权限配置文件不存在", 412);
+        }
         departments.forEach(departmentPojo -> {
             try {
                 String authority = departmentPojo.getAuthority();

+ 26 - 2
service/src/main/java/com/koobietech/eas/service/impl/EasSysStudentLoginServiceImpl.java

@@ -1,7 +1,9 @@
 package com.koobietech.eas.service.impl;
 
+import com.alibaba.fastjson2.JSON;
 import com.koobietech.eas.common.exception.EasException;
 import com.koobietech.eas.common.pojo.JwtUserPojo;
+import com.koobietech.eas.common.utils.FileManager;
 import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import com.koobietech.eas.dao.constant.UserType;
@@ -16,10 +18,15 @@ import com.koobietech.eas.mbg.model.EasSysStudentExample;
 import com.koobietech.eas.service.EasSysUserLoginRedisService;
 import com.koobietech.eas.service.EasSysStudentLoginService;
 import com.koobietech.eas.service.config.EasAuthorityConfig;
+import org.apache.commons.io.IOUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.List;
@@ -48,11 +55,14 @@ public class EasSysStudentLoginServiceImpl implements EasSysStudentLoginService
     EasSysUserLoginRedisService studentLoginRedisService;
 
     @Resource
-    private EasAuthorityConfig easAuthorityConfig;
+    FileManager fileManager;
 
     @Resource
     JwtManager jwtManager;
 
+    @Value("${authority.path}")
+    String jsonFilePath;
+
     // token过期时间 单位:s
     private final Integer token_expires = 24 * 60 * 60;
 
@@ -71,8 +81,22 @@ public class EasSysStudentLoginServiceImpl implements EasSysStudentLoginService
         //先调用自定义sql查询detail类中的数据 最后把这个类封装到redis里面
         UserDetailPojo userDetailPojo = adminLoginMapper.getStudentDetailById(adminId);
         userDetailPojo.setDepartments(adminLoginMapper.getStudentDepartmentsById(adminId));
-        //TODO
+
         List<DepartmentPojo> departments = userDetailPojo.getDepartments();
+
+        //权限配置文件
+        String absolutePath = new File(jsonFilePath).getAbsolutePath();
+        FileInputStream archiveFile = fileManager.getArchiveFile(absolutePath);
+        String jsonStr = "";
+        try {
+            jsonStr = IOUtils.toString(archiveFile, "UTF-8");
+        } catch (IOException ignored) {}
+
+        EasAuthorityConfig easAuthorityConfig = JSON.parseObject(jsonStr, EasAuthorityConfig.class);
+        if (easAuthorityConfig == null) {
+            throw new EasException("权限配置文件不存在", 412);
+        }
+
         departments.forEach(departmentPojo -> {
             try {
                 String authority = departmentPojo.getAuthority();