gpt4 book ai didi

spring-mvc - CharacterEncodingFilter不能与Spring Security 3.2.0一起使用

转载 作者:行者123 更新时间:2023-12-03 08:31:37 25 4
gpt4 key购买 nike

我是Spring MVC框架的新手,但遇到一个我自己无法解决的问题。当我将Spring Security与我的应用程序集成后,一切都开始了,之后HTML表单中的所有unicode值都未编码(Spring Security正常工作)。我得出结论,这可能是因为我的DelegatingFilterProxy被称为链中的第一个过滤器而发生的。

这是我认为可以使用的配置,但无效:

1)我正在从Javadoc扩展AbstractSecurityWebApplicationInitializer-

Registers the DelegatingFilterProxy to use the springSecurityFilterChain() before any
other registered Filter.

从该类中,我还重写了有关javadoc的beforeSpringSecurityFilterChain方法:
Invoked before the springSecurityFilterChain is added.

所以我认为这将是注册CharacterEncodingFilter的最佳位置:
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, "/*");
}
}

但这不起作用。

我烦的另一个选择是通过重写getServletFilters()方法通过AbstractAnnotationConfigDispatcherServletInitializer类注册过滤器:
public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

//{!begin addToRootContext}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { SecurityConfig.class, DatabaseConfig.class, InternationalizationConfig.class };
}
//{!end addToRootContext}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { 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};
}
}

但是,这也不起作用。是否有人遇到相同的问题或有解决此问题的想法?

这是我通过AbstractSecurityWebApplicationInitializer注册编码过滤器的第一个选项的完整配置:
@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<?>[] getRootConfigClasses() {
return new Class<?>[] { SecurityConfig.class, DatabaseConfig.class, InternationalizationConfig.class };
}
//{!end addToRootContext}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { 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<HandlerMethodArgumentResolver> argumentResolvers) {
PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver =
new PageableHandlerMethodArgumentResolver();
pageableHandlerMethodArgumentResolver.setFallbackPageable(new PageRequest(0, 4, new Sort(Sort.Direction.DESC, "id")));

argumentResolvers.add(pageableHandlerMethodArgumentResolver);
}
}

依赖项:

spring-mvc 3.2.5.RELEASE

spring-security-config,spring-security-web,spring-security-core 3.2.0.RELEASE

我正在通过以下链接对此进行处理:
https://github.com/ajurasz/Manager

最佳答案

有同样的问题。我的解决方案是使用原始servlet过滤器:

public void onStartup(ServletContext servletContext) throws ServletException {
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding-filter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}

请注意,此问题仅在Tomcat中发生,而在Jetty中不发生。

关于spring-mvc - CharacterEncodingFilter不能与Spring Security 3.2.0一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com