学无先后,达者为师

网站首页 编程语言 正文

...has been blocked by CORS policy: Response to preflight request doesn‘t pass access control check

作者:Austinu 更新时间: 2022-03-15 编程语言
Access to XMLHttpRequest at 'http://localhost:88/api/sys/login' from origin 'http://localhost:8001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
xhr.js?ec6c:178 POST http://localhost:88/api/sys/login net::ERR_FAILED

解决办法:
在网关配置文件中加入跨域配置:

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1.配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

注意:

corsConfiguration.addAllowedHeader("*");

即可。
如果做到这一步还是有问题,那么就需要将cookie权限设置

在这里插入图片描述

要允许访问前端url
在这里插入图片描述

原文链接:https://blog.csdn.net/Austin_/article/details/121315983

栏目分类
最近更新