Http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html#xsshappen
Here's a list of our solutions for the recent project, mainly using the ORG.APACHE.COMMONS.LANG3.STRINGESCAPEUTILS.ESCAPEHTML4 () approach to commons-lang3-3.1.jar this package.
The resolution process is mainly in the user input and display output two steps: At the input of the special word such as <> "' & Escape, in the output with Jstl fn:excapexml (" fff ") method.
Where the input filter is implemented with a filter,
Implementation process:
Add a filter to Web. xml
XML code
- <filter>
- <filter-name>XssEscape</filter-name>
- <filter-class>cn.pconline.morden.filter.XssFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>XssEscape</filter-name>
- <url-pattern>/*</url-pattern>
- <dispatcher>REQUEST</dispatcher>
- </filter-mapping>
The implementation of Xssfilter is to implement the servlet's filter interface
Java code
- Package cn.pconline.morden.filter;
- 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 init (Filterconfig filterconfig) throws Servletexception {
- }
- @Override
- public void DoFilter (ServletRequest request, servletresponse response,
- Filterchain chain) throws IOException, Servletexception {
- Chain.dofilter (New Xsshttpservletrequestwrapper ((httpservletrequest) request), response);
- }
- @Override
- public void Destroy () {
- }
- }
The key is the implementation of Xsshttpservletrequestwrapper, inheriting the httpservletrequestwrapper of the servlet, and rewriting the corresponding several possible methods with XSS attacks, such as:
Java code
- Package cn.pconline.morden.filter;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletRequestWrapper;
- Import Org.apache.commons.lang3.StringEscapeUtils;
- public class Xsshttpservletrequestwrapper extends Httpservletrequestwrapper {
- Public Xsshttpservletrequestwrapper (HttpServletRequest request) {
- Super (Request);
- }
- @Override
- public string GetHeader (string name) {
- Return Stringescapeutils.escapehtml4 (Super.getheader (name));
- }
- @Override
- Public String getquerystring () {
- Return Stringescapeutils.escapehtml4 (Super.getquerystring ());
- }
- @Override
- public string GetParameter (string name) {
- Return Stringescapeutils.escapehtml4 (Super.getparameter (name));
- }
- @Override
- Public string[] Getparametervalues (String name) {
- String[] values = super.getparametervalues (name);
- if (values! = null) {
- int length = Values.length;
- string[] escapsevalues = new String[length];
- for (int i = 0; i < length; i++) {
- Escapsevalues[i] = STRINGESCAPEUTILS.ESCAPEHTML4 (Values[i]);
- }
- return escapsevalues;
- }
- return super.getparametervalues (name);
- }
- }
At this end, the input filter is complete.
When displaying data on a page, simply use Fn:escapexml () to escape the output in a place where an XSS vulnerability is likely to occur.
The display of complex content, specific problems, and then specific analysis.
In addition, some cases do not want to display the filtered content, you can use STRINGESCAPEUTILS.UNESCAPEHTML4 () This method, the STRINGESCAPEUTILS.ESCAPEHTML4 () escaped character after the return to the original.
Java protection against XSS attacks