-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert WebUtils to DataTransferService
- Loading branch information
Showing
5 changed files
with
34 additions
and
35 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
src/main/kotlin/plus/maa/backend/config/security/AccessDeniedHandlerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
8 changes: 3 additions & 5 deletions
8
src/main/kotlin/plus/maa/backend/config/security/AuthenticationEntryPointImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/kotlin/plus/maa/backend/service/DataTransferService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |