最近在做一个微信相关的网站,很多地方涉及到微信表情的输入,导致内容无法插入到数据库,虽然有用到一个表情过滤的工具类,但是需要过滤的地方比较多,于是想到在过滤器中过滤用户请求的内容。
request这个方法里,有getParameter(String name)、getParameterValues(String name)、getParameterNames()以及getParameterMap()是可以获得参数键值对的,Servlet自带的HttpServletRequest的方法是不支持修改用户请求类型的,我也尝试过直接去修改,都以异常收尾,后面我想到HttpServletRequest既然是一个接口,那我去实现它,然后直接用我们实现的类传到filterChain.doFilter(servletRequest, servletResponse)里面不也可以么,可是要实现的方法也太多了,好在ServletApi提供了一个class HttpServletRequestWrapper封装了这些方法,我们可以通过extends这个类,然后重写一些重要的方法来修改用户的请求,下面就开始我们的改造吧。
1.新建一个Filter,我给这个类命名为RequestFilter
public class RequestFilter implements Filter { private final Logger logger = LoggerFactory.getLogger(RequestWrapper.class); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { } @Override public void destroy() { } }
2.新建一个类,通过继承HttpServletRequestWrapper来实现request的作用。
class RequestWrapper extends HttpServletRequestWrapper { }
3.新建一个private属性:private Map<String, String[]> paramMap;用这个paramMap来存储用户请求的内容,并且在构造方法中初始化paramMap,初始化的方法为
paramMap = new HashMap<>();//定义 paramMap.putAll(request.getParameterMap());//初始化
4.重写上面提到的四个方法,
@Override public String getParameter(String name) { String[] values = paramMap.get(name); if (values == null || values.length == 0) { return null; } return values[0]; } @Override public String[] getParameterValues(String name) { return paramMap.get(name); } @Override public Enumeration<String> getParameterNames() { return new Enumeration<String>() { private Iterator<String> iterator = paramMap.keySet().iterator(); @Override public boolean hasMoreElements() { return iterator.hasNext(); } @Override public String nextElement() { return iterator.next(); } }; } @Override public Map<String, String[]> getParameterMap() { return paramMap; }
5.通过以上这些操作,我们就可以实现对用户请求进行修改。
public class RequestFilter implements Filter { private final Logger logger = LoggerFactory.getLogger(RequestWrapper.class); @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; RequestWrapper requestWrapper = new RequestWrapper(request); if (request.getMethod().equals("POST")) { logger.debug("开始过滤请求中的表情字符"); requestWrapper.emojiFilter(); } filterChain.doFilter(requestWrapper, servletResponse); } @Override public void destroy() { } class RequestWrapper extends HttpServletRequestWrapper { private Map<String, String[]> paramMap; RequestWrapper(HttpServletRequest request) { super(request); paramMap = new HashMap<>(); paramMap.putAll(request.getParameterMap()); } @Override public String getParameter(String name) { String[] values = paramMap.get(name); if (values == null || values.length == 0) { return null; } return values[0]; } @Override public String[] getParameterValues(String name) { return paramMap.get(name); } @Override public Enumeration<String> getParameterNames() { return new Enumeration<String>() { private Iterator<String> iterator = paramMap.keySet().iterator(); @Override public boolean hasMoreElements() { return iterator.hasNext(); } @Override public String nextElement() { return iterator.next(); } }; } @Override public Map<String, String[]> getParameterMap() { return paramMap; } private void resetParamMap(String key, String[] value) { paramMap.put(key, value); } //过滤表情 void emojiFilter() { for (String key : paramMap.keySet()) { String[] valueSet = paramMap.get(key); if (BeanUtil.nonNull(valueSet) && valueSet.length > 0) { logger.debug("过滤前:" + valueSet[0]); valueSet[0] = EmojiFilter.filterEmoji(valueSet[0]); logger.debug("过滤后:" + valueSet[0]); resetParamMap(key, valueSet); } } } } }