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";
/**
*
* 在業(yè)務(wù)處理器處理請求之前被調(diào)用,在該方法中對用戶請求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中配置攔截器
