1、https://github.com/vdurmont/emoji-java
pom中引入依赖
2、编写拦截器
public class AuthInterceptorimplements HandlerInterceptor {
private static final Loggerlog = LoggerFactory.getLogger(AuthInterceptor.class);
/* */
private static final StringcontentType ="application/json;charset=utf-8";
/**
*
* 在业务处理器处理请求之前被调用,在该方法中对用户请求request进行处理
*
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String contxtPath = request.getContextPath();
String requestUrl = request.getRequestURI().replace(contxtPath, "");
if (log.isDebugEnabled()) {
log.debug("preHandle:{}", requestUrl);
}
//emoji判断
boolean emoji = existsEmoji(request);
if (emoji) {
JSONObject json =new JSONObject();
json.put("status", CodeConts.PARAM_LEGAL);
json.put("message", "包含表情");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
return false;
}
private boolean existsEmoji(HttpServletRequest request)
throws IOException {
Map parameterMap = request.getParameterMap();
Set> keSet = parameterMap.entrySet();
for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
Map.Entry me = (Map.Entry) itr.next();
Object ov = me.getValue();
String[] value =new String[1];
if (ovinstanceof String[]) {
value = (String[]) ov;
for (String s : value) {
List list = EmojiParser.extractEmojis(s);
if (list.size()>0) {
return true;
}
}
}else {
List list = EmojiParser.extractEmojis(ov.toString());
if (list.size()>0) {
return true;
}
}
}
return false;
}
}
preHandle方法中编写
3、SpringMVC中配置拦截器
网友评论