Spring Security without web.xml

前端 未结 7 1427
不思量自难忘°
不思量自难忘° 2021-01-30 15:06

What is the recommended way to add Spring Security to a web application that is using Spring\'s new WebApplicationInitializer interface instead of the web.xml file?

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 15:12

    Faced with the same problem. Merge RootConfig and WebAppConfig - not best way - because this i did not try this solution. Tried all other solutions - everty time got "org.apache.catalina.core.StandardContext.startInternal Error filterStart". After some work, got something like this:

        FilterRegistration.Dynamic enc= servletContext.addFilter("encodingFilter",
                new CharacterEncodingFilter());
        encodingFilter .setInitParameter("encoding", "UTF-8");
        encodingFilter .setInitParameter("forceEncoding", "true");
        encodingFilter .addMappingForUrlPatterns(null, true, "/*");
    

    But is not working with DelegatingFilterProxy(). Continue finding for best common solution for all filters.

    UPDATE: I did it.

    So, the main issue is: if you want add filters using java config, especially if you want to add security filter, such as DelegatingFilterProxy, then you have to create WebAppSecurityConfig:

    @Configuration
    @EnableWebSecurity
    @ImportResource("classpath:security.xml")
    public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {
    }
    

    In this case i create WebAppSecurityConfig and make import resource ("security.xml"). This let me to do that in Initializer class:

    servletContext.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
                .addMappingForUrlPatterns(null, false, "/*");
    

提交回复
热议问题