From 5df5346176c94e0ef909677910ab407e71c0871f Mon Sep 17 00:00:00 2001 From: 05412 <2738076308@qq.com> Date: Thu, 1 Aug 2024 14:00:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=91=BD=E5=90=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../surl/common/exception/UnauthorizedExcecption.kt | 6 ------ .../surl/common/exception/UnauthorizedException.kt | 6 ++++++ .../surl/filter/JwtAuthenticationTokenFilter.kt | 13 +++++++++---- .../surl/surl/handler/DefaultExceptionHandler.kt | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 src/main/java/dev/surl/surl/common/exception/UnauthorizedExcecption.kt create mode 100644 src/main/java/dev/surl/surl/common/exception/UnauthorizedException.kt diff --git a/src/main/java/dev/surl/surl/common/exception/UnauthorizedExcecption.kt b/src/main/java/dev/surl/surl/common/exception/UnauthorizedExcecption.kt deleted file mode 100644 index 1022854..0000000 --- a/src/main/java/dev/surl/surl/common/exception/UnauthorizedExcecption.kt +++ /dev/null @@ -1,6 +0,0 @@ -package dev.surl.surl.common.exception - -/** - * 自定义权限异常 - */ -class UnauthorizedExcecption(message: String? = null, cause: Throwable? = null) : Exception(message, cause) \ No newline at end of file diff --git a/src/main/java/dev/surl/surl/common/exception/UnauthorizedException.kt b/src/main/java/dev/surl/surl/common/exception/UnauthorizedException.kt new file mode 100644 index 0000000..081c587 --- /dev/null +++ b/src/main/java/dev/surl/surl/common/exception/UnauthorizedException.kt @@ -0,0 +1,6 @@ +package dev.surl.surl.common.exception + +/** + * 自定义权限异常 + */ +class UnauthorizedException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) \ No newline at end of file diff --git a/src/main/java/dev/surl/surl/filter/JwtAuthenticationTokenFilter.kt b/src/main/java/dev/surl/surl/filter/JwtAuthenticationTokenFilter.kt index 7ec7ddd..5b8bbeb 100644 --- a/src/main/java/dev/surl/surl/filter/JwtAuthenticationTokenFilter.kt +++ b/src/main/java/dev/surl/surl/filter/JwtAuthenticationTokenFilter.kt @@ -4,13 +4,14 @@ import com.fasterxml.jackson.databind.ObjectMapper import dev.surl.surl.cfg.BaseConfig import dev.surl.surl.common.Msg import dev.surl.surl.common.enums.RedisStorage -import dev.surl.surl.common.exception.UnauthorizedExcecption +import dev.surl.surl.common.exception.UnauthorizedException import dev.surl.surl.util.JwtTokenUtil import dev.surl.surl.util.redis.RedisUtil import jakarta.servlet.FilterChain import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.http.HttpHeaders +import org.springframework.oxm.ValidationFailureException import org.springframework.stereotype.Component import org.springframework.web.filter.OncePerRequestFilter @@ -33,7 +34,7 @@ class JwtAuthenticationTokenFilter( if (request.servletPath notMatchedIn cfg.whiteList) { try { // 验证token - val exp = UnauthorizedExcecption("unauthorized") + val exp = UnauthorizedException("unauthorized") val authHeader = request.getHeader(HttpHeaders.AUTHORIZATION) ?: throw exp val token = jwtTokenUtil.getTokenFromHeader(authHeader) val cachedToken = run { @@ -45,9 +46,13 @@ class JwtAuthenticationTokenFilter( } // redis缓存内检查不到已存在token拒绝认证,抛出异常 if (cachedToken != token) throw exp - } catch (e: UnauthorizedExcecption) { + } catch (e: Exception) { // 认证失败 - response.status = HttpServletResponse.SC_UNAUTHORIZED + if(e is UnauthorizedException || e is ValidationFailureException) { + response.status = HttpServletResponse.SC_UNAUTHORIZED + } else { + response.status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR + } val responseBody = om.writeValueAsString(Msg(code = -1, msg = e.message)) response.writer.run { write(responseBody) diff --git a/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt b/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt index c6e0e4b..ddca2c9 100644 --- a/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt +++ b/src/main/java/dev/surl/surl/handler/DefaultExceptionHandler.kt @@ -1,7 +1,7 @@ package dev.surl.surl.handler import dev.surl.surl.common.Msg -import dev.surl.surl.common.exception.UnauthorizedExcecption +import dev.surl.surl.common.exception.UnauthorizedException import dev.surl.surl.common.exception.UserRegistException import jakarta.validation.ConstraintViolationException import org.springframework.http.HttpHeaders @@ -98,7 +98,7 @@ class DefaultExceptionHandler : ResponseEntityExceptionHandler() { return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unknown validation error"), HttpStatus.BAD_REQUEST) } - @ExceptionHandler(value = [UnauthorizedExcecption::class]) + @ExceptionHandler(value = [UnauthorizedException::class]) fun handleUnauthorizedException(ex: Exception): ResponseEntity> { return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unauthorized"), HttpStatus.UNAUTHORIZED) }