Java servlet过滤器在Spring中是如何工作的?
我试图将CORS过滤器添加到SpringWeb应用程序中,但该过滤器未被执行。我在这里遵循了相关步骤:没有用。我没有用弹簧靴。我正在使用Spring的WebApplicationInitializer在3.0+servlet规范容器中引导我的应用程序 我的应用程序中的其他一切都在工作:配置类、控制器、hibernate实体等 更新1: 通过将过滤器添加到servlet容器,下面的答案对我很有用:Java servlet过滤器在Spring中是如何工作的?,java,spring,spring-mvc,servlets,Java,Spring,Spring Mvc,Servlets,我试图将CORS过滤器添加到SpringWeb应用程序中,但该过滤器未被执行。我在这里遵循了相关步骤:没有用。我没有用弹簧靴。我正在使用Spring的WebApplicationInitializer在3.0+servlet规范容器中引导我的应用程序 我的应用程序中的其他一切都在工作:配置类、控制器、hibernate实体等 更新1: 通过将过滤器添加到servlet容器,下面的答案对我很有用: container.addFilter("CorsFilter", CorsFilter.class
container.addFilter("CorsFilter", CorsFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
然而,我很好奇为什么SpringBoot不需要这个?他们必须自动搜索过滤器并将其添加到上下文中。有没有一种简单的方法可以做到这一点?似乎很不幸,我必须创建一个过滤器,然后将其添加到另一个类的某个列表中。如果它们像在Spring Boot中那样自动注册,那就太好了
相关代码片段:
WebApplicationInitializer:
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebAppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
AppConfig:
@Configuration
@ComponentScan
public class AppConfig {
}
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@ComponentScan(basePackageClasses = AppConfig.class)
public class WebAppConfig extends WebMvcConfigurerAdapter {
}
WebAppConfig:
@Configuration
@ComponentScan
public class AppConfig {
}
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@ComponentScan(basePackageClasses = AppConfig.class)
public class WebAppConfig extends WebMvcConfigurerAdapter {
}
科斯菲尔特:
@Component
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
httpResponse.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
httpResponse.setHeader("Access-Control-Max-Age", "3600");
httpResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type");
HttpServletRequest httpRequest = (HttpServletRequest) request;
String origin = httpRequest.getHeader("Origin");
if (StringUtils.hasText(origin)) {
boolean isMyDomain =
Pattern.compile("^https://(.*?\\.)?mydomain.com(:\\d+)?$")
.matcher(origin)
.find();
if (isMyDomain) {
httpResponse.setHeader("Access-Control-Allow-Origin", origin);
} else {
httpResponse.setHeader("Access-Control-Allow-Origin", "mydomain");
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
LoggerFactory.getLogger(getClass()).info("CorsFilter initiated");
}
@Override
public void destroy() {
LoggerFactory.getLogger(getClass()).info("CorsFilter destroyed");
}
}
您可以这样添加:
container.addFilter("CorsFilter", CorsFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
您只需在
应用初始值设定项中注册过滤器即可
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebAppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
//Added filter dynamically
javax.servlet.FilterRegistration.Dynamic corsFilter = container.addFilter("corsfilter", CORSFilter.class);
corsFilter.addMappingForUrlPatterns(null, true, "/*");
}
}
您可以参考。您是否应该在web容器中注册您的过滤器(例如,通过将其添加到web.xml)确定的可能副本非常感谢我将尝试一下。。。出于好奇,Spring Boot如何做到这一点而不在WebApplicationInitializer中显式添加过滤器?使用SpringBoot,我可以添加一个javax.servlet.Filter作为SpringBean,其余的都由它来完成。在26.3.1 servlet和Filters的文档中,它是这样写的:“当使用嵌入式servlet容器时,您可以将servlet和Filters直接注册为SpringBean。”我已经更新了关于SpringBoot自动注册servlet过滤器的方法的问题。