拦截器拦截emoji

This commit is contained in:
liuyingfang 2023-04-14 15:49:21 +08:00
parent 4446877297
commit 9770e1f72a
4 changed files with 129 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package cn.pluss.platform.configurer;
import cn.pluss.platform.configurer.extend.CurrentUserMethodArgumentResolver;
import cn.pluss.platform.configurer.extend.FastJsonHttpMessageConverterEx;
import cn.pluss.platform.interceptor.EmojiInterceptor;
import cn.pluss.platform.interceptor.RequestInfoInterceptor;
import cn.pluss.platform.interceptor.TokenRegistryInterceptor;
import org.springframework.context.annotation.Bean;
@ -72,6 +73,7 @@ public class WebConfigurer implements WebMvcConfigurer {
registry.addInterceptor(new RequestInfoInterceptor());
registry.addInterceptor(tokenRegistryInterceptor()).excludePathPatterns(excludes);
registry.addInterceptor(emojiInterceptor()).excludePathPatterns(excludes);
}
@Bean
@ -88,4 +90,8 @@ public class WebConfigurer implements WebMvcConfigurer {
public FastJsonHttpMessageConverterEx fastJsonHttpMessageConverterEx() {
return new FastJsonHttpMessageConverterEx();
}
@Bean
public EmojiInterceptor emojiInterceptor() {
return new EmojiInterceptor();
}
}

View File

@ -0,0 +1,32 @@
package cn.pluss.platform.interceptor;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.Emoji;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
/**
* @author lyf
*/
public class EmojiInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String body = sb.toString();
boolean emoji = Emoji.containsEmoji(body);
if (emoji) {
throw new MsgException("请正确输入");//这里是我自己封装了一个异常,根据自己需要写自己的异常
}
return super.preHandle(request, response, handler);
}
}

View File

@ -0,0 +1,91 @@
package cn.pluss.platform.util;
import org.apache.commons.lang3.StringUtils;
public class Emoji {
/**
* @Description: 是否有表情判断
* @author zhaopeng
* @email zp152527@163.com
* @date 2018/8/24 18:27
*/
public static boolean containsEmoji(String source) {
int len = source.length();
boolean isEmoji = false;
for (int i = 0; i < len; i++) {
char hs = source.charAt(i);
if (0xd800 <= hs && hs <= 0xdbff) {
if (source.length() > 1) {
char ls = source.charAt(i + 1);
int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
return true;
}
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
return true;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
return true;
} else if (0x2934 <= hs && hs <= 0x2935) {
return true;
} else if (0x3297 <= hs && hs <= 0x3299) {
return true;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
|| hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
|| hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
return true;
}
if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
char ls = source.charAt(i + 1);
if (ls == 0x20e3) {
return true;
}
}
}
}
return isEmoji;
}
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* @Description: 过滤emoji 或者 其他非文字类型的字符
* @author zhaopeng
* @email zp152527@163.com
* @date 2018/8/24 18:27
*/
public static String filterEmoji(String source) {
if (StringUtils.isBlank(source)) {
return source;
}
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
}
if (buf == null) {
return source;
} else {
if (buf.length() == len) {
buf = null;
return source;
} else {
return buf.toString();
}
}
}
}

View File

@ -73,7 +73,6 @@ public class IdCard implements Serializable {
*/
@NotEmpty(message = "请输入证件号", groups = {IdCard.Common.class})
@TableField("certNo")
@NotEmpty(message = "证件号未填写")
private String certNo;
@ -82,7 +81,6 @@ public class IdCard implements Serializable {
*/
@NotEmpty(message = "请输入证件人名称", groups = {IdCard.Common.class})
@TableField("certName")
@NotEmpty(message = "证件姓名未填写")
private String certName;