release branch 0.0.15

This commit is contained in:
binbin.hou
2021-07-16 15:33:46 +08:00
parent 56decce3d9
commit f8bdf1d22e
14 changed files with 323 additions and 14 deletions

View File

@@ -68,7 +68,10 @@ public class SensitiveWordBs {
List<String> results = CollectionUtil.difference(denyList, allowList);
// 初始化 DFA 信息
sensitiveWordMap = new SensitiveWordMap();
if(sensitiveWordMap == null) {
sensitiveWordMap = new SensitiveWordMap();
}
// 便于可以多次初始化
sensitiveWordMap.initWordMap(results);
}
@@ -321,8 +324,13 @@ public class SensitiveWordBs {
* @since 0.0.13
*/
private void statusCheck(){
//DLC
if(sensitiveWordMap == null) {
this.init();
synchronized (this) {
if(sensitiveWordMap == null) {
this.init();
}
}
}
}

View File

@@ -50,15 +50,10 @@ public class SensitiveWordMap implements IWordMap {
*/
@Override
@SuppressWarnings("unchecked")
public void initWordMap(Collection<String> collection) {
// 避免重复加载
if (MapUtil.isNotEmpty(innerWordMap)) {
return;
}
public synchronized void initWordMap(Collection<String> collection) {
long startTime = System.currentTimeMillis();
// 避免扩容带来的消耗
innerWordMap = new HashMap(collection.size());
Map newInnerWordMap = new HashMap(collection.size());
for (String key : collection) {
if (StringUtil.isEmpty(key)) {
@@ -70,7 +65,7 @@ public class SensitiveWordMap implements IWordMap {
final int size = chars.length;
// 每一个新词的循环,直接将结果设置为当前 map所有变化都会体现在结果的 map 中
Map currentMap = innerWordMap;
Map currentMap = newInnerWordMap;
for (int i = 0; i < size; i++) {
// 截取敏感词当中的字在敏感词库中字为HashMap对象的Key键值
@@ -101,6 +96,9 @@ public class SensitiveWordMap implements IWordMap {
}
}
// 最后更新为新的 map保证更新过程中旧的数据可用
this.innerWordMap = newInnerWordMap;
long endTime = System.currentTimeMillis();
System.out.println("Init sensitive word map end! Cost time: " + (endTime - startTime) + "ms");
}

View File

@@ -0,0 +1,40 @@
package com.github.houbb.sensitive.word.spring;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.spring.annotation.Autowired;
import com.github.houbb.sensitive.word.spring.annotation.Bean;
import com.github.houbb.sensitive.word.spring.annotation.Configuration;
import com.github.houbb.sensitive.word.spring.database.MyDdWordAllow;
import com.github.houbb.sensitive.word.spring.database.MyDdWordDeny;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Configuration
public class SpringSensitiveWordConfig {
@Autowired
private MyDdWordAllow myDdWordAllow;
@Autowired
private MyDdWordDeny myDdWordDeny;
/**
* 初始化引导类
* @return 初始化引导类
* @since 1.0.0
*/
@Bean
public SensitiveWordBs sensitiveWordBs() {
SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
.wordAllow(WordAllows.chains(WordAllows.system(), myDdWordAllow))
.wordDeny(myDdWordDeny)
// 各种其他配置
.init();
return sensitiveWordBs;
}
}

View File

@@ -0,0 +1,18 @@
package com.github.houbb.sensitive.word.spring.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
boolean required() default true;
}

View File

@@ -0,0 +1,24 @@
package com.github.houbb.sensitive.word.spring.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Bean {
String[] value() default {};
String[] name() default {};
String initMethod() default "";
String destroyMethod() default "";
}

View File

@@ -0,0 +1,15 @@
package com.github.houbb.sensitive.word.spring.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
}

View File

@@ -0,0 +1,31 @@
package com.github.houbb.sensitive.word.spring.annotation;
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}

View File

@@ -0,0 +1,22 @@
package com.github.houbb.sensitive.word.spring.database;
import com.github.houbb.sensitive.word.api.IWordAllow;
import com.github.houbb.sensitive.word.spring.annotation.Component;
import java.util.Arrays;
import java.util.List;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Component
public class MyDdWordAllow implements IWordAllow {
@Override
public List<String> allow() {
// 数据库查询
return Arrays.asList("学习");
}
}

View File

@@ -0,0 +1,22 @@
package com.github.houbb.sensitive.word.spring.database;
import com.github.houbb.sensitive.word.api.IWordDeny;
import com.github.houbb.sensitive.word.spring.annotation.Component;
import java.util.Arrays;
import java.util.List;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Component
public class MyDdWordDeny implements IWordDeny {
@Override
public List<String> deny() {
// 数据库返回的各种信息
return Arrays.asList("广告");
}
}

View File

@@ -0,0 +1,41 @@
package com.github.houbb.sensitive.word.spring.service;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.spring.annotation.Autowired;
import com.github.houbb.sensitive.word.spring.annotation.Component;
/**
* @author binbin.hou
* @since 1.0.0
*/
@Component
public class SensitiveWordService {
@Autowired
private SensitiveWordBs sensitiveWordBs;
/**
* 更新词库
*
* 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法。
* 如果需要生效,则调用这个方法。
*
* 说明:重新初始化不影响旧的方法使用。初始化完成后,会以新的为准。
*/
public void refresh() {
// 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法,然后调用这个方法。
sensitiveWordBs.init();
}
/**
* 是否包含
*
* 可以重新封装,也可以直接使用 sensitiveWordBs
* @param word 单词
* @return 结果
*/
public boolean contains(String word){
return sensitiveWordBs.contains(word);
}
}