Skip to content

Commit

Permalink
refactor: convert SensitiveWordAop to SensitiveWordService
Browse files Browse the repository at this point in the history
  • Loading branch information
Handiwork committed Apr 20, 2024
1 parent d2e01e4 commit 88641b1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 168 deletions.

This file was deleted.

75 changes: 0 additions & 75 deletions src/main/kotlin/plus/maa/backend/common/aop/SensitiveWordAop.kt

This file was deleted.

61 changes: 0 additions & 61 deletions src/main/kotlin/plus/maa/backend/config/SensitiveWordConfig.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import plus.maa.backend.common.annotation.SensitiveWordDetection
import plus.maa.backend.config.doc.RequireJwt
import plus.maa.backend.config.security.AuthenticationHelper
import plus.maa.backend.controller.request.comments.CommentsAddDTO
Expand All @@ -35,7 +34,6 @@ class CommentsAreaController(
private val commentsAreaService: CommentsAreaService,
private val authHelper: AuthenticationHelper,
) {
@SensitiveWordDetection("#comments.message")
@PostMapping("/add")
@Operation(summary = "发送评论")
@ApiResponse(description = "发送评论结果")
Expand Down
12 changes: 3 additions & 9 deletions src/main/kotlin/plus/maa/backend/controller/CopilotController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import plus.maa.backend.common.annotation.SensitiveWordDetection
import plus.maa.backend.config.doc.RequireJwt
import plus.maa.backend.config.security.AuthenticationHelper
import plus.maa.backend.controller.request.copilot.CopilotCUDRequest
Expand Down Expand Up @@ -44,12 +43,10 @@ class CopilotController(
@Operation(summary = "上传作业")
@ApiResponse(description = "上传作业结果")
@RequireJwt
@SensitiveWordDetection(
"#request.content != null ? #objectMapper.readTree(#request.content).get('doc')?.toString() : null",
)
@PostMapping("/upload")
fun uploadCopilot(@RequestBody @Valid request: CopilotCUDRequest): MaaResult<Long> =
success(copilotService.upload(helper.requireUserId(), request.content))
fun uploadCopilot(@RequestBody @Valid request: CopilotCUDRequest): MaaResult<Long> {
return success(copilotService.upload(helper.requireUserId(), request.content))
}

@Operation(summary = "删除作业")
@ApiResponse(description = "删除作业结果")
Expand Down Expand Up @@ -81,9 +78,6 @@ class CopilotController(
@Operation(summary = "更新作业")
@ApiResponse(description = "更新结果")
@RequireJwt
@SensitiveWordDetection(
"#copilotCUDRequest.content != null ? #objectMapper.readTree(#copilotCUDRequest.content).get('doc')?.toString() : null",
)
@PostMapping("/update")
fun updateCopilot(@RequestBody @Valid copilotCUDRequest: CopilotCUDRequest): MaaResult<Unit> {
copilotService.update(helper.requireUserId(), copilotCUDRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.springframework.web.servlet.NoHandlerFoundException
import plus.maa.backend.controller.response.MaaResult
import plus.maa.backend.controller.response.MaaResult.Companion.fail
import plus.maa.backend.controller.response.MaaResultException
import plus.maa.backend.service.sensitiveword.SensitiveWordException

private val log = KotlinLogging.logger { }

Expand Down Expand Up @@ -132,6 +133,11 @@ class GlobalExceptionHandler {
return fail(ex.statusCode.value(), ex.message)
}

@ExceptionHandler(SensitiveWordException::class)
fun handleSensitiveWordException(ex: SensitiveWordException): MaaResult<Unit> {
return fail(400, ex.message)
}

/**
* @return plus.maa.backend.controller.response.MaaResult
* @author john180
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import plus.maa.backend.repository.entity.CommentsArea
import plus.maa.backend.repository.entity.Copilot
import plus.maa.backend.repository.entity.MaaUser
import plus.maa.backend.service.model.RatingType
import plus.maa.backend.service.sensitiveword.SensitiveWordService
import java.time.LocalDateTime

/**
Expand All @@ -33,6 +34,7 @@ class CommentsAreaService(
private val copilotRepository: CopilotRepository,
private val userRepository: UserRepository,
private val emailService: EmailService,
private val sensitiveWordService: SensitiveWordService,
) {
/**
* 评论
Expand All @@ -42,6 +44,7 @@ class CommentsAreaService(
* @param commentsAddDTO CommentsRequest
*/
fun addComments(userId: String, commentsAddDTO: CommentsAddDTO) {
sensitiveWordService.validate(commentsAddDTO.message)
val copilotId = commentsAddDTO.copilotId
val copilot = copilotRepository.findByCopilotId(copilotId).requireNotNull { "作业不存在" }

Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/plus/maa/backend/service/CopilotService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.springframework.util.Assert
import org.springframework.util.ObjectUtils
import plus.maa.backend.common.utils.IdComponent
import plus.maa.backend.common.utils.converter.CopilotConverter
import plus.maa.backend.common.utils.requireNotNull
import plus.maa.backend.config.external.MaaCopilotProperties
import plus.maa.backend.controller.request.copilot.CopilotCUDRequest
import plus.maa.backend.controller.request.copilot.CopilotDTO
Expand All @@ -36,6 +37,7 @@ import plus.maa.backend.repository.findByUsersId
import plus.maa.backend.service.level.ArkLevelService
import plus.maa.backend.service.model.RatingCache
import plus.maa.backend.service.model.RatingType
import plus.maa.backend.service.sensitiveword.SensitiveWordService
import java.math.RoundingMode
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
Expand Down Expand Up @@ -67,6 +69,7 @@ class CopilotService(
private val commentsAreaRepository: CommentsAreaRepository,
private val properties: MaaCopilotProperties,
private val copilotConverter: CopilotConverter,
private val sensitiveWordService: SensitiveWordService,
) {
/**
* 并修正前端的冗余部分
Expand Down Expand Up @@ -132,6 +135,7 @@ class CopilotService(
*/
fun upload(loginUserId: String, content: String?): Long {
val copilotDTO = correctCopilot(parseToCopilotDto(content))
sensitiveWordService.validate(copilotDTO.doc)
// 将其转换为数据库存储对象
val copilot = copilotConverter.toCopilot(
copilotDTO,
Expand Down Expand Up @@ -363,9 +367,10 @@ class CopilotService(
*/
fun update(loginUserId: String, copilotCUDRequest: CopilotCUDRequest) {
val content = copilotCUDRequest.content
val id = copilotCUDRequest.id
copilotRepository.findByCopilotId(id!!)?.let { copilot: Copilot ->
val id = copilotCUDRequest.id.requireNotNull { "id 不能为空" }
copilotRepository.findByCopilotId(id)?.let { copilot: Copilot ->
val copilotDTO = correctCopilot(parseToCopilotDto(content))
sensitiveWordService.validate(copilotDTO.doc)
Assert.state(copilot.uploaderId == loginUserId, "您无法修改不属于您的作业")
copilot.uploadTime = LocalDateTime.now()
copilotConverter.updateCopilotFromDto(copilotDTO, content!!, copilot)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package plus.maa.backend.service.sensitiveword

class SensitiveWordException(message: String?) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package plus.maa.backend.service.sensitiveword

import cn.hutool.dfa.WordTree
import com.fasterxml.jackson.databind.ObjectMapper
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.context.ApplicationContext
import org.springframework.stereotype.Service
import plus.maa.backend.config.external.MaaCopilotProperties

@Service
class SensitiveWordService(
private val ctx: ApplicationContext,
maaCopilotProperties: MaaCopilotProperties,
private val objectMapper: ObjectMapper,
) {
private val log = KotlinLogging.logger {}
private val wordTree = WordTree().apply {
val path = maaCopilotProperties.sensitiveWord.path
try {
ctx.getResource(path).inputStream.bufferedReader().use { it.lines().forEach(::addWord) }
log.info { "初始化敏感词库完成: $path" }
} catch (e: Exception) {
log.error { "初始化敏感词库失败: $path" }
throw e
}
}

@Throws(SensitiveWordException::class)
fun <T> validate(value: T) {
if (value == null) return
val text = if (value is String) value else objectMapper.writeValueAsString(value)
val detected = wordTree.matchAll(text)
if (detected.size > 0) throw SensitiveWordException("包含敏感词:$detected")
}
}

0 comments on commit 88641b1

Please sign in to comment.