-
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 SensitiveWordAop to SensitiveWordService
- Loading branch information
Showing
10 changed files
with
57 additions
and
168 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
src/main/kotlin/plus/maa/backend/common/annotation/SensitiveWordDetection.kt
This file was deleted.
Oops, something went wrong.
75 changes: 0 additions & 75 deletions
75
src/main/kotlin/plus/maa/backend/common/aop/SensitiveWordAop.kt
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
src/main/kotlin/plus/maa/backend/config/SensitiveWordConfig.kt
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
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
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
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
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
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/plus/maa/backend/service/sensitiveword/SensitiveWordException.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,3 @@ | ||
package plus.maa.backend.service.sensitiveword | ||
|
||
class SensitiveWordException(message: String?) : RuntimeException(message) |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/plus/maa/backend/service/sensitiveword/SensitiveWordService.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,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") | ||
} | ||
} |