diff --git a/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DefaultExceptionHandler.java b/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DefaultExceptionHandler.java index 541e705db6cd71163ebaa49e508b6c12c462bca1..1f6a5181c11956ad290bb723f2c509c8d5d743fc 100644 --- a/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DefaultExceptionHandler.java +++ b/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DefaultExceptionHandler.java @@ -17,9 +17,7 @@ package cn.stylefeng.roses.core.exception; import cn.stylefeng.roses.core.reqres.response.ErrorResponseData; import cn.stylefeng.roses.core.reqres.response.ResponseData; -import cn.stylefeng.roses.kernel.model.exception.ApiServiceException; -import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException; -import cn.stylefeng.roses.kernel.model.exception.ServiceException; +import cn.stylefeng.roses.kernel.model.exception.*; import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; @@ -78,6 +76,28 @@ public class DefaultExceptionHandler { return new ErrorResponseData(e.getCode(), e.getErrorMessage()); } + /** + * 拦截未授权的异常 + */ + @ExceptionHandler(UnauthorizedException.class) + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ResponseBody + public ResponseData unauthorizedError(UnauthorizedException e) { + log.error("未授权异常:", e); + return new ErrorResponseData(e.getCode(), e.getMessage()); + } + + /** + * 拦截无权限的异常 + */ + @ExceptionHandler(ForbiddenException.class) + @ResponseStatus(HttpStatus.FORBIDDEN) + @ResponseBody + public ResponseData forbiddenError(ForbiddenException e) { + log.error("无权限异常:", e); + return new ErrorResponseData(e.getCode(), e.getMessage()); + } + /** * 拦截未知的运行时异常 */ diff --git a/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DevelopExceptionHandler.java b/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DevelopExceptionHandler.java index 034cd61c6735625b59cc7ed90df6fdbb11150b5b..28ab311d57c1630cd7e3c210ea6895375b3f0eab 100644 --- a/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DevelopExceptionHandler.java +++ b/kernel-core/src/main/java/cn/stylefeng/roses/core/exception/DevelopExceptionHandler.java @@ -18,9 +18,7 @@ package cn.stylefeng.roses.core.exception; import cn.stylefeng.roses.core.reqres.response.ErrorResponseData; import cn.stylefeng.roses.core.reqres.response.ResponseData; import cn.stylefeng.roses.core.util.HttpContext; -import cn.stylefeng.roses.kernel.model.exception.ApiServiceException; -import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException; -import cn.stylefeng.roses.kernel.model.exception.ServiceException; +import cn.stylefeng.roses.kernel.model.exception.*; import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum; import lombok.extern.slf4j.Slf4j; import org.springframework.core.annotation.Order; @@ -90,6 +88,28 @@ public class DevelopExceptionHandler { return getErrorResponse(CoreExceptionEnum.SERVICE_ERROR.getCode(), e.getMessage()); } + /** + * 拦截未授权的异常 + */ + @ExceptionHandler(UnauthorizedException.class) + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ResponseBody + public ResponseData unauthorizedError(UnauthorizedException e) { + log.error("未授权异常:", e); + return getErrorResponse(e.getCode(), e.getMessage()); + } + + /** + * 拦截无权限的异常 + */ + @ExceptionHandler(ForbiddenException.class) + @ResponseStatus(HttpStatus.FORBIDDEN) + @ResponseBody + public ResponseData forbiddenError(ForbiddenException e) { + log.error("无权限异常:", e); + return getErrorResponse(e.getCode(), e.getMessage()); + } + private ErrorResponseData getErrorResponse(Integer code, String message) { ErrorResponseData error = new ErrorResponseData(code, "requestNo:" + HttpContext.getRequestNo() + "," + message); return error; diff --git a/kernel-core/src/main/java/cn/stylefeng/roses/core/util/redisson/RedissonDistributedLocker.java b/kernel-core/src/main/java/cn/stylefeng/roses/core/util/redisson/RedissonDistributedLocker.java index b9f2cd0d857ccda102b24a9de702f6e8a9dfe78f..e1f1ae829984e3759326319e745d5d5fe478296d 100644 --- a/kernel-core/src/main/java/cn/stylefeng/roses/core/util/redisson/RedissonDistributedLocker.java +++ b/kernel-core/src/main/java/cn/stylefeng/roses/core/util/redisson/RedissonDistributedLocker.java @@ -18,7 +18,11 @@ public class RedissonDistributedLocker implements DistributedLocker { @Override public void unlock(String lockKey) { RLock lock = redissonClient.getLock(lockKey); - lock.unlock(); + if (lock.isLocked()) { + if (lock.isHeldByCurrentThread()) { + lock.unlock(); + } + } } @Override diff --git a/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/ForbiddenException.java b/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/ForbiddenException.java new file mode 100644 index 0000000000000000000000000000000000000000..26781a5feff9d880e606528ec1b3581905a17aaf --- /dev/null +++ b/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/ForbiddenException.java @@ -0,0 +1,51 @@ +package cn.stylefeng.roses.kernel.model.exception; + +/** + * 已登录但无权限异常的封装 + * + * @author pony + */ +public class ForbiddenException extends RuntimeException { + + private Integer code; + + private String errorMessage = "无权限,配置权限后再试"; + + public ForbiddenException() { + this(403); + this.code = 403; + } + + public ForbiddenException(Integer code) { + super(); + this.code = code; + } + + public ForbiddenException(Integer code, String errorMessage) { + super(errorMessage); + this.code = code; + this.errorMessage = errorMessage; + } + + public ForbiddenException(AbstractBaseExceptionEnum exception) { + super(exception.getMessage()); + this.code = exception.getCode(); + this.errorMessage = exception.getMessage(); + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } +} diff --git a/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/UnauthorizedException.java b/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/UnauthorizedException.java new file mode 100644 index 0000000000000000000000000000000000000000..2eb59fb61d2e570cea39b1cff7e537c62d5f2dd4 --- /dev/null +++ b/kernel-model/src/main/java/cn/stylefeng/roses/kernel/model/exception/UnauthorizedException.java @@ -0,0 +1,51 @@ +package cn.stylefeng.roses.kernel.model.exception; + +/** + * 未授权异常的封装 + * + * @author pony + */ +public class UnauthorizedException extends RuntimeException { + + private Integer code; + + private String errorMessage = "未授权,请登录授权"; + + public UnauthorizedException() { + this(401); + this.code = 401; + } + + public UnauthorizedException(Integer code) { + super(); + this.code = code; + } + + public UnauthorizedException(Integer code, String errorMessage) { + super(errorMessage); + this.code = code; + this.errorMessage = errorMessage; + } + + public UnauthorizedException(AbstractBaseExceptionEnum exception) { + super(exception.getMessage()); + this.code = exception.getCode(); + this.errorMessage = exception.getMessage(); + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } +}