@Override protected Filter[] getServletFilters() { CharacterEncodingFilter filter1 = new CharacterEncodingFilter(); filter1.setEncoding("utf-8"); filter1.setForceEncoding(true); HttpPutFormContentFilter filter2 = new HttpPutFormContentFilter(); HiddenHttpMethodFilter filter3 = new HiddenHttpMethodFilter(); LOG.info("Adding filter to dispatcher servlet[CharacterEncodingFilter,HttpPutFormContentFilter,HiddenHttpMethodFilter]"); return new Filter[]{filter1, filter2, filter3}; }
@Test public void httpPutFormContentFilterCanBeOverridden() { load(CustomHttpPutFormContentFilter.class); assertThat(this.context.getBeansOfType(OrderedHttpPutFormContentFilter.class)) .hasSize(0); assertThat(this.context.getBeansOfType(HttpPutFormContentFilter.class)) .hasSize(1); }
/** * <p>默认关闭<p> * 注意:需放在initHiddenHttpMethodFilter之后 * @param servletContext * @see HttpPutFormContentFilter */ protected void initHttpPutFormContentFilter(ServletContext servletContext){ String enablePutForm = servletContext.getInitParameter("enablePutForm"); if(BooleanUtils.valueOf(enablePutForm)){ //HttpPutFormContentFilter HttpPutFormContentFilter httpPutFormContentFilter = new HttpPutFormContentFilter(); FilterRegistration filterRegistration = servletContext.addFilter("httpPutFormContentFilter", httpPutFormContentFilter); filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); } }
@Test public void httpPutFormContentFilterCanBeOverridden() { load(CustomHttpPutFormContentFilter.class); assertThat( this.context.getBeansOfType(OrderedHttpPutFormContentFilter.class).size(), is(equalTo(0))); assertThat(this.context.getBeansOfType(HttpPutFormContentFilter.class).size(), is(equalTo(1))); }
@Override protected Filter[] getServletFilters() { CharacterEncodingFilter charFilter = new CharacterEncodingFilter(); charFilter.setEncoding("UTF-8"); charFilter.setForceEncoding(true); return new Filter[] { new HiddenHttpMethodFilter(), charFilter, new HttpPutFormContentFilter() }; }
protected void createFilters(ServletContext ctx) { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); SimpleCORSFilter corsFilter = new SimpleCORSFilter(); corsFilter.setCorsAllowCredentials("true"); corsFilter.setCorsAllowMethods("GET, POST, PUT, PATCH, DELETE, OPTIONS"); corsFilter.setCorsAllowHeaders("content-type, x-requested-with, origin, accept, authorization, username, password, x-app-type, x-app-version, x-auth-token, soapaction"); corsFilter.setCorsExposeHeaders("content-type, cookie, x-requested-with, origin, accept, username, password, x-app-type, x-app-version, x-auth-token, soapaction"); corsFilter.setCorsMaxAge("3600"); ctx.addFilter("springSessionRepositoryFilter", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("simpleCORSFilter", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); // ctx.addFilter("SimpleCorsFilter", corsFilter).addMappingForUrlPatterns( // EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("JsonHttpRequestFilter", new JsonHttpRequestFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/api/authenticate"); ctx.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("CharacterEncodingFilter", characterEncodingFilter).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("HiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("HttpPutFormContentFilter", new HttpPutFormContentFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); ctx.addFilter("ShallowEtagHeaderFilter", new ShallowEtagHeaderFilter()).addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, FILTER_MAPPING); }
@Bean(name = "httpMethodFilter") public Filter httpMethodFilter() { return new HttpPutFormContentFilter(); }
@Bean public HttpPutFormContentFilter httpPutFormContentFilter() { HttpPutFormContentFilter filter = new HttpPutFormContentFilter(); filter.setCharset(Charset.forName("utf-8")); return filter; }
@Bean @ConditionalOnMissingBean(HttpPutFormContentFilter.class) public OrderedHttpPutFormContentFilter httpPutFormContentFilter() { return new OrderedHttpPutFormContentFilter(); }
@Bean public HttpPutFormContentFilter customHttpPutFormContentFilter() { return new HttpPutFormContentFilter(); }