简体   繁体   English

将Filter bean添加到ServletContext

[英]Add Filter bean to ServletContext

I am using Java configuration and know I can add a custom filter like this: 我正在使用Java配置,并知道我可以添加这样的自定义过滤器:

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
    filter.setServletContext(servletContext);
    filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
    servletContext.addFilter("corsFilter", CorsFilter.class).addMappingForUrlPatterns(null, false, "/*");
    servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, true, "/*");
}

This works fine, but feels like I am hacking around the edges of Spring. 这很好,但感觉就像我在Spring的边缘乱砍。

What I would like to do is add my CORS filter to the DelegationFilterProxy. 我想要做的是将我的CORS过滤器添加到DelegationFilterProxy。

In my root config I added: 在我的root配置中,我添加了:

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Filter CorsFilter() {
    return new CorsFilter();
}

and removed the adding of the custom filter in onStartup. 并删除了在onStartup中添加自定义过滤器。 Now it does not work at all. 现在它根本不起作用。

How do I add a bean to my filter chain and make sure it is called first? 如何将bean添加到我的过滤器链并确保首先调用它?

You can add it same way as you are adding springSecurityFilteChain in your onStartUp() method like... 您可以像在onStartUp()方法中添加springSecurityFilteChain一样添加它,就像...

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    DelegatingFilterProxy filter1 = new DelegatingFilterProxy("springSecurityFilterChain");
    filter1.setServletContext(servletContext);
    filter1.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
    DelegatingFilterProxy filter2 = new DelegatingFilterProxy("corsFilter");
    servletContext.addFilter("springSecurityFilterChain", filter1).addMappingForUrlPatterns(null, true, "/*");
    servletContext.addFilter("corsFilter",filter2).addMappingForUrlPatterns(null, false, "/*");
}

To simply globally enable the CORS filter using Java config, try this: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#javaconfig 要使用Java配置简单地全局启用CORS过滤器,请尝试以下操作: https//spring.io/blog/2015/06/08/cors-support-in-spring-framework#javaconfig

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ServletContext 加载 Applicationscoped Bean - Load Applicationscoped Bean with ServletContext Spring 3在自定义bean中接收servletContext - Spring 3 receive servletContext in custom bean 创建在ServletContext中定义的名称为“ authenticationmanager”的bean时出错 - Error creating bean with name 'authenticationmanager' defined in ServletContext BeanCreationException:创建在ServletContext中定义的名称为“ /”的bean时出错 - BeanCreationException: Error creating bean with name '/' defined in ServletContext Autowired ServletContext 是 xml 定义的 bean 中的 NULL - Autowired ServletContext is NULL in bean defined by xml 创建在ServletContext中定义的名称为“ dataSource”的bean时出错 - Error creating bean with name 'dataSource' defined in ServletContext 在Spring中使用ContextStartedEvent将过滤器添加到servletcontext - Adding filter to servletcontext using ContextStartedEvent in Spring 如何在Spring XML元数据配置中为bean设置ServletContext属性 - How to set ServletContext property for a bean in Spring XML metadata configuration 创建在ServletContext资源中定义的名称为'entityManagerFactory'的bean时出错(持久性错误) - Error creating bean with name 'entityManagerFactory' defined in ServletContext resource (persistance errors) 创建在ServletContext资源中定义的名称为'sessionFactory'的bean时出错 - Error creating bean with name 'sessionFactory' defined in ServletContext resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM