Java protection against XSS attacks

Source: Internet
Author: User

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
    1. <filter>
    2. <filter-name>XssEscape</filter-name>
    3. <filter-class>cn.pconline.morden.filter.XssFilter</filter-class>
    4. </filter>
    5. <filter-mapping>
    6. <filter-name>XssEscape</filter-name>
    7. <url-pattern>/*</url-pattern>
    8. <dispatcher>REQUEST</dispatcher>
    9. </filter-mapping>

The implementation of Xssfilter is to implement the servlet's filter interface

Java code
  1. Package cn.pconline.morden.filter;
  2. Import java.io.IOException;
  3. Import Javax.servlet.Filter;
  4. Import Javax.servlet.FilterChain;
  5. Import Javax.servlet.FilterConfig;
  6. Import javax.servlet.ServletException;
  7. Import Javax.servlet.ServletRequest;
  8. Import Javax.servlet.ServletResponse;
  9. Import Javax.servlet.http.HttpServletRequest;
  10. public class Xssfilter implements Filter {
  11. @Override
  12. public void init (Filterconfig filterconfig) throws Servletexception {
  13. }
  14. @Override
  15. public void DoFilter (ServletRequest request, servletresponse response,
  16. Filterchain chain) throws IOException, Servletexception {
  17. Chain.dofilter (New Xsshttpservletrequestwrapper ((httpservletrequest) request), response);
  18. }
  19. @Override
  20. public void Destroy () {
  21. }
  22. }

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
  1. Package cn.pconline.morden.filter;
  2. Import Javax.servlet.http.HttpServletRequest;
  3. Import Javax.servlet.http.HttpServletRequestWrapper;
  4. Import Org.apache.commons.lang3.StringEscapeUtils;
  5. public class Xsshttpservletrequestwrapper extends Httpservletrequestwrapper {
  6. Public Xsshttpservletrequestwrapper (HttpServletRequest request) {
  7. Super (Request);
  8. }
  9. @Override
  10. public string GetHeader (string name) {
  11. Return Stringescapeutils.escapehtml4 (Super.getheader (name));
  12. }
  13. @Override
  14. Public String getquerystring () {
  15. Return Stringescapeutils.escapehtml4 (Super.getquerystring ());
  16. }
  17. @Override
  18. public string GetParameter (string name) {
  19. Return Stringescapeutils.escapehtml4 (Super.getparameter (name));
  20. }
  21. @Override
  22. Public string[] Getparametervalues (String name) {
  23. String[] values = super.getparametervalues (name);
  24. if (values! = null) {
  25. int length = Values.length;
  26. string[] escapsevalues = new String[length];
  27. for (int i = 0; i < length; i++) {
  28. Escapsevalues[i] = STRINGESCAPEUTILS.ESCAPEHTML4 (Values[i]);
  29. }
  30. return escapsevalues;
  31. }
  32. return super.getparametervalues (name);
  33. }
  34. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.