generated from root/miduo_server
init
parent
adca6e992b
commit
22428c74b2
|
|
@ -0,0 +1,23 @@
|
|||
*.iml
|
||||
.idea
|
||||
.gradle
|
||||
*.log
|
||||
*.xml
|
||||
*.bin
|
||||
*.lock
|
||||
serverlogic/src/main/java/com/ljsd/jieling/protocols
|
||||
gamecommon/src/main/java/rpc/global
|
||||
gamecommon/src/main/java/rpc/world
|
||||
gamecommon/src/main/java/rpc/protocols
|
||||
*.class
|
||||
out
|
||||
build
|
||||
out
|
||||
tmp/
|
||||
luafight/LogError/*.txt
|
||||
luafight/LogError/*.json
|
||||
luafight/BattleRecord/*.txt
|
||||
luafight/BattleRecord/*.json
|
||||
serverlogic/src/main/com.ljsd.jieling.thrift
|
||||
conf/server/*.txt
|
||||
serverlogic/src/main/resources/application.properties
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group 'SensitivewordFilter'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
compile("org.apache.thrift:libthrift:0.9.2")
|
||||
compile("ch.qos.logback:logback-core:1.1.11")
|
||||
compile("ch.qos.logback:logback-classic:1.1.11")
|
||||
compile("org.springframework.boot:spring-boot:1.5.9.RELEASE")
|
||||
compile("org.springframework.boot:spring-boot-starter-test:1.5.9.RELEASE")
|
||||
compile("org.springframework.boot:spring-boot-starter-data-redis:1.5.9.RELEASE")
|
||||
}
|
||||
Binary file not shown.
|
|
@ -0,0 +1,5 @@
|
|||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
|
@ -0,0 +1,172 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
APP_ARGS=$(save "$@")
|
||||
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
|
||||
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
|
||||
cd "$(dirname "$0")"
|
||||
fi
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
@if "%DEBUG%" == "" @echo off
|
||||
@rem ##########################################################################
|
||||
@rem
|
||||
@rem Gradle startup script for Windows
|
||||
@rem
|
||||
@rem ##########################################################################
|
||||
|
||||
@rem Set local scope for the variables with windows NT shell
|
||||
if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%" == "" set DIRNAME=.
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
set DEFAULT_JVM_OPTS=
|
||||
|
||||
@rem Find java.exe
|
||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if "%ERRORLEVEL%" == "0" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto init
|
||||
|
||||
echo.
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
echo.
|
||||
echo Please set the JAVA_HOME variable in your environment to match the
|
||||
echo location of your Java installation.
|
||||
|
||||
goto fail
|
||||
|
||||
:init
|
||||
@rem Get command-line arguments, handling Windows variants
|
||||
|
||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
||||
|
||||
:win9xME_args
|
||||
@rem Slurp the command line arguments.
|
||||
set CMD_LINE_ARGS=
|
||||
set _SKIP=2
|
||||
|
||||
:win9xME_args_slurp
|
||||
if "x%~1" == "x" goto execute
|
||||
|
||||
set CMD_LINE_ARGS=%*
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
|
||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||
|
||||
@rem Execute Gradle
|
||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
||||
|
||||
:end
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||
|
||||
:fail
|
||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||
rem the _cmd.exe /c_ return code!
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
||||
:omega
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
rootProject.name = 'SensitivewordFilter'
|
||||
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.ljsd;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class CoreService implements com.ljsd.jieling.thrift.idl.RPCRequestIFace.Iface{
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CoreService.class);
|
||||
@Override
|
||||
public boolean isContaintSensitiveWord(String txt, int matchType) throws TException {
|
||||
boolean containtSensitiveWord = SensitivewordFilter.isContaintSensitiveWord(txt, matchType);
|
||||
return containtSensitiveWord;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSensitiveWord(String txt, int matchType) throws TException {
|
||||
Set<String> sensitiveWordList = new HashSet<>();
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
int length = SensitivewordFilter.CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符
|
||||
if (length > 0) { //存在,加入list中
|
||||
sensitiveWordList.add(txt.substring(i, i + length));
|
||||
i = i + length - 1; //减1的原因,是因为for会自增
|
||||
}
|
||||
}
|
||||
return sensitiveWordList;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package com.ljsd;
|
||||
|
||||
|
||||
import com.ljsd.config.CoreSettings;
|
||||
import org.apache.thrift.TProcessor;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.apache.thrift.server.TServer;
|
||||
import org.apache.thrift.server.TThreadPoolServer;
|
||||
import org.apache.thrift.transport.TServerSocket;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class RPCServerTask extends Thread {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RPCServerTask.class);
|
||||
|
||||
private CoreSettings coreSettings;
|
||||
|
||||
public RPCServerTask(CoreSettings coreSettings) {
|
||||
setName("rpc-sensitive");
|
||||
this.coreSettings = coreSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
TServerSocket serverTransport = new TServerSocket(new InetSocketAddress(coreSettings.getIp(), coreSettings.getPort()));
|
||||
TThreadPoolServer.Args trArgs = new TThreadPoolServer.Args(serverTransport);
|
||||
TProcessor processor = new com.ljsd.jieling.thrift.idl.RPCRequestIFace.Processor(new CoreService());
|
||||
trArgs.processor(processor);
|
||||
TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(true, true);
|
||||
trArgs.protocolFactory(proFactory);
|
||||
trArgs.maxWorkerThreads(4);
|
||||
trArgs.executorService(Executors.newFixedThreadPool(4));
|
||||
LOGGER.info("run->敏感字校验服 services is starting.");
|
||||
final TServer server = new TThreadPoolServer(trArgs);
|
||||
while (true) {
|
||||
try {
|
||||
server.serve();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("run->{}", e.getMessage(), e);
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (TTransportException e) {
|
||||
LOGGER.error("run->{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package com.ljsd;
|
||||
|
||||
import com.ljsd.config.CoreServerAutoConfiguration;
|
||||
import com.ljsd.config.CoreSettings;
|
||||
import com.ljsd.config.RedisAutoConfiguration;
|
||||
import com.ljsd.config.RedisProperties;
|
||||
import com.ljsd.util.RedisUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
@EnableScheduling
|
||||
public class SensitiveWordApplication {
|
||||
private static SpringApplication app = null;
|
||||
private static ConfigurableApplicationContext configurableApplicationContext = null;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordApplication.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SensitivewordFilter.init();
|
||||
app = new SpringApplication(SensitiveWordApplication.class);
|
||||
app.setRegisterShutdownHook(false);
|
||||
app.setWebEnvironment(false);
|
||||
configurableApplicationContext = app.run(args);
|
||||
RedisAutoConfiguration redisAutoConfiguration = configurableApplicationContext.getBean(RedisAutoConfiguration.class);
|
||||
RedisProperties redisProperties = redisAutoConfiguration.getRedisProperties();
|
||||
RedisUtil.getInstence().init(redisProperties);
|
||||
CoreServerAutoConfiguration coreServerAutoConfiguration = configurableApplicationContext.getBean(CoreServerAutoConfiguration.class);
|
||||
CoreSettings coreSettings = coreServerAutoConfiguration.getConfig();
|
||||
String adress = coreSettings.getIp() + "#" + coreSettings.getPort();
|
||||
RedisUtil.getInstence().redisSet(RedisUtil.SENSIT_WORD_FILTER_KEY, adress);
|
||||
new RPCServerTask(coreSettings).start();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,141 @@
|
|||
package com.ljsd;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Description: 初始化敏感词库,将敏感词加入到HashMap中,构建DFA算法模型
|
||||
* @version 1.0
|
||||
*/
|
||||
public class SensitiveWordInit {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordInit.class);
|
||||
private static String ENCODING = "UTF-8"; //字符编码
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static HashMap sensitiveWordMap;
|
||||
|
||||
public SensitiveWordInit(){
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static Map initKeyWord(){
|
||||
try {
|
||||
//读取敏感词库
|
||||
Set<String> keyWordSet = readSensitiveWordFile();
|
||||
//将敏感词库加入到HashMap中
|
||||
addSensitiveWordToHashMap(keyWordSet);
|
||||
//spring获取application,然后application.setAttribute("sensitiveWordMap",sensitiveWordMap);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("initKeyWord->msg={}", e.getMessage(), e);
|
||||
}
|
||||
return sensitiveWordMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
|
||||
* 中 = {
|
||||
* isEnd = 0
|
||||
* 国 = {<br>
|
||||
* isEnd = 1
|
||||
* 人 = {isEnd = 0
|
||||
* 民 = {isEnd = 1}
|
||||
* }
|
||||
* 男 = {
|
||||
* isEnd = 0
|
||||
* 人 = {
|
||||
* isEnd = 1
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* 五 = {
|
||||
* isEnd = 0
|
||||
* 星 = {
|
||||
* isEnd = 0
|
||||
* 红 = {
|
||||
* isEnd = 0
|
||||
* 旗 = {
|
||||
* isEnd = 1
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @param keyWordSet 敏感词库
|
||||
* @version 1.0
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private static void addSensitiveWordToHashMap(Set<String> keyWordSet) {
|
||||
sensitiveWordMap = new HashMap(keyWordSet.size()); //初始化敏感词容器,减少扩容操作
|
||||
String key = null;
|
||||
Map nowMap = null;
|
||||
Map<String, String> newWorMap = null;
|
||||
//迭代keyWordSet
|
||||
Iterator<String> iterator = keyWordSet.iterator();
|
||||
while(iterator.hasNext()){
|
||||
key = iterator.next(); //关键字
|
||||
nowMap = sensitiveWordMap;
|
||||
for(int i = 0 ; i < key.length() ; i++){
|
||||
char keyChar = key.charAt(i); //转换成char型
|
||||
Object wordMap = nowMap.get(keyChar); //获取
|
||||
|
||||
if(wordMap != null){ //如果存在该key,直接赋值
|
||||
nowMap = (Map) wordMap;
|
||||
}
|
||||
else{ //不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
|
||||
newWorMap = new HashMap<String,String>();
|
||||
newWorMap.put("isEnd", "0"); //不是最后一个
|
||||
nowMap.put(keyChar, newWorMap);
|
||||
nowMap = newWorMap;
|
||||
}
|
||||
|
||||
if(i == key.length() - 1){
|
||||
nowMap.put("isEnd", "1"); //最后一个
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取敏感词库中的内容,将内容添加到set集合中
|
||||
* @return
|
||||
* @version 1.0
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
private static Set<String> readSensitiveWordFile() throws Exception{
|
||||
Set<String> set = null;
|
||||
InputStream is = SensitiveWordInit.class.getClassLoader().getResourceAsStream("mingan.txt");
|
||||
InputStreamReader read = new InputStreamReader(is,ENCODING);
|
||||
BufferedReader bufferedReader =null;
|
||||
try {
|
||||
if(is!=null){ //文件流是否存在
|
||||
set = new HashSet<String>();
|
||||
bufferedReader = new BufferedReader(read);
|
||||
String txt = null;
|
||||
while((txt = bufferedReader.readLine()) != null){ //读取文件,将文件内容放入到set中
|
||||
set.add(txt);
|
||||
}
|
||||
}
|
||||
else{ //不存在抛出异常信息
|
||||
throw new Exception("敏感词库文件不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("readSensitiveWordFile->msg={}", e.getMessage(), e);
|
||||
}finally{
|
||||
if( bufferedReader!=null){
|
||||
bufferedReader.close();
|
||||
}
|
||||
is.close();
|
||||
read.close(); //关闭文件流
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
package com.ljsd;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Description: 敏感词过滤
|
||||
*/
|
||||
public class SensitivewordFilter {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Map sensitiveWordMap = null;
|
||||
public static final int minMatchTYpe = 1; //最小匹配规则
|
||||
public static final int maxMatchType = 2; //最大匹配规则
|
||||
|
||||
/**
|
||||
* 构造函数,初始化敏感词库
|
||||
*/
|
||||
public SensitivewordFilter() {}
|
||||
|
||||
public static void init(){
|
||||
sensitiveWordMap = SensitiveWordInit.initKeyWord();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断文字是否包含敏感字符
|
||||
*
|
||||
* @param txt 文字
|
||||
* @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
|
||||
* @return 若包含返回true,否则返回false
|
||||
* @version 1.0
|
||||
*/
|
||||
public static boolean isContaintSensitiveWord(String txt, int matchType) {
|
||||
boolean flag = false;
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
int matchFlag = CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符
|
||||
if (matchFlag > 0) { //大于0存在,返回true
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文字中的敏感词
|
||||
*
|
||||
* @param txt 文字
|
||||
* @param matchType 匹配规则 1:最小匹配规则,2:最大匹配规则
|
||||
* @return
|
||||
* @version 1.0
|
||||
*/
|
||||
public static Set<String> getSensitiveWord(String txt, int matchType) {
|
||||
Set<String> sensitiveWordList = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
int length = CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符
|
||||
if (length > 0) { //存在,加入list中
|
||||
sensitiveWordList.add(txt.substring(i, i + length));
|
||||
i = i + length - 1; //减1的原因,是因为for会自增
|
||||
}
|
||||
}
|
||||
|
||||
return sensitiveWordList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换敏感字字符
|
||||
*
|
||||
* @param txt
|
||||
* @param matchType
|
||||
* @param replaceChar 替换字符,默认*
|
||||
* @version 1.0
|
||||
*/
|
||||
public static String replaceSensitiveWord(String txt, int matchType, String replaceChar) {
|
||||
String resultTxt = txt;
|
||||
Set<String> set = getSensitiveWord(txt, matchType); //获取所有的敏感词
|
||||
Iterator<String> iterator = set.iterator();
|
||||
String word = null;
|
||||
String replaceString = null;
|
||||
while (iterator.hasNext()) {
|
||||
word = iterator.next();
|
||||
replaceString = getReplaceChars(replaceChar, word.length());
|
||||
resultTxt = resultTxt.replaceAll(word, replaceString);
|
||||
}
|
||||
|
||||
return resultTxt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取替换字符串
|
||||
*
|
||||
* @param replaceChar
|
||||
* @param length
|
||||
* @return
|
||||
* @version 1.0
|
||||
*/
|
||||
private static String getReplaceChars(String replaceChar, int length) {
|
||||
StringBuilder bulder = new StringBuilder();
|
||||
bulder.append(replaceChar);
|
||||
for(int i = 1 ; i < length ; i++){
|
||||
bulder.append(replaceChar);
|
||||
}
|
||||
return bulder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文字中是否包含敏感字符,检查规则如下:<br>
|
||||
*
|
||||
* @param txt
|
||||
* @param beginIndex
|
||||
* @param matchType
|
||||
* @return,如果存在,则返回敏感词字符的长度,不存在返回0
|
||||
* @version 1.0
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public static int CheckSensitiveWord(String txt, int beginIndex, int matchType) {
|
||||
boolean flag = false; //敏感词结束标识位:用于敏感词只有1位的情况
|
||||
int matchFlag = 0; //匹配标识数默认为0
|
||||
char word = 0;
|
||||
Map nowMap = sensitiveWordMap;
|
||||
for (int i = beginIndex; i < txt.length(); i++) {
|
||||
word = txt.charAt(i);
|
||||
nowMap = (Map) nowMap.get(word); //获取指定key
|
||||
if (nowMap != null) { //存在,则判断是否为最后一个
|
||||
matchFlag++; //找到相应key,匹配标识+1
|
||||
if ("1".equals(nowMap.get("isEnd"))) { //如果为最后一个匹配规则,结束循环,返回匹配标识数
|
||||
flag = true; //结束标志位为true
|
||||
if (SensitivewordFilter.minMatchTYpe == matchType) { //最小规则,直接返回,最大规则还需继续查找
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else { //不存在,直接返回
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matchFlag < 1 || !flag) { //长度大于等于1,为词
|
||||
matchFlag = 0;
|
||||
}
|
||||
return matchFlag;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package com.ljsd;
|
||||
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ShieldedWordUtils {
|
||||
|
||||
|
||||
public static boolean checkName(String name,boolean validateSpecStr){
|
||||
boolean result = true;
|
||||
if(validateSpecStr){
|
||||
result = validateUserName(name);
|
||||
if(!result){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
result = isContaintSensitiveWord(name);
|
||||
if(result){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数
|
||||
*
|
||||
* @param userName
|
||||
* @return
|
||||
* @author www.zuidaima.com
|
||||
*/
|
||||
private static boolean validateUserName(String userName) {
|
||||
String validateStr = "^[\\\\w\\\\--_[A-Za-z0-9]\\u4e00-\\u9fa5\\uFF21-\\uFF3A\\uFF41-\\uFF5A]+$";
|
||||
return matcher(validateStr, userName);
|
||||
}
|
||||
|
||||
private static boolean matcher(String reg, String string) {
|
||||
Pattern pattern = Pattern.compile(reg);
|
||||
Matcher matcher = pattern.matcher(string);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
private static boolean isContaintSensitiveWord(String userName){
|
||||
boolean hasDirtyWords = SensitivewordFilter.isContaintSensitiveWord(userName, 1);
|
||||
return hasDirtyWords;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.ljsd.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2016/3/29.
|
||||
*/
|
||||
@Configuration(value = "CoreServerAutoConfiguration")
|
||||
@EnableConfigurationProperties({CoreSettings.class})
|
||||
public class CoreServerAutoConfiguration {
|
||||
@Autowired
|
||||
private CoreSettings coreSettings;
|
||||
|
||||
public CoreSettings getConfig(){
|
||||
return coreSettings;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package com.ljsd.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2016/3/29.
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "services.core")
|
||||
public class CoreSettings {
|
||||
|
||||
private String ip;
|
||||
private int port;
|
||||
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.ljsd.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2016/3/17.
|
||||
*/
|
||||
@Configuration(value = "redisAutoConfiguration")
|
||||
@EnableConfigurationProperties(RedisProperties.class)
|
||||
public class RedisAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private RedisProperties redisProperties;
|
||||
|
||||
public RedisProperties getRedisProperties() {
|
||||
return redisProperties;
|
||||
}
|
||||
|
||||
public void setRedisProperties(RedisProperties redisProperties) {
|
||||
this.redisProperties = redisProperties;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package com.ljsd.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Created by Administrator on 2016/3/17.
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.redis")
|
||||
public class RedisProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private String password;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
package com.ljsd.util;
|
||||
|
||||
import com.ljsd.config.RedisProperties;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class RedisUtil {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
|
||||
private static final int MAX_TRY_TIMES = 3; //最大尝试次数,保障获取/存储成功
|
||||
private static final int FAILED_SLEEP = 2; //每次失败最大停顿时间
|
||||
|
||||
public static final String SENSIT_WORD_FILTER_KEY = "SENSIT_WORD_FILTER_KEY";
|
||||
|
||||
private RedisUtil() {
|
||||
}
|
||||
|
||||
public static RedisUtil getInstence() {
|
||||
return InnerClass.instance;
|
||||
}
|
||||
|
||||
private static class InnerClass {
|
||||
private static final RedisUtil instance = new RedisUtil();
|
||||
}
|
||||
|
||||
private StringRedisTemplate redisObjectTemplate;
|
||||
|
||||
public void init(RedisProperties redisProperties) {
|
||||
// 设置参数
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||
config.setBlockWhenExhausted(true);
|
||||
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
|
||||
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
|
||||
//是否启用pool的jmx管理功能, 默认true
|
||||
config.setJmxEnabled(true);
|
||||
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默 认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
|
||||
config.setJmxNamePrefix("pool");
|
||||
//是否启用后进先出, 默认true
|
||||
config.setLifo(true);
|
||||
//最大空闲连接数, 默认8个
|
||||
config.setMaxIdle(100);
|
||||
//最大连接数, 默认8个
|
||||
config.setMaxTotal(1000);
|
||||
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
||||
config.setMaxWaitMillis(-1);
|
||||
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
|
||||
config.setMinEvictableIdleTimeMillis(1800000);
|
||||
//最小空闲连接数, 默认0
|
||||
config.setMinIdle(100);
|
||||
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
|
||||
config.setNumTestsPerEvictionRun(3);
|
||||
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
|
||||
config.setSoftMinEvictableIdleTimeMillis(1800000);
|
||||
//在获取连接的时候检查有效性, 默认false
|
||||
config.setTestOnBorrow(false);
|
||||
//在空闲时检查有效性, 默认false
|
||||
config.setTestWhileIdle(false);
|
||||
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
||||
config.setTimeBetweenEvictionRunsMillis(-1);
|
||||
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(config);
|
||||
//设置host
|
||||
connectionFactory.setHostName(redisProperties.getHost());
|
||||
//设置端口
|
||||
connectionFactory.setPort(redisProperties.getPort());
|
||||
//设置密码
|
||||
connectionFactory.setPassword(redisProperties.getPassword());
|
||||
//初始化connectionFactory
|
||||
connectionFactory.afterPropertiesSet();
|
||||
//实例化
|
||||
redisObjectTemplate = new StringRedisTemplate(connectionFactory);
|
||||
}
|
||||
|
||||
public String redisGetValue(String key) {
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
return redisObjectTemplate.opsForValue().get(key);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("redisGetValue->i={},key={},emsg={}", i, key, e.getMessage(), e);
|
||||
try {
|
||||
Thread.sleep(FAILED_SLEEP);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void redisSet(String key, String value) {
|
||||
for (int i = 0; i < MAX_TRY_TIMES; i++) {
|
||||
try {
|
||||
redisObjectTemplate.opsForValue().set(key, value);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("redisSet->i={},key={},value={},emsg={}", i, key, value, e.getMessage(), e);
|
||||
try {
|
||||
Thread.sleep(FAILED_SLEEP);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
echo off
|
||||
echo "======first del java src files======"
|
||||
echo del D:\SensitivewordFilter\src\main\thrift\idl\gen-java\com\ljsd\xianxia\thrift\idl\*.java
|
||||
echo del D:\SensitivewordFilter\src\main\java\com\ljsd\xianxia\thrift\idl\*.java
|
||||
|
||||
echo "======gen java src files======"
|
||||
for %%i in (".\*.thrift") do thrift-0.9.2 --gen java %%i
|
||||
echo "=======begin to copy src files======"
|
||||
echo copy .\gen-java\com\ljsd\xianxia\thrift\idl\*.java D:\SensitivewordFilter\src\main\java\com\ljsd\xianxia\thrift\idl\ /y
|
||||
echo on
|
||||
pause
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
|
|
@ -0,0 +1,13 @@
|
|||
namespace java com.ljsd.jieling.thrift.idl
|
||||
|
||||
/**
|
||||
* Structs can also be exceptions, if they are nasty.
|
||||
*/
|
||||
|
||||
service RPCRequestIFace{
|
||||
// 判断文字是否包含敏感字符
|
||||
bool isContaintSensitiveWord(1:string txt, 2:i32 matchType);
|
||||
// 获取文字中的敏感词
|
||||
set<string> getSensitiveWord(1:string txt, 2:i32 matchType);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
|
@ -0,0 +1,8 @@
|
|||
# redis config
|
||||
spring.redis.host=60.1.1.21
|
||||
spring.redis.port=6379
|
||||
spring.redis.password=
|
||||
|
||||
#core
|
||||
services.core.ip=60.1.1.221
|
||||
services.core.port=5900
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue