首页 > 解决方案 > 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 授权。

是否可以跨域+基本授权?

标签: spring-webflux

解决方案


这是我的 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");

                }
            };
        }

}

推荐阅读