Angular 6 + Spring Boot:错误:“来自源'http://localhost:4200'已被CORS策略阻止" [英] Angular 6 + Spring Boot: Error: "from origin 'http://localhost:4200' has been blocked by CORS policy"

查看:117
本文介绍了Angular 6 + Spring Boot:错误:“来自源'http://localhost:4200'已被CORS策略阻止"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Angle 6项目与Spring Boot应用程序连接.尽管我已经安装了所有依赖项和导入,但是当我运行角度项目时,它始终会出现此错误.

I am trying to connect angular 6 project with spring boot application. When I run angular project, it constantly gives this error, although I have installed all the dependencies and imports.

我在控制器类中使用了以下代码.

I have used following line of code in controller class.

@CrossOrigin(origins =" http://localhost:4200/",maxAge = 3600)

@CrossOrigin(origins = "http://localhost:4200/", maxAge = 3600)

我还将这个SimpleCORSFilter.java文件包含在java文件夹中:

I have also included this SimpleCORSFilter.java file in java folder:

package com.oms;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);

public SimpleCORSFilter() {
    log.info("SimpleCORSFilter init");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

    chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

我也已在angular项目中创建了proxy.conf.json文件:

Also I have created proxy.conf.json file in angular project:

{
    "/api": {
      "target": "http://localhost:8080",
      "secure": false
    }
  }

此文件包含在angular.json中:

This file is included like this in angular.json:

开始":"ng serve --proxy-config proxy.conf.json",

"start": "ng serve --proxy-config proxy.conf.json",

仍然出现此错误:

在以下位置访问XMLHttpRequest ' http://localhost:8080/fetchAll " http://localhost:4200 "已被CORS政策阻止:请求 标头字段授权不被 飞行前响应中的Access-Control-Allow-Header.**

Access to XMLHttpRequest at 'http://localhost:8080/fetchAll' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.**

我提到了这类查询,但无法获得确切的解决方案.

I referred such type of queries, but could not get the exact solution.

我真的很困惑,如果代码中有任何错误?应该采取什么步骤解决此问题?

I am really confused, if there is any error in code? What steps should be taken to resolve this issue?

推荐答案

在使用Spring Boot时,建议您在配置中添加 CorsFilter bean,而不要引入CORS过滤器.让我知道是否有帮助.

When you are using Spring Boot, I would suggest you to add a CorsFilter bean in your configurations instead of introducing a CORS filter. Let me know if this helps.

@Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("DELETE");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

快乐编码:)

Happy Coding :)

这篇关于Angular 6 + Spring Boot:错误:“来自源'http://localhost:4200'已被CORS策略阻止"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆