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) }