增加错误处理,修复命名错误

This commit is contained in:
05412 2024-08-01 14:00:56 +08:00
parent 6f3ec5a45a
commit 5df5346176
4 changed files with 17 additions and 12 deletions

View File

@ -1,6 +0,0 @@
package dev.surl.surl.common.exception
/**
* 自定义权限异常
*/
class UnauthorizedExcecption(message: String? = null, cause: Throwable? = null) : Exception(message, cause)

View File

@ -0,0 +1,6 @@
package dev.surl.surl.common.exception
/**
* 自定义权限异常
*/
class UnauthorizedException(message: String? = null, cause: Throwable? = null) : Exception(message, cause)

View File

@ -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) {
// 认证失败
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<String>(code = -1, msg = e.message))
response.writer.run {
write(responseBody)

View File

@ -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<Msg<String>> {
return ResponseEntity(Msg(code = -1, msg = ex.message ?: "unauthorized"), HttpStatus.UNAUTHORIZED)
}