春季启动应用返回“ CORS”策略已阻止对“ myURL”处的XMLHttpRequest的访问

马哈茂德

我陷入了这个问题:我在后端使用spring boot应用程序,而在前端使用angular应用程序,问题是当我在后端调用一个特定的rest url时,该URL使用我已作为maven添加到后端的jar依赖项系统范围的依赖关系,我得到一个Cors错误。所有其他后端网址都可以正常工作

这是我如何包含后端的jar依赖项:

    <dependency>
        <groupId>com.ex</groupId>
        <artifactId>lib</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/Utils.jar</systemPath>
    </dependency>

另请注意,我在前端和后端之间使用Zuul调度程序,并且我在后端进行了此配置

@Component

公共类CorsFilter扩展了OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    response.setHeader("Access-Control-Max-Age", "3600"); 
    response.setHeader("Access-Control-Allow-Headers", "authorization, content-type, xsrf-token, Content-Range, Content-Disposition, Content-Description, GAuth");
    response.addHeader("Access-Control-Expose-Headers", "xsrf-token");
    if ("OPTIONS".equals(request.getMethod())) {
        response.setStatus(HttpServletResponse.SC_OK);
    } else {
        filterChain.doFilter(request, response);
    } 
}

}

任何帮助将不胜感激,谢谢

塞尔多97

首先,您需要@CrossOrigin在控制器中添加注释。比这里是文档中正确的配置:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {

            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*");

        }
    };
}

您可以按照自己的方式配置它。

其次,如果您正在使用,则SpringSecurity需要添加.cors()到您的配置中。

例:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors().and()
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll()
                .and().httpBasic();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CORS 策略已阻止从源“localhost:3000”访问“...”处的 XMLHttpRequest

无法从离子应用程序连接到Signalr Hub:CORS策略已阻止访问XMLHttpRequest ...

CORS策略已阻止从原始位置访问XMLHttpRequest(Vue.JS应用程序调用Lumen App)

CORS 策略已阻止从源“http://localhost:8080”访问“http://localhost:8092/URL”处的 XMLHttpRequest

CORS策略已阻止访问源为“ http:// localhost:4200”的“ http:// ....”处的XMLHttpRequest

通过CORS策略已阻止从源“ null”访问“ http://github/..file.json”处的XMLHttpRequest:

CORS策略已阻止从来源“ http:// localhost:8000”访问“ http:// localhost / api”处的XMLHttpRequest

如何解决CORS策略已阻止对XMLHttpRequest的访问?

对XMLHttpRequest的访问已被CORS策略阻止

Spring Boot:即使使用@ CrossOrigin,CORS策略也阻止了从源FRONTEND在BACK_END'处访问XMLHttpRequest

从CORS策略阻止从源“ http:// localhost:4200”访问“ URL”处的XMLHttpRequest:

CORS 策略(Spring Boot 和 Angular 7)阻止了从源“http://...”访问“https://...”处的 XMLHttpRequest

CORS 策略已阻止在“来自源”的 XMLHttpRequest 访问:请求中不存在“Access-Control-Allow-Origin”标头

将数据发布到 Web 应用程序时,XMLHttpRequest 被 CORS 策略阻止

CORS 策略已阻止从源“http://localhost:3000”访问“https://localhost:7144/api/employees”

使用 Axios 在 ReactJS 中访问被 CORS 策略阻止的 XMLHttpRequest

CORS 策略阻止了对源“http://localhost:4200”的 XMLHttpRequest 的访问

Vue + Golang:从源头访问 [Apiurl] 处的 XMLHttpRequest 已被 CORS 策略阻止:不允许请求头字段授权

如何修复XMLHttpRequest已被CORS策略阻止

访问被 CORS 策略阻止的 Web 清单

在春季启动CORS策略冲突

对 XMLHttpRequest 的访问已被 cors 策略阻止:对预检请求的响应未通过访问控制检查

从源头访问 Xmlhttprequest 被 CORS 策略 Asp.NET Core MVC 阻止

从CORS策略阻止从源“空”访问“ https:// localhost:44395”的访问

JS 访问 ZUUL 背后的 Spring REST 服务被 CORS 策略阻止

Node.JS OvernightJS Express CORS XMLHttpRequest已被CORS策略阻止

从源对XMLHttpRequest的访问已被CORS策略阻止:请求的资源上不存在“ Access-Control-Allow-Origin”标头

CORS政策已阻止从源“空”访问脚本“

從源“http://localhost:4200”訪問“localhost:8088/user/”的 XMLHttpRequest 已被 CORS 策略阻止