美文网首页
emoji表情拦截

emoji表情拦截

作者: 楊大大大侠 | 来源:发表于2018-04-18 17:54 被阅读0次

    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中配置拦截器

    相关文章

      网友评论

          本文标题:emoji表情拦截

          本文链接:https://www.haomeiwen.com/subject/cnkbkftx.html