title: CharacterEncodingFilter don't work together with Spring Security 3.2.0
body: <p>I'm new to Spring MVC framework and I have got an issue that I can not resolve by myself. Everything started when I integrated spring security with my application, after that all unicode values from HTML form were not encoded (spring security works correctly). I came to conclusion that this is happening probably because my <code>DelegatingFilterProxy</code> is called as the first filter in the chain.</p>
  
  <p>Here is my configuration that I thought will work, but it doesn't:</p>
  
  <p>1)I'm extending AbstractSecurityWebApplicationInitializer - from javadoc:</p>
  
  <pre><code>Registers the DelegatingFilterProxy to use the springSecurityFilterChain() before any
  other registered Filter.
  </code></pre>
  
  <p>From that class I also override beforeSpringSecurityFilterChain method which regarding to javadoc:</p>
  
  <pre><code>Invoked before the springSecurityFilterChain is added.
  </code></pre>
  
  <p>So I thought this will be the best place to register CharacterEncodingFilter:</p>
  
  <pre><code>public class MessageSecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
      @Override
      protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
          FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
          characterEncodingFilter.setInitParameter("encoding", "UTF-8");
          characterEncodingFilter.setInitParameter("forceEncoding", "true");
          characterEncodingFilter.addMappingForUrlPatterns(null, true, "/*");
      }
  }
  </code></pre>
  
  <p>But this do not work. </p>
  
  <p>Another option I tired was to register filter through AbstractAnnotationConfigDispatcherServletInitializer class by overriding getServletFilters() method:</p>
  
  <pre><code>public class WebAppInitializer extends
          AbstractAnnotationConfigDispatcherServletInitializer {
  
      //{!begin addToRootContext}
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[] { SecurityConfig.class, DatabaseConfig.class, InternationalizationConfig.class };
      }
      //{!end addToRootContext}
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[] { WebAppConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  
      @Override
      protected Filter[] getServletFilters() {
  
          CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
          characterEncodingFilter.setEncoding("UTF-8");
          characterEncodingFilter.setForceEncoding(true);
          return new Filter[] { characterEncodingFilter};
      }
  }
  </code></pre>
  
  <p>But this do not work neither. Does anyone come across the same issue or have got some ideas how to resolve this?</p>
  
  <p>Here is my full configuration for the first option where I'm registering encoding filter through AbstractSecurityWebApplicationInitializer:</p>
  
  <pre><code>@Order(1)
  public class MessageSecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
      @Override
      protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
          FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
          characterEncodingFilter.setInitParameter("encoding", "UTF-8");
          characterEncodingFilter.setInitParameter("forceEncoding", "true");
          characterEncodingFilter.addMappingForUrlPatterns(null, true, "/*");
      }
  }
  
  @Order(2)
  public class WebAppInitializer extends
          AbstractAnnotationConfigDispatcherServletInitializer {
  
      //{!begin addToRootContext}
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[] { SecurityConfig.class, DatabaseConfig.class, InternationalizationConfig.class };
      }
      //{!end addToRootContext}
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[] { WebAppConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  }
  
  @EnableWebMvc
  //@Import(value = {DatabaseConfig.class, InternationalizationConfig.class, SecurityConfig.class})
  @ComponentScan(basePackages = {"com.ajurasz.controller", "com.ajurasz.service", "com.ajurasz.model"})
  @Configuration
  public class WebAppConfig extends WebMvcConfigurerAdapter {
  
      @Bean
      public UrlBasedViewResolver viewResolver() {
          UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
          urlBasedViewResolver.setViewClass(TilesView.class);
          urlBasedViewResolver.setContentType("text/html;charset=UTF-8");
          return urlBasedViewResolver;
      }
  
      @Bean
      public TilesConfigurer tilesConfigurer() {
          TilesConfigurer tilesConfigurer = new TilesConfigurer();
          tilesConfigurer.setDefinitions(new String[] {"/WEB-INF/tiles.xml"});
          return tilesConfigurer;
      }
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/**");
          registry.addResourceHandler("/documents/**").addResourceLocations("/WEB-INF/pdfs/documents/**");
      }
  
      @Override
      public void addArgumentResolvers(List&lt;HandlerMethodArgumentResolver&gt; argumentResolvers) {
          PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver =
                  new PageableHandlerMethodArgumentResolver();
          pageableHandlerMethodArgumentResolver.setFallbackPageable(new PageRequest(0, 4, new Sort(Sort.Direction.DESC, "id")));
  
          argumentResolvers.add(pageableHandlerMethodArgumentResolver);
      }
  }
  </code></pre>
  
  <p><strong>Dependencies:</strong></p>
  
  <p>spring-mvc                 <strong>3.2.5.RELEASE</strong></p>
  
  <p>spring-security-config, spring-security-web, spring-security-core  <strong>3.2.0.RELEASE</strong></p>
  
  <p>I'm working on this under following link:
  <a href="https://github.com/ajurasz/Manager" rel="nofollow noreferrer">https://github.com/ajurasz/Manager</a></p>