title: How to create filter in Spring RESTful for Prevent XSS?
body: <p>i use from Spring 4.2.6.RELEASE and backend is rest services.
  and now I can not have a filter for <strong>Prevent XSS</strong></p>
  
  <p>my filter is:</p>
  
  <pre><code>@Component
  @Order(1)
  public class XSSFilter implements Filter {
  
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
  
    @Override
    public void destroy() {
    }
  
    @Override
    public void doFilter(
          ServletRequest request, 
          ServletResponse response, 
          FilterChain chain) throws IOException, ServletException {
  
        chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response);
     }
  
  }
  </code></pre>
  
  <p>and XSSRequestWrapper is :</p>
  
  <pre><code>public class XSSRequestWrapper extends HttpServletRequestWrapper {
  
    public XSSRequestWrapper(HttpServletRequest servletRequest) {
      super(servletRequest);
    }
  
    @Override
    public String[] getParameterValues(String parameter) {
      String[] values = super.getParameterValues(parameter);
  
      if (values == null) {
          return null;
      }
  
      int count = values.length;
      String[] encodedValues = new String[count];
      for (int i = 0; i &lt; count; i++) {
          encodedValues[i] = stripXSS(values[i]);
      }
  
      return encodedValues;
    }
  
    @Override
    public String getParameter(String parameter) {
      String value = super.getParameter(parameter);
  
      return stripXSS(value);
    }
  
    @Override
    public String getHeader(String name) {
      String value = super.getHeader(name);
      return stripXSS(value);
    }
  
    private String stripXSS(String value) {
  
      return StringEscapeUtils.escapeHtml(value);
    }
  }
  </code></pre>
  
  <p>and in <strong><em>WebConfig extends WebMvcConfigurerAdapter</em></strong> Class:</p>
  
  <pre><code>// -----------------------------------------------------
  // Prevent XSS
  // -----------------------------------------------------
  
  @Bean
  public FilterRegistrationBean xssPreventFilter() {
      FilterRegistrationBean registrationBean = new FilterRegistrationBean();
  
      registrationBean.setFilter(new XSSFilter());
      registrationBean.addUrlPatterns("/*");
  
      return registrationBean;
  }
  </code></pre>
  
  <p>My Rest Class is:</p>
  
  <pre><code>@RestController
  @RequestMapping("/personService")
  public class PersonController extends BaseController&lt;PersonDto, PersonCriteria&gt; {
  
    @RequestMapping( value= "/test" )
    private void getTest2(@RequestParam String name) {
  
        System.out.println(name);
  
        System.out.println( StringEscapeUtils.escapeHtml(name) );
  
    }
  
  }
  </code></pre>
  
  <p>But it does not work, without any Error or Exception.
  How can I do this and create my own filter? I use only Java Config and no XML.
  in my Controller I was forced again use StringEscapeUtils.escapeHtml(name) and this is bad.</p>