简体   繁体   English

Angular Spring Boot:重定向错误:在飞行前响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type

[英]Angular Spring Boot: Redirection error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response

I'm using Angular 6 in the frontend and Java Spring Boot in the backend. 我在前端使用Angular 6,在后端使用Java Spring Boot。

I want my login page to make request to server. 我希望我的登录页面向服务器发出请求。 Server redirects to dashboard page. 服务器重定向到仪表板页面。

I have http://localhost:4200/login angular page which makes a POST request to server http://localhost:8080/login 我有http:// localhost:4200 / login角度页面,该页面向服务器http:// localhost:8080 / login发出POST请求

The .ts file contains: .ts文件包含:

onLoginClickPost() {
  console.log('redirect request clicked');
  this.data.getUserInfoPost().subscribe(
    data => {
      console.log(data);
      this.userInfo = data;
    }
  );
}

getUserInfoPost() {
    return this.http.post('http://localhost:8080/login', {nextUrl: 'http://localhost:4200/dashboard'});
}

The server has @RestController with a method mapping to incoming POST request, which should redirect to http://localhost:4200/dashboard : 服务器具有@RestController,其方法映射到传入的POST请求,该方法应重定向到http:// localhost:4200 / dashboard

@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<?> login_post(@RequestBody LoginReqPostParam payload) {

    HttpHeaders headers = new HttpHeaders();

    try {
        headers.setLocation(new URI(payload.getNextUrl()));
        headers.add("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    } catch (URISyntaxException e) {
        logger.error("URI Exception: ", e);
        e.printStackTrace();
    }

    return new ResponseEntity<>(headers, HttpStatus.FOUND);
}

LoginReqPostParam is a simple class to have incoming JSON payload as object. LoginReqPostParam是一个简单的类,将传入的JSON有效负载作为对象。

The login angular page is able to make POST request to the server. 登录角度页面能够向服务器发出POST请求。 Now at the server, after returning the ResponseEntity from server, I'm getting following error in browser console: 现在在服务器上,从服务器返回ResponseEntity之后,在浏览器控制台中出现以下错误:

Failed to load http://localhost:4200/dashboard : Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 无法加载http:// localhost:4200 / dashboard预检响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type。

在此处输入图片说明

How do I redirect to http://localhost:4200/dashboard page? 如何重定向到http:// localhost:4200 / dashboard页面? Do I need to add something to angular dashboard component files? 我是否需要向角度仪表板组件文件中添加一些内容?

You are using @CrossOrigin for POST request but Angular (in development) is sending a Preflight request using OPTIONS. 您正在使用@CrossOrigin进行POST请求,但是Angular(正在开发中)正在使用OPTIONS发送预检请求

I recommend you to use a development profile that permit all origins (you must create a custom filter in a Bean for these profile) 我建议您使用允许所有来源的开发配置文件(必须在Bean中为这些配置文件创建自定义过滤器)

Use Allow-Control-Allow-Origin: * extension for google chrome. 使用Allow-Control-Allow-Origin:* Google chrome扩展名。 And enable cross origin resource sharing https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en 并启用跨源资源共享https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=zh-CN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 - Request header field is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 x-xsrf-token - Request header field x-xsrf-token is not allowed by Access-Control-Allow-Headers in preflight response 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段 ack 即使服务器已经允许它 - Request header field ack is not allowed by Access-Control-Allow-Headers in preflight response even the server already allowing it 在飞行前响应中,Access-Control-Allow-Headers不允许在Request标头字段中使用cors enable Access-Control-Allow-Origin - cors enable in Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response CORS 策略:无法解析预检响应中的 Access-Control-Allow-Headers 响应头字段 - CORS policy: Cannot parse Access-Control-Allow-Headers response header field in preflight response 尝试获取 Spring 启动应用程序以在请求没有“内容类型”时发送错误响应消息 HTTP 请求 header - Trying to get Spring Boot app to send an error response message when request does not have "Content-type" HTTP request header 对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头。 服务器错误 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present . Server error 如何处理“对预检请求的响应未通过没有访问控制允许来源 header 存在于请求的资源上”来自 angular - How to handle “Response to preflight request doesn't pass No Access-control-Allow-Origin header is present on requested resource ” from angular in BE 对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present Spring Boot 允许 GET 请求标头中的任何内容类型 - Spring boot to allow any content type in headers for GET request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM