Browse Source

代码格式

wanggaokun 1 năm trước cách đây
mục cha
commit
1dce1b8316

+ 4 - 8
km-common/km-common-security/src/main/java/com/km/common/security/config/SecurityConfig.java

@@ -36,16 +36,12 @@ public class SecurityConfig implements WebMvcConfigurer {
         registry.addInterceptor(new SaInterceptor(handler -> {
                 AllUrlHandler allUrlHandler = SpringUtils.getBean(AllUrlHandler.class);
                 // 登录验证 -- 排除多个路径
-                SaRouter
+                    // 检查是否登录 是否有token
+                    //TODO :以后完善多平台登录校验clientID功能
+                    SaRouter
                     // 获取所有的
                     .match(allUrlHandler.getUrls())  // 拦截的 path 列表
-                    .check(() -> {
-                        // 检查是否登录 是否有token
-                        StpUtil.checkLogin();
-
-                        //TODO :以后完善多平台登录校验clientID功能
-
-                    });
+                    .check(StpUtil::checkLogin);
             })).addPathPatterns("/**")
             // 排除不需要拦截的路径
             .excludePathPatterns(securityProperties.getExcludes());

+ 6 - 4
km-common/km-common-security/src/main/java/com/km/common/security/core/service/SaPermissionImpl.java

@@ -24,9 +24,10 @@ public class SaPermissionImpl implements StpInterface {
         UserType userType = UserType.getUserType(loginUser.getUserType());
         if (userType == UserType.SYS_USER) {
             return new ArrayList<>(loginUser.getMenuPermission());
-        } else if (userType == UserType.APP_USER) {
-            // 其他端 自行根据业务编写
         }
+        /* else if (userType == UserType.APP_USER) {
+            其他端 自行根据业务编写
+        }*/
         return new ArrayList<>();
     }
 
@@ -39,9 +40,10 @@ public class SaPermissionImpl implements StpInterface {
         UserType userType = UserType.getUserType(loginUser.getUserType());
         if (userType == UserType.SYS_USER) {
             return new ArrayList<>(loginUser.getRolePermission());
-        } else if (userType == UserType.APP_USER) {
-            // 其他端 自行根据业务编写
         }
+        /*else if (userType == UserType.APP_USER) {
+            其他端 自行根据业务编写
+        }*/
         return new ArrayList<>();
     }
 }

+ 2 - 1
km-common/km-common-security/src/main/java/com/km/common/security/handler/AllUrlHandler.java

@@ -24,7 +24,7 @@ import java.util.regex.Pattern;
 @Data
 public class AllUrlHandler implements InitializingBean {
 
-    private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}");
+    private static final Pattern PATTERN = Pattern.compile("\\{(.*?)}");
 
     private List<String> urls = new ArrayList<>();
 
@@ -35,6 +35,7 @@ public class AllUrlHandler implements InitializingBean {
         Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
         map.keySet().forEach(info -> {
             // 获取注解上边的 path 替代 path variable 为 *
+            assert info.getPathPatternsCondition() != null;
             Objects.requireNonNull(info.getPathPatternsCondition().getPatterns())
                 .forEach(url -> set.add(ReUtil.replaceAll(url.getPatternString(), PATTERN, "*")));
         });

+ 20 - 18
km-common/km-common-security/src/main/java/com/km/common/security/handler/GlobalExceptionHandler.java

@@ -22,6 +22,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 
+import java.util.Objects;
+
 /**
  * 全局异常处理器
  *
@@ -36,8 +38,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(NotPermissionException.class)
     public CommonResult<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',权限码校验失败'{}'", requestUri, e.getMessage());
         return CommonResult.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
     }
 
@@ -46,8 +48,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(NotRoleException.class)
     public CommonResult<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',角色权限校验失败'{}'", requestUri, e.getMessage());
         return CommonResult.fail(HttpStatus.HTTP_FORBIDDEN, "没有访问权限,请联系管理员授权");
     }
 
@@ -56,8 +58,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(NotLoginException.class)
     public CommonResult<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestUri, e.getMessage());
         return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
     }
 
@@ -67,8 +69,8 @@ public class GlobalExceptionHandler {
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
     public CommonResult<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
                                                                   HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',不支持'{}'请求", requestUri, e.getMethod());
         return CommonResult.fail(e.getMessage());
     }
 
@@ -96,8 +98,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(MissingPathVariableException.class)
     public CommonResult<Void> handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI);
+        String requestUri = request.getRequestURI();
+        log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestUri);
         return CommonResult.fail(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
     }
 
@@ -106,9 +108,9 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
     public CommonResult<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI);
-        return CommonResult.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue()));
+        String requestUri = request.getRequestURI();
+        log.error("请求参数类型不匹配'{}',发生系统异常.", requestUri);
+        return CommonResult.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), Objects.requireNonNull(e.getRequiredType()).getName(), e.getValue()));
     }
 
     /**
@@ -116,8 +118,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(RuntimeException.class)
     public CommonResult<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',发生未知异常.", requestURI, e);
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',发生未知异常.", requestUri, e);
         return CommonResult.fail(e.getMessage());
     }
 
@@ -126,8 +128,8 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(Exception.class)
     public CommonResult<Void> handleException(Exception e, HttpServletRequest request) {
-        String requestURI = request.getRequestURI();
-        log.error("请求地址'{}',发生系统异常.", requestURI, e);
+        String requestUri = request.getRequestURI();
+        log.error("请求地址'{}',发生系统异常.", requestUri, e);
         return CommonResult.fail(e.getMessage());
     }
 
@@ -157,7 +159,7 @@ public class GlobalExceptionHandler {
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public CommonResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
         log.error(e.getMessage());
-        String message = e.getBindingResult().getFieldError().getDefaultMessage();
+        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
         return CommonResult.fail(message);
     }
 }

+ 4 - 3
km-common/km-common-security/src/main/java/com/km/common/security/listener/UserActionListener.java

@@ -56,9 +56,10 @@ public class UserActionListener implements SaTokenListener {
                 RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
             }
             log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
-        } else if (userType == UserType.APP_USER) {
-            // app端 自行根据业务编写
         }
+        /*else if (userType == UserType.APP_USER) {
+            app端 自行根据业务编写
+        }*/
     }
 
     /**
@@ -76,7 +77,7 @@ public class UserActionListener implements SaTokenListener {
     @Override
     public void doKickout(String loginType, Object loginId, String tokenValue) {
         RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
-        log.info("user doKickout, userId:{}, token:{}", loginId, tokenValue);
+        log.info("user doKick out, userId:{}, token:{}", loginId, tokenValue);
     }
 
     /**

+ 1 - 1
km-common/km-common-security/src/main/java/com/km/common/security/utils/LoginHelper.java

@@ -36,7 +36,7 @@ public class LoginHelper {
     public static final String TENANT_KEY = "tenantId";
     public static final String USER_KEY = "userId";
     public static final String DEPT_KEY = "deptId";
-    public static final String CLIENT_KEY = "clientid";
+    public static final String CLIENT_KEY = "clientId";
     public static final String TENANT_ADMIN_KEY = "isTenantAdmin";
 
     /**