From 0109cea6bbd824d8f5501d21928b65ad45774881 Mon Sep 17 00:00:00 2001 From: Handiwork Date: Fri, 19 Apr 2024 22:43:03 +0800 Subject: [PATCH] refactor: convert WebUtils to DataTransferService --- .../plus/maa/backend/common/utils/WebUtils.kt | 20 --------------- .../accesslimit/AccessLimitInterceptor.kt | 8 +++--- .../security/AccessDeniedHandlerImpl.kt | 8 +++--- .../security/AuthenticationEntryPointImpl.kt | 8 +++--- .../backend/service/DataTransferService.kt | 25 +++++++++++++++++++ 5 files changed, 34 insertions(+), 35 deletions(-) delete mode 100644 src/main/kotlin/plus/maa/backend/common/utils/WebUtils.kt create mode 100644 src/main/kotlin/plus/maa/backend/service/DataTransferService.kt diff --git a/src/main/kotlin/plus/maa/backend/common/utils/WebUtils.kt b/src/main/kotlin/plus/maa/backend/common/utils/WebUtils.kt deleted file mode 100644 index 9b818b9c..00000000 --- a/src/main/kotlin/plus/maa/backend/common/utils/WebUtils.kt +++ /dev/null @@ -1,20 +0,0 @@ -package plus.maa.backend.common.utils - -import jakarta.servlet.http.HttpServletResponse -import java.io.IOException - -/** - * @author AnselYuki - */ -object WebUtils { - fun renderString(response: HttpServletResponse, json: String?, code: Int) { - try { - response.status = code - response.contentType = "application/json" - response.characterEncoding = "UTF-8" - response.writer.println(json) - } catch (e: IOException) { - e.printStackTrace() - } - } -} diff --git a/src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitInterceptor.kt b/src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitInterceptor.kt index 5af3dbd2..989c4515 100644 --- a/src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitInterceptor.kt +++ b/src/main/kotlin/plus/maa/backend/config/accesslimit/AccessLimitInterceptor.kt @@ -1,6 +1,5 @@ package plus.maa.backend.config.accesslimit -import com.fasterxml.jackson.databind.ObjectMapper import io.github.oshai.kotlinlogging.KotlinLogging import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse @@ -9,8 +8,8 @@ import org.springframework.http.HttpStatus import org.springframework.web.method.HandlerMethod import org.springframework.web.servlet.HandlerInterceptor import plus.maa.backend.common.utils.IpUtil -import plus.maa.backend.common.utils.WebUtils import plus.maa.backend.controller.response.MaaResult.Companion.fail +import plus.maa.backend.service.DataTransferService import java.util.concurrent.TimeUnit /** @@ -18,7 +17,7 @@ import java.util.concurrent.TimeUnit */ class AccessLimitInterceptor( private val stringRedisTemplate: StringRedisTemplate, - private val objectMapper: ObjectMapper, + private val dataTransferService: DataTransferService, ) : HandlerInterceptor { private val log = KotlinLogging.logger { } @@ -42,8 +41,7 @@ class AccessLimitInterceptor( // 请求过于频繁 log.info { "$key 请求过于频繁" } val result = fail(HttpStatus.TOO_MANY_REQUESTS.value(), "请求过于频繁") - val json = objectMapper.writeValueAsString(result) - WebUtils.renderString(response, json, HttpStatus.TOO_MANY_REQUESTS.value()) + dataTransferService.writeJson(response, result, HttpStatus.TOO_MANY_REQUESTS.value()) return false } diff --git a/src/main/kotlin/plus/maa/backend/config/security/AccessDeniedHandlerImpl.kt b/src/main/kotlin/plus/maa/backend/config/security/AccessDeniedHandlerImpl.kt index 33d9fb19..410233f5 100644 --- a/src/main/kotlin/plus/maa/backend/config/security/AccessDeniedHandlerImpl.kt +++ b/src/main/kotlin/plus/maa/backend/config/security/AccessDeniedHandlerImpl.kt @@ -1,25 +1,23 @@ package plus.maa.backend.config.security -import com.fasterxml.jackson.databind.ObjectMapper import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.http.HttpStatus import org.springframework.security.access.AccessDeniedException import org.springframework.security.web.access.AccessDeniedHandler import org.springframework.stereotype.Component -import plus.maa.backend.common.utils.WebUtils.renderString import plus.maa.backend.controller.response.MaaResult.Companion.fail +import plus.maa.backend.service.DataTransferService import java.io.IOException /** * @author AnselYuki */ @Component -class AccessDeniedHandlerImpl : AccessDeniedHandler { +class AccessDeniedHandlerImpl(private val dataTransferService: DataTransferService) : AccessDeniedHandler { @Throws(IOException::class) override fun handle(request: HttpServletRequest, response: HttpServletResponse, accessDeniedException: AccessDeniedException) { val result = fail(HttpStatus.FORBIDDEN.value(), "权限不足") - val json = ObjectMapper().writeValueAsString(result) - renderString(response, json, HttpStatus.FORBIDDEN.value()) + dataTransferService.writeJson(response, result, HttpStatus.FORBIDDEN.value()) } } diff --git a/src/main/kotlin/plus/maa/backend/config/security/AuthenticationEntryPointImpl.kt b/src/main/kotlin/plus/maa/backend/config/security/AuthenticationEntryPointImpl.kt index 28cb5866..ff77df0e 100644 --- a/src/main/kotlin/plus/maa/backend/config/security/AuthenticationEntryPointImpl.kt +++ b/src/main/kotlin/plus/maa/backend/config/security/AuthenticationEntryPointImpl.kt @@ -1,14 +1,13 @@ package plus.maa.backend.config.security -import com.fasterxml.jackson.databind.ObjectMapper import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.http.HttpStatus import org.springframework.security.core.AuthenticationException import org.springframework.security.web.AuthenticationEntryPoint import org.springframework.stereotype.Component -import plus.maa.backend.common.utils.WebUtils.renderString import plus.maa.backend.controller.response.MaaResult.Companion.fail +import plus.maa.backend.service.DataTransferService import java.io.IOException /** @@ -16,12 +15,11 @@ import java.io.IOException */ @Component class AuthenticationEntryPointImpl( - private val objectMapper: ObjectMapper, + private val dataTransferService: DataTransferService, ) : AuthenticationEntryPoint { @Throws(IOException::class) override fun commence(request: HttpServletRequest, response: HttpServletResponse, authException: AuthenticationException) { val result = fail(HttpStatus.UNAUTHORIZED.value(), authException.message) - val json = objectMapper.writeValueAsString(result) - renderString(response, json, HttpStatus.UNAUTHORIZED.value()) + dataTransferService.writeJson(response, result, HttpStatus.UNAUTHORIZED.value()) } } diff --git a/src/main/kotlin/plus/maa/backend/service/DataTransferService.kt b/src/main/kotlin/plus/maa/backend/service/DataTransferService.kt new file mode 100644 index 00000000..7899c8b9 --- /dev/null +++ b/src/main/kotlin/plus/maa/backend/service/DataTransferService.kt @@ -0,0 +1,25 @@ +package plus.maa.backend.service + +import com.fasterxml.jackson.databind.ObjectMapper +import jakarta.servlet.http.HttpServletResponse +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Service +import org.springframework.util.MimeTypeUtils +import java.io.IOException + +/** + * @author AnselYuki + */ +@Service +class DataTransferService(private val objectMapper: ObjectMapper) { + fun writeJson(response: HttpServletResponse, value: T, code: Int = HttpStatus.OK.value()) { + try { + response.status = code + response.contentType = MimeTypeUtils.APPLICATION_JSON_VALUE + response.characterEncoding = "UTF-8" + objectMapper.writeValue(response.outputStream, value) + } catch (e: IOException) { + e.printStackTrace() + } + } +}