Spring boot 如何定制;无效的CORS请求“;Spring DefaultCorsProcessor引发的消息?

Spring boot 如何定制;无效的CORS请求“;Spring DefaultCorsProcessor引发的消息?,spring-boot,cors,Spring Boot,Cors,当我们为Spring引导应用程序启用CORS时,它会为带有无效原始标头的restapi调用抛出无效CORS请求“消息。这是由DefaultCorsProcessor的下面的方法引发的。有没有办法自定义此消息 protected void rejectRequest(ServerHttpResponse response) throws IOException { response.setStatusCode(HttpStatus.FORBIDDEN); respo

当我们为Spring引导应用程序启用
CORS
时,它会为带有无效原始标头的restapi调用抛出
无效CORS请求“
消息。这是由
DefaultCorsProcessor的
下面的方法引发的。有没有办法自定义此消息

protected void rejectRequest(ServerHttpResponse response) throws IOException {
        response.setStatusCode(HttpStatus.FORBIDDEN);
        response.getBody().write("Invalid CORS request".getBytes(StandardCharsets.UTF_8));
        response.flush();
}

尝试了各种选项,如自定义异常处理程序,但没有帮助。

我认为您可以像这样插入自定义CorsProcessor:

import java.io.IOException;
导入org.springframework.boot.autoconfigure.web.servlet.WebMVC注册;
导入org.springframework.http.server.ServerHttpResponse;
导入org.springframework.stereotype.Component;
导入org.springframework.web.cors.DefaultCorsProcessor;
导入org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@组成部分
公共类CustomWebMvcRegistrations实现WebMvcRegistrations{
@凌驾
公共RequestMappingHandlerMappingGetRequestMappingHandlerMapping(){
RequestMappingHandlerMapping rhm=新的RequestMappingHandlerMapping();
rhm.setCorsProcessor(新的DefaultCorsProcessor(){
@凌驾
受保护的无效拒绝请求(ServerHttpResponse响应)引发IOException{
//你想干什么就干什么
超级拒绝请求(响应);
}
});
返回rhm;
}
}