diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java index 3f52847e..a40318c3 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java @@ -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 配置 */ diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index e53694dc..ee781d20 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -572,6 +572,13 @@ ${revision} + + + top.continew + continew-starter-security-sensitivewords + ${revision} + + top.continew @@ -592,14 +599,6 @@ continew-starter-core ${revision} - - - - top.continew - continew-starter-sensitive-words - ${revision} - - diff --git a/continew-starter-sensitive-words/pom.xml b/continew-starter-security/continew-starter-security-sensitivewords/pom.xml similarity index 50% rename from continew-starter-sensitive-words/pom.xml rename to continew-starter-security/continew-starter-security-sensitivewords/pom.xml index 1a785c09..d364fa2a 100644 --- a/continew-starter-sensitive-words/pom.xml +++ b/continew-starter-security/continew-starter-security-sensitivewords/pom.xml @@ -5,25 +5,15 @@ 4.0.0 top.continew - continew-starter + continew-starter-security ${revision} - continew-starter-sensitive-words - Continew starter sensitive words 模块 + continew-starter-security-sensitivewords + ContiNew Starter 安全模块 - 敏感词模块 - - - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-configuration-processor - - - + cn.hutool hutool-dfa @@ -33,10 +23,5 @@ org.springframework.boot spring-boot-starter-validation - - - org.projectlombok - lombok - \ No newline at end of file diff --git a/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsAutoConfiguration.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsAutoConfiguration.java new file mode 100644 index 00000000..2af055f3 --- /dev/null +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsAutoConfiguration.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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."); + } +} diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsProperties.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsProperties.java similarity index 62% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsProperties.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsProperties.java index f8ae551d..9632a1bc 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsProperties.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/autoconfigure/SensitiveWordsProperties.java @@ -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 values; + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } } diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsConfig.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsConfig.java similarity index 73% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsConfig.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsConfig.java index d5fba31f..78a47e53 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsConfig.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsConfig.java @@ -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; diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsService.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsService.java similarity index 90% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsService.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsService.java index 9716651e..cef2a990 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/DefaultSensitiveWordsService.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/DefaultSensitiveWordsService.java @@ -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; diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsConfig.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsConfig.java similarity index 76% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsConfig.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsConfig.java index e63487d7..e35f5279 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsConfig.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsConfig.java @@ -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 getWords(); - } diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsService.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsService.java similarity index 84% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsService.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsService.java index fe0794e2..a51b13aa 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/service/SensitiveWordsService.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/service/SensitiveWordsService.java @@ -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 { + /** * 检查敏感词 * diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWord.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWords.java similarity index 66% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWord.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWords.java index 6efb1369..7c0d9d1c 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWord.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWords.java @@ -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[] payload() default {}; } \ No newline at end of file diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWordValidator.java b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWordsValidator.java similarity index 54% rename from continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWordValidator.java rename to continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWordsValidator.java index 27239ace..2303cfaf 100644 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/validate/SensitiveWordValidator.java +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/java/top/continew/starter/security/sensitivewords/validation/SensitiveWordsValidator.java @@ -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 { +/** + * 敏感词校验器 + * + * @author luoqiz + * @author Charles7c + * @since 2.9.0 + */ +public class SensitiveWordsValidator implements ConstraintValidator { @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 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; diff --git a/continew-starter-security/continew-starter-security-sensitivewords/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-security/continew-starter-security-sensitivewords/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..83ecab86 --- /dev/null +++ b/continew-starter-security/continew-starter-security-sensitivewords/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.continew.starter.security.sensitivewords.autoconfigure.SensitiveWordsAutoConfiguration \ No newline at end of file diff --git a/continew-starter-security/pom.xml b/continew-starter-security/pom.xml index 1a0f4f54..66be5684 100644 --- a/continew-starter-security/pom.xml +++ b/continew-starter-security/pom.xml @@ -18,6 +18,7 @@ continew-starter-security-mask continew-starter-security-crypto continew-starter-security-limiter + continew-starter-security-sensitivewords diff --git a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsAutoConfiguration.java b/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsAutoConfiguration.java deleted file mode 100644 index 0ca5a3b7..00000000 --- a/continew-starter-sensitive-words/src/main/java/top/continew/starter/sensitive/words/autoconfigure/SensitiveWordsAutoConfiguration.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - *

- * 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 - *

- * http://www.gnu.org/licenses/lgpl.html - *

- * 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."); - } -} diff --git a/continew-starter-sensitive-words/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-sensitive-words/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports deleted file mode 100644 index 72906200..00000000 --- a/continew-starter-sensitive-words/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ /dev/null @@ -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 \ No newline at end of file diff --git a/continew-starter-sensitive-words/src/test/java/top/continew/starter/sensitive/words/SensitiveWordsTest.java b/continew-starter-sensitive-words/src/test/java/top/continew/starter/sensitive/words/SensitiveWordsTest.java deleted file mode 100644 index 08a13561..00000000 --- a/continew-starter-sensitive-words/src/test/java/top/continew/starter/sensitive/words/SensitiveWordsTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - *

- * 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 - *

- * http://www.gnu.org/licenses/lgpl.html - *

- * 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 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()); - } -} diff --git a/pom.xml b/pom.xml index 9076a968..cd3a2843 100644 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,6 @@ continew-starter-auth continew-starter-messaging continew-starter-extension - continew-starter-sensitive-words