gpt4 book ai didi

java - servlet 过滤器如何在 Spring 中工作?

转载 作者:行者123 更新时间:2023-11-30 08:57:07 29 4
gpt4 key购买 nike

我正在尝试向我的 Spring Web 应用程序添加一个 CORS 过滤器,但该过滤器没有被执行。我已按照此处的相关步骤操作:https://spring.io/guides/gs/rest-service-cors/无济于事。我没有使用 Spring Boot。我在 3.0+ servlet 规范容器中使用 Spring 的 WebApplicationInitializer 引导我的应用程序。

我的应用程序中的其他一切都在工作:配置类、 Controller 、 hibernate 实体等。

更新 1:

通过将过滤器添加到 servlet 容器,以下答案对我有用:

container.addFilter("CorsFilter", CorsFilter.class)
.addMappingForUrlPatterns(null, false, "/*");

但是,我很好奇为什么 Spring Boot 不需要这个?他们必须自动搜索过滤器并将它们添加到上下文中。有没有直接的方法来做到这一点?不幸的是,我必须创建一个过滤器,然后将它添加到另一个类的某个列表中。如果它们像在 Spring Boot 中一样自动注册,那就太好了。

相关代码片段:

网络应用程序初始化器:

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 {
}

网络应用配置:

@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, "/*");

关于java - servlet 过滤器如何在 Spring 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293575/

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