-
Notifications
You must be signed in to change notification settings - Fork 41.2k
when interceptor preHandler throw exception, the cors is broken #9595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@binlaniua Thank you for trying to report what, I guess, you think is a bug in Spring Boot. Unfortunately, you haven't provided enough information for us to do anything about it. When I report a bug, I find it useful to look at it from the perspective of a project maintainer and ask myself if they could easily identify the problem with the information that I have provided. In this case, there's lots of useful information that is missing. For example:
Of these, 4 is the most important. It is best answered with a small sample that reproduces the problem. Can you please provide one? |
in WebMvcConfigurerAdapter1. we add cors config public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**/*")
.allowCredentials(true)
;
} 2. add interceptor
3. i found the source code in AbstractHandlerMapping add cors cors interceptor at the last in interceptor chain protected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, CorsConfiguration config) {
if(CorsUtils.isPreFlightRequest(request)) {
HandlerInterceptor[] interceptors = chain.getInterceptors();
// here
chain = new HandlerExecutionChain(new AbstractHandlerMapping.PreFlightHandler(config), interceptors);
} else {
chain.addInterceptor(new AbstractHandlerMapping.CorsInterceptor(config));
}
return chain;
} in HandlerExecutionChain public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
this.interceptorIndex = -1;
if(handler instanceof HandlerExecutionChain) {
HandlerExecutionChain originalChain = (HandlerExecutionChain)handler;
this.handler = originalChain.getHandler();
this.interceptorList = new ArrayList();
CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);
CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);
} else {
this.handler = handler;
this.interceptors = interceptors;
}
} how to fix
|
It's still not clear why you think CORS is "broken" in this case. I'm not sure that it makes sense for CORS processing to occur when an interceptor's thrown an exception and, presumably, the request is going to fail anyway. The requested sample may have made this clearer, but you haven't provided one. Anyway, |
in this case, the cors not effect when interceptor throw exception i provider plan is make the cors at the first interceptor chain |
Same issue here. |
@detinho so i create a filter, in doFilter code like this CorsConfiguration cors = handlerMapping.getCorsConfigurations().values().iterator().next()
handlerMapping.getCorsProcessor().processRequest(cors, request, response); and must add config public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**/*")
.allowCredentials(true)
;
} |
CorsFilter works here! |
Thx, @qixiaobo , switching to Filter based implementation helped for me. |
@Configuration
public class CorsConfigFilters {
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
} This piece of code works for me in interceptor if your interceptor return true or false both |
spring boot version: 1.5.4.RELEASE
The text was updated successfully, but these errors were encountered: