From feb6fb304b64ae6dc4071a64df733082f61f4abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8A=9B?= Date: Fri, 11 Mar 2022 17:32:51 +0800 Subject: [PATCH] fix bug --- .../canvas/web/exception/CustomException.java | 33 ---------- .../web/exception/GlobalExceptionHandler.java | 20 +++++++ .../web/exception/user/CaptchaException.java | 14 ----- .../web/exception/user/UserException.java | 14 ----- .../user/UserNotExistsException.java | 12 ---- .../java/com/canvas/web/utils/Response.java | 60 +++++++++++++------ .../security/config/SpringSecurityConfig.java | 4 +- .../system/controller/UserController.java | 3 + 8 files changed, 68 insertions(+), 92 deletions(-) delete mode 100644 common/src/main/java/com/canvas/web/exception/CustomException.java create mode 100644 common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java delete mode 100644 common/src/main/java/com/canvas/web/exception/user/CaptchaException.java delete mode 100644 common/src/main/java/com/canvas/web/exception/user/UserException.java delete mode 100644 common/src/main/java/com/canvas/web/exception/user/UserNotExistsException.java diff --git a/common/src/main/java/com/canvas/web/exception/CustomException.java b/common/src/main/java/com/canvas/web/exception/CustomException.java deleted file mode 100644 index 6ccd464..0000000 --- a/common/src/main/java/com/canvas/web/exception/CustomException.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.canvas.web.exception; - - -//自定义异常类 -public class CustomException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - private Integer code; - - private String message; - - public CustomException(String message) { this.message=message;} - - public CustomException(String message ,Integer code){ - this.message=message; - this.code=code; - } - - public CustomException(String message,Throwable e){ - super(message,e); - this.message=message; - } - - @Override - public String getMessage() { - return message; - } - - public Integer getCode() { - return code; - } -} diff --git a/common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java b/common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..f1749fb --- /dev/null +++ b/common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java @@ -0,0 +1,20 @@ +package com.canvas.web.exception; + +import com.canvas.web.utils.Response; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +@ControllerAdvice +public class GlobalExceptionHandler { + private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + + +// public Response baseExceptionHandler(HttpServletRequest req,BaseException e){ +// logger.error("发生业务异常!原因是:{}",e.getDefaultMessage()); +// return Response.error(e.getCode(),e.getDefaultMessage()); +// } +} diff --git a/common/src/main/java/com/canvas/web/exception/user/CaptchaException.java b/common/src/main/java/com/canvas/web/exception/user/CaptchaException.java deleted file mode 100644 index f7aa45a..0000000 --- a/common/src/main/java/com/canvas/web/exception/user/CaptchaException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.canvas.web.exception.user; - - -import com.canvas.web.exception.BaseException; - -//验证码处理异常 -public class CaptchaException extends UserException { - - private static final long seriaVersionUID = 1L; - - public CaptchaException() { - super("user.jcaptcha.error", null); - } -} diff --git a/common/src/main/java/com/canvas/web/exception/user/UserException.java b/common/src/main/java/com/canvas/web/exception/user/UserException.java deleted file mode 100644 index 321b972..0000000 --- a/common/src/main/java/com/canvas/web/exception/user/UserException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.canvas.web.exception.user; - - -import com.canvas.web.exception.BaseException; - -//用户处理异常 -public class UserException extends BaseException { - - private static final long serialVersionUID = 1L; - - public UserException(String code, Object[] args) { - super("user", code, args, null); - } -} diff --git a/common/src/main/java/com/canvas/web/exception/user/UserNotExistsException.java b/common/src/main/java/com/canvas/web/exception/user/UserNotExistsException.java deleted file mode 100644 index 8f7a6f1..0000000 --- a/common/src/main/java/com/canvas/web/exception/user/UserNotExistsException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.canvas.web.exception.user; - - -//用户不存在异常 -public class UserNotExistsException extends UserException { - - private static final long seriaVersionUID = 1L; - - public UserNotExistsException() { - super("user.not.exits",null); - } -} diff --git a/common/src/main/java/com/canvas/web/utils/Response.java b/common/src/main/java/com/canvas/web/utils/Response.java index 323ca41..acab81e 100644 --- a/common/src/main/java/com/canvas/web/utils/Response.java +++ b/common/src/main/java/com/canvas/web/utils/Response.java @@ -18,39 +18,63 @@ public class Response { private long timestamp; + public Response(){ + this.timestamp=System.currentTimeMillis(); + } - private Response(T data){ - this.code= ResponseEnum.SUCCESS.getCode(); - this.msg=ResponseEnum.SUCCESS.getMessage(); - this.timestamp=System.currentTimeMillis(); - this.data=data; + + public Response(T data) { + this.code = ResponseEnum.SUCCESS.getCode(); + this.msg = ResponseEnum.SUCCESS.getMessage(); + this.timestamp = System.currentTimeMillis(); + this.data = data; } - private Response(ResponseEnum responseEnum){ - if (null==responseEnum){ + + + public Response(ResponseEnum responseEnum) { + if (null == responseEnum) { return; } - this.code= responseEnum.getCode(); - this.msg= responseEnum.getMessage(); + this.code = responseEnum.getCode(); + this.msg = responseEnum.getMessage(); } //成功时调用 public static Response success() { return success(null); } + + + //成功时调用 public static Response success(T data) { - return new Response(data); + Response response=new Response<>(); + response.setCode(ResponseEnum.SUCCESS.getCode()); + response.setMsg(ResponseEnum.SUCCESS.getMessage()); + response.setData(data); + return response; } - public static Response error(){ + public static Response error() { return error(ResponseEnum.ERROR); } - public static Response error(ResponseEnum responseEnum){ + public static Response error(ResponseEnum responseEnum) { return new Response(responseEnum); } + public static Response error(Integer code,String message){ + Response response=new Response<>(); + response.setCode(code); + response.setMsg(message); + return response; + + } + + + + public Integer getCode() { return code; } @@ -64,15 +88,17 @@ public class Response { return msg; } - public Response setMsg(String msg){ - this.msg=msg; + public Response setMsg(String msg) { + this.msg = msg; return this; } - public T getData() {return data;} + public T getData() { + return data; + } - public Response setData(T data){ - this.data=data; + public Response setData(T data) { + this.data = data; return this; } } diff --git a/system/src/main/java/com/canvas/web/modules/security/config/SpringSecurityConfig.java b/system/src/main/java/com/canvas/web/modules/security/config/SpringSecurityConfig.java index b9539a1..75868f4 100644 --- a/system/src/main/java/com/canvas/web/modules/security/config/SpringSecurityConfig.java +++ b/system/src/main/java/com/canvas/web/modules/security/config/SpringSecurityConfig.java @@ -123,8 +123,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { // 所有类型的接口都放行 .antMatchers(anonymousUrls.get(RequestMethodEnum.ALL.getType()).toArray(new String[0])).permitAll() // 所有请求都需要认证 测试需求注释 - .anyRequest().authenticated() - .and().apply(securityConfigurerAdapter()); + .anyRequest().authenticated() + .and().apply(securityConfigurerAdapter()); } private TokenConfigurer securityConfigurerAdapter() { diff --git a/system/src/main/java/com/canvas/web/modules/system/controller/UserController.java b/system/src/main/java/com/canvas/web/modules/system/controller/UserController.java index 7a937f3..d1f2be2 100644 --- a/system/src/main/java/com/canvas/web/modules/system/controller/UserController.java +++ b/system/src/main/java/com/canvas/web/modules/system/controller/UserController.java @@ -49,10 +49,13 @@ public class UserController { //如果当前用户角色级别低于创建用户的角色级别,抛出权限不足 private void checkLevel(User resources){ + Integer currentLevel= Collections.min(roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList())); Integer optLevel = roleService.findByRoles(resources.getRoles()); if (currentLevel > optLevel){ throw new BaseException("权限不足"); } } + + }