【发布时间】:2015-04-02 08:00:40
【问题描述】:
我正在尝试向我的 Spring Web 应用程序添加一个 CORS 过滤器,但该过滤器没有被执行。我已经按照此处的相关步骤操作:https://spring.io/guides/gs/rest-service-cors/ 无济于事。我没有使用 Spring Boot。我在 3.0+ servlet 规范容器中使用 Spring 的 WebApplicationInitializer 引导我的应用程序。
我的应用程序中的其他一切都在工作:配置类、控制器、休眠实体等。
更新 1:
通过将过滤器添加到 servlet 容器,以下答案对我有用:
container.addFilter("CorsFilter", CorsFilter.class)
.addMappingForUrlPatterns(null, false, "/*");
但是,我很好奇为什么 Spring Boot 不需要这个?他们必须自动搜索过滤器并将它们添加到上下文中。有没有一种简单的方法可以做到这一点?不幸的是,我必须创建一个过滤器,然后将其添加到另一个类的某个列表中。如果它们像在 Spring Boot 中那样自动注册就好了。
相关代码sn-ps:
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("/");
}
}
应用配置:
@Configuration
@ComponentScan
public class AppConfig {
}
WebAppConfig:
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@ComponentScan(basePackageClasses = AppConfig.class)
public class WebAppConfig extends WebMvcConfigurerAdapter {
}
CorsFilter:
@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");
}
}
【问题讨论】:
-
您不应该在 Web 容器中注册您的过滤器(例如,通过将其添加到 web.xml)
标签: java spring spring-mvc servlets