Skip to content

Commit

Permalink
refactor: convert WebUtils to DataTransferService
Browse files Browse the repository at this point in the history
  • Loading branch information
Handiwork committed Apr 19, 2024
1 parent 0bf8512 commit 0109cea
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
20 changes: 0 additions & 20 deletions src/main/kotlin/plus/maa/backend/common/utils/WebUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,16 +8,16 @@ 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

/**
* @author Baip1995
*/
class AccessLimitInterceptor(
private val stringRedisTemplate: StringRedisTemplate,
private val objectMapper: ObjectMapper,
private val dataTransferService: DataTransferService,
) : HandlerInterceptor {
private val log = KotlinLogging.logger { }

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
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

/**
* @author AnselYuki
*/
@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())
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/plus/maa/backend/service/DataTransferService.kt
Original file line number Diff line number Diff line change
@@ -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 <T> 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()
}
}
}

0 comments on commit 0109cea

Please sign in to comment.