spring-webflux - webflux:跨域+基本授权不起作用?
问题描述
我使用 Spring 2.0.1,这是我的 SecurityWebFilterChain
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http
// Demonstrate that method security works
// Best practice to use both for defense in depth
.authorizeExchange()
.anyExchange().permitAll()
.and()
.httpBasic().and()
.build();
这是交叉配置
@Configuration
@EnableWebFlux
public class WebConfig implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
final String ALLOWED_HEADERS = "x-requested-with, authorization,
Content-Type, Authorization, credential, X-XSRF-TOKEN";
final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS";
final String ALLOWED_ORIGIN = "http://192.168.18.124:8888";
final long MAX_AGE = 3600;
registry.addMapping("/report/**")
.allowedOrigins(ALLOWED_ORIGIN)
.allowedMethods("PUT", "GET")
.allowedHeaders("x-requested-with", "authorization",
"Content-Type", "Authorization", "credential", "X-XSRF-TOKEN")
.allowCredentials(true).maxAge(3600);
}
}
我的ajax代码
var data = {};
$.ajax({
type: 'GET',
async: false,
url: 'http://192.168.18.135:8765/report/summaries/date/2017-06-12',
dataType: 'json',
data: data,
crossDomain: true,
crossOrigin: true,
beforeSend: function (xhr) {
xhr.withCredentials = true;
xhr.setRequestHeader('Authorization', 'Basic ' + "xxxxx");
},
success: function (responseData) {
console.log('-----------------response-------------------');
console.log(responseData);
console.log('-----------------response-------------------');
response = responseData;
},
error: function (responseData) {
response.error = responseData;
}
});
return response;
});
服务器响应的错误:
http://192.168.18.135:8765/report/summaries/date/2017-06-12。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“ http://192.168.18.124:8888 ”。响应的 HTTP 状态代码为 500。
如果我删除
xhr.setRequestHeader('授权', '基本' + "xxxxx");
它将返回 401 授权。
是否可以跨域+基本授权?
解决方案
这是我的 CORS 配置。创建一个新的 WebConfig 类并声明一个 Bean,如下所示:
@Configuration
public class WebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
}
};
}
}
推荐阅读
- xml - 遍历具有旧/新值的文件并在单独的文件中更改为新值
- rust - 在其他文件中调用 Struct 数据类型
- android - 什么是 CursorLoader 的 Android Jetpack 替代品?
- angular - 如果任何表单控件值无效,如何突出显示 Primeng 选项卡 - Angular
- r - R 也无法连接以打开文件
- excel - 不要在 apache poi 中使用 XSSFBReader 读取隐藏的 xlsb 文件表
- javascript - 过滤后动态添加到表的行不隐藏
- angular8 - 目标入口点“angular-in-memory-web-api”中的错误缺少依赖项:-@angular/http
- recursion - DFS 获得所有可能的解决方案?
- python - 根据python中的自定义函数聚合数据框中的每一列