mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 07:01:37 +08:00
refactor(security/sensitivewords): 优化敏感词模块代码
This commit is contained in:
@@ -54,6 +54,11 @@ public class PropertiesConstants {
|
||||
*/
|
||||
public static final String SECURITY_LIMITER = SECURITY + StringConstants.DOT + "limiter";
|
||||
|
||||
/**
|
||||
* 敏感词配置
|
||||
*/
|
||||
public static final String SECURITY_SENSITIVE_WORDS = SECURITY + StringConstants.DOT + "sensitive-words";
|
||||
|
||||
/**
|
||||
* Web 配置
|
||||
*/
|
||||
|
@@ -572,6 +572,13 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 安全模块 - 敏感词 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-security-sensitivewords</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- API 文档模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
@@ -592,14 +599,6 @@
|
||||
<artifactId>continew-starter-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 敏感词模块 -->
|
||||
<dependency>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter-sensitive-words</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@@ -5,25 +5,15 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.continew</groupId>
|
||||
<artifactId>continew-starter</artifactId>
|
||||
<artifactId>continew-starter-security</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-sensitive-words</artifactId>
|
||||
<description>Continew starter sensitive words 模块</description>
|
||||
<artifactId>continew-starter-security-sensitivewords</artifactId>
|
||||
<description>ContiNew Starter 安全模块 - 敏感词模块</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot Starter(自动配置相关依赖) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Hutool(工具类库) -->
|
||||
<!-- Hutool DFA 模块(基于 DFA 的关键词查找) -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-dfa</artifactId>
|
||||
@@ -33,10 +23,5 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.security.sensitivewords.autoconfigure;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import top.continew.starter.security.sensitivewords.service.DefaultSensitiveWordsConfig;
|
||||
import top.continew.starter.security.sensitivewords.service.DefaultSensitiveWordsService;
|
||||
import top.continew.starter.security.sensitivewords.service.SensitiveWordsConfig;
|
||||
import top.continew.starter.security.sensitivewords.service.SensitiveWordsService;
|
||||
|
||||
/**
|
||||
* 敏感词自动配置
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(SensitiveWordsProperties.class)
|
||||
public class SensitiveWordsAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SensitiveWordsAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* 默认敏感词配置
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SensitiveWordsConfig sensitiveWordsConfig(SensitiveWordsProperties properties) {
|
||||
return new DefaultSensitiveWordsConfig(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认敏感词服务
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SensitiveWordsService sensitiveWordsService(SensitiveWordsConfig sensitiveWordsConfig) {
|
||||
return new DefaultSensitiveWordsService(sensitiveWordsConfig);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'Security-Sensitive Words' completed initialization.");
|
||||
}
|
||||
}
|
@@ -14,19 +14,33 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.autoconfigure;
|
||||
package top.continew.starter.security.sensitivewords.autoconfigure;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "continew.sensitive-words")
|
||||
/**
|
||||
* 敏感词配置属性
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
@ConfigurationProperties(PropertiesConstants.SECURITY_SENSITIVE_WORDS)
|
||||
public class SensitiveWordsProperties {
|
||||
// 敏感词注入类型
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 敏感词列表
|
||||
*/
|
||||
private List<String> values;
|
||||
|
||||
public List<String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public void setValues(List<String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
}
|
@@ -14,19 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.service;
|
||||
package top.continew.starter.security.sensitivewords.service;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.continew.starter.sensitive.words.autoconfigure.SensitiveWordsProperties;
|
||||
import top.continew.starter.security.sensitivewords.autoconfigure.SensitiveWordsProperties;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认敏感词配置
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "continew.sensitive-words", name = "type", havingValue = "default", matchIfMissing = true)
|
||||
public class DefaultSensitiveWordsConfig implements SensitiveWordsConfig {
|
||||
|
||||
private final SensitiveWordsProperties properties;
|
@@ -14,26 +14,27 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.service;
|
||||
package top.continew.starter.security.sensitivewords.service;
|
||||
|
||||
import cn.hutool.dfa.WordTree;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 默认敏感词服务
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
@Component
|
||||
@ConditionalOnBean(SensitiveWordsConfig.class)
|
||||
@ConditionalOnMissingBean(SensitiveWordsService.class)
|
||||
public class DefaultSensitiveWordsService implements SensitiveWordsService {
|
||||
|
||||
private final SensitiveWordsConfig sensitiveWordsConfig;
|
||||
|
||||
private WordTree tree = new WordTree();
|
||||
private final WordTree tree = new WordTree();
|
||||
|
||||
public DefaultSensitiveWordsService(SensitiveWordsConfig sensitiveWordsConfig) {
|
||||
this.sensitiveWordsConfig = sensitiveWordsConfig;
|
@@ -14,15 +14,23 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.service;
|
||||
package top.continew.starter.security.sensitivewords.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 敏感词配置
|
||||
* 敏感词配置接口
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public interface SensitiveWordsConfig {
|
||||
|
||||
/**
|
||||
* 获取敏感词列表
|
||||
*
|
||||
* @return 敏感词列表
|
||||
*/
|
||||
List<String> getWords();
|
||||
|
||||
}
|
@@ -14,11 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.service;
|
||||
package top.continew.starter.security.sensitivewords.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 敏感词服务接口
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public interface SensitiveWordsService {
|
||||
|
||||
/**
|
||||
* 检查敏感词
|
||||
*
|
@@ -14,22 +14,44 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.validate;
|
||||
package top.continew.starter.security.sensitivewords.validation;
|
||||
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.Payload;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 敏感词注解
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
@Target({ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Constraint(validatedBy = {SensitiveWordValidator.class})
|
||||
public @interface SensitiveWord {
|
||||
@Constraint(validatedBy = {SensitiveWordsValidator.class})
|
||||
public @interface SensitiveWords {
|
||||
|
||||
String message() default "有敏感词,请检测!";
|
||||
/**
|
||||
* 提示消息
|
||||
*
|
||||
* @return 提示消息
|
||||
*/
|
||||
String message() default "内容包含敏感词汇";
|
||||
|
||||
/**
|
||||
* 分组
|
||||
*
|
||||
* @return 分组
|
||||
*/
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
/**
|
||||
* 负载
|
||||
*
|
||||
* @return 负载
|
||||
*/
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
@@ -14,45 +14,35 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.validate;
|
||||
package top.continew.starter.security.sensitivewords.validation;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import top.continew.starter.sensitive.words.service.SensitiveWordsService;
|
||||
import top.continew.starter.security.sensitivewords.service.SensitiveWordsService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SensitiveWordValidator implements ConstraintValidator<SensitiveWord, String> {
|
||||
/**
|
||||
* 敏感词校验器
|
||||
*
|
||||
* @author luoqiz
|
||||
* @author Charles7c
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public class SensitiveWordsValidator implements ConstraintValidator<SensitiveWords, String> {
|
||||
|
||||
@Resource
|
||||
private SensitiveWordsService sensitiveWordsService;
|
||||
|
||||
/**
|
||||
* 初始化方法,可以用自定义注解中获取值进行初始化
|
||||
*
|
||||
* @param {@link SensitiveWord } constraintAnnotation 注解值内容
|
||||
*/
|
||||
@Override
|
||||
public void initialize(SensitiveWord constraintAnnotation) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际校验自定义注解 value 值
|
||||
*
|
||||
* @param {@link String} value 待检测字符串
|
||||
* @param {@link ConstraintValidatorContext } constraintValidatorContext 检测的上下文
|
||||
* @return boolean 是否通过检测
|
||||
*/
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
List<String> res = sensitiveWordsService.check(value);
|
||||
if (!res.isEmpty()) {
|
||||
// 禁用默认消息
|
||||
context.disableDefaultConstraintViolation();
|
||||
// 动态设置错误消息
|
||||
context.disableDefaultConstraintViolation(); // 禁用默认消息
|
||||
context.buildConstraintViolationWithTemplate("包含敏感词: " + String.join(",", res))
|
||||
.addConstraintViolation();
|
||||
context.buildConstraintViolationWithTemplate("内容包含敏感词汇: " + String.join(",", res)).addConstraintViolation();
|
||||
return false;
|
||||
}
|
||||
return true;
|
@@ -0,0 +1 @@
|
||||
top.continew.starter.security.sensitivewords.autoconfigure.SensitiveWordsAutoConfiguration
|
@@ -18,6 +18,7 @@
|
||||
<module>continew-starter-security-mask</module>
|
||||
<module>continew-starter-security-crypto</module>
|
||||
<module>continew-starter-security-limiter</module>
|
||||
<module>continew-starter-security-sensitivewords</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words.autoconfigure;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
|
||||
/**
|
||||
* JSR 303 校验器自动配置
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.3.0
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class SensitiveWordsAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SensitiveWordsAutoConfiguration.class);
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'sensitive words service' completed initialization.");
|
||||
}
|
||||
}
|
@@ -1,4 +0,0 @@
|
||||
top.continew.starter.sensitive.words.autoconfigure.SensitiveWordsAutoConfiguration
|
||||
top.continew.starter.sensitive.words.autoconfigure.SensitiveWordsProperties
|
||||
top.continew.starter.sensitive.words.service.DefaultSensitiveWordsConfig
|
||||
top.continew.starter.sensitive.words.service.DefaultSensitiveWordsService
|
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.sensitive.words;
|
||||
|
||||
import cn.hutool.dfa.FoundWord;
|
||||
import cn.hutool.dfa.WordTree;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SensitiveWordsTest {
|
||||
public static void main(String[] args) {
|
||||
WordTree tree = new WordTree();
|
||||
tree.addWord("大");
|
||||
tree.addWord("大土豆");
|
||||
tree.addWord("土豆");
|
||||
tree.addWord("刚出锅");
|
||||
tree.addWord("出锅");
|
||||
//正文
|
||||
String text = "我有一颗大土豆,刚出锅的";
|
||||
|
||||
// 匹配到【大】,由于非密集匹配,因此从下一个字符开始查找,匹配到【土豆】接着被匹配
|
||||
// 由于【刚出锅】被匹配,由于非密集匹配,【出锅】被跳过
|
||||
List<String> matchAll = tree.matchAll(text, -1, false, true);
|
||||
for (String s : matchAll) {
|
||||
System.out.println(s);
|
||||
}
|
||||
System.out.println("-------------------");
|
||||
String match = tree.match(text);
|
||||
System.out.println(match);
|
||||
|
||||
System.out.println("-------------------");
|
||||
FoundWord matchText = tree.matchWord(text);
|
||||
System.out.println(matchText.getFoundWord());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user