filter过滤器实现特殊字符转义

Posted 起个名字好难

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了filter过滤器实现特殊字符转义相关的知识,希望对你有一定的参考价值。

 

web.xml

<!-- 特殊字符转义或转换 -->
    <filter>
        <filter-name>XssFilter</filter-name>
        <filter-class>cn.zsmy.interceptor.XssFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>XssFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping> 

 

XssFilter.java

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class XssFilter implements Filter {

    @Override
    public void destroy() {
        // to nothing
    }

     @Override  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        XssHttpServletRequestWraper xssRequest = new XssHttpServletRequestWraper((HttpServletRequest) request);
        chain.doFilter(xssRequest, response);
    }  

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // do nothing
    }

}

 

XssHttpServletRequestWraper.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.lang.StringUtils;

public class XssHttpServletRequestWraper extends HttpServletRequestWrapper {

    public XssHttpServletRequestWraper(HttpServletRequest request) {
        super(request);
    }
    
    @Override
    public String getParameter(String name) {
        //Constants.MY_LOG.debug("getParameter----->转义处理");
        //return clearXss(super.getParameter(name));// 保留勿删
        return xssEncode(super.getParameter(name));
    }
    
    @Override
    public String getHeader(String name) {
        //Constants.MY_LOG.debug("getHeader----->转义处理");
        //return clearXss(super.getHeader(name)); // 保留勿删
        return xssEncode(super.getParameter(name));
    }
    
    @Override
    public String[] getParameterValues(String name) {
        //Constants.MY_LOG.debug("getParameterValues----->转义处理");
        if(!StringUtils.isEmpty(name)){
            String[] values = super.getParameterValues(name);
            if(values != null && values.length > 0){
                String[] newValues = new String[values.length];
                
                for(int i =0; i< values.length; i++){
                    //newValues[i] = clearXss(values[i]);// 保留勿删
                    newValues[i] = xssEncode(values[i]);
                }
                return newValues;
            }
        }
        return null;
    }

    /**
     *  
     * 处理字符转义【勿删,请保留该注释代码】
     * @param value
     * @return
    private String clearXss(String value){
        if (value == null || "".equals(value)) {
            return value;
        }
        value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
        value = value.replaceAll("\\(", "&#40;").replace("\\)", "&#41;");
        value = value.replaceAll("‘", "&#39;");
        value = value.replaceAll("eval\\((.*)\\)", "");
        value = value.replaceAll("[\\\"\\\‘][\\s]*javascript:(.*)[\\\"\\\‘]", "\"\"");
        value = value.replace("script", "");
        return value;
    }*/
    
    /** 
     * 将特殊字符替换为全角 
     * @param s 
     * @return 
     */  
    private  String xssEncode(String s) {  
        if (s == null || s.isEmpty()) {  
            return s;  
        }  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < s.length(); i++) {  
            char c = s.charAt(i);  
            switch (c) {  
            case ‘>‘:  
                sb.append(‘>‘);// 全角大于号  
                break;  
            case ‘<‘:  
                sb.append(‘<‘);// 全角小于号  
                break;  
            case ‘\‘‘:  
                sb.append(‘‘‘);// 全角单引号  
                break;  
            case ‘\"‘:  
                sb.append(‘“‘);// 全角双引号  
                break;  
            case ‘&‘:  
                sb.append(‘&‘);// 全角&  
                break;  
            case ‘\\‘:  
                sb.append(‘\‘);// 全角斜线  
                break;  
            case ‘/‘:  
                sb.append(‘/‘);// 全角斜线  
                break;  
            case ‘#‘:  
                sb.append(‘#‘);// 全角井号  
                break;  
            case ‘(‘:  
                sb.append(‘(‘);// 全角(号  
                break;  
            case ‘)‘:  
                sb.append(‘)‘);// 全角)号  
                break;  
            default:  
                sb.append(c);  
                break;  
            }  
        }  
        return sb.toString();  
    }  
}

 

以上是关于filter过滤器实现特殊字符转义的主要内容,如果未能解决你的问题,请参考以下文章

如何过滤django模板中的特殊字符

解析ldap过滤器以转义特殊字符

函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义

JavaWeb 之 Filter:过滤器

微信小程序 WXS实现json数据需要做过滤转义(filter)

如何转义 YAML 字符串中的冒号和其他特殊字符?