尽管具有CORS的功能,Spring应用程序仍会响应406

尽管具有CORS的功能,Spring应用程序仍会响应406

问题描述:

假设 Project 是我们的POJO类.以下功能提供了从数据库中删除行的功能.它已成功处理POSTMAN请求.

Assume that Project is our POJO class. Following function provides to delete a row from database. It is successfully working with POSTMAN requests.

@RestController
@RequestMapping(value = "/project")
@CrossOrigin
public class ProjectController {

    private final ProjectServiceImpl projectServiceImpl;

    ------------
    @DeleteMapping
    @RequestMapping("/delete/{id}")
    public ResponseEntity<Boolean> deleteProject(@PathVariable Long id) {
        boolean result = projectServiceImpl.delete(id);
        return ResponseEntity.ok(result);
    }
    ------------
}

但是来自Angular项目的请求被403消息拒绝.控制台屏幕中将显示以下消息.

But requests from Angular project are rejecting with 403 message. And following message is appearing in console screen.

经过一些搜索.我了解到,应用程序必须用200回答飞行前请求.为此,向控制器添加了以下功能.

After some searches. I learned, the application have to answer pre-flight requests with 200. To provide this, following function was added to controller.

@GetMapping
@RequestMapping("/delete/{id:[0-9]+}")
public ResponseEntity.BodyBuilder retreive(@PathVariable Long id) {
    return ResponseEntity.ok();
}

我使用正则表达式进行请求映射,因为没有它,Spring Framework会抛出已经与另一个函数映射的/project/delete/{id} .Angular通过这种方式获得 pre-flight 请求的200OK.但是对于删除操作,应用程序响应为406.Angular将 http://localhost:8080/project/delete/2 网址发送到应用程序.如果我发送相同的链接而没有CORS的功能.ID为2的行将成功删除.我想念什么吗?

I used regex for request mapping because without it Spring Framework throws /project/delete/{id} already mapped with another function. Angular get its 200OK for pre-flight request with this way. But the application response is 406 for delete operation. Angular is sending http://localhost:8080/project/delete/2 url to the application. If I send same link without have a function for CORS. Row has id with 2 will delete successfully. Am I missing something?

来源:

为什么Angular在删除之前发送OPTIONS消息

如何向Spring Boot应用程序添加CORS支持

要实现服务器端代理:proxy.conf.json

To implement server side proxy: proxy.conf.json

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

angular.json中的修改部分

modified section in angular.json

    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "issue-management:build",
        "proxyConfig": "proxy.conf.json"
      },

和以 ng开始的Angular项目服务--proxy-config proxy.conf.json ,但结果没有改变.另外,文章中的建议已应用,再次结果没有没变化.

and Angular project started with ng serve --proxy-config proxy.conf.json but result didn't change. Plus, suggestions in this article applied, again result didn't change.

您的应用程序在两个不同的端口上运行,这会导致CORS问题.

Your applications are running on two different ports, that causing the CORS issue.

在Angular应用程序中添加代理(文件proxy.conf.json).

Add the proxy(file proxy.conf.json) in your Angular application.

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

并运行此 ng serve --proxy-config proxy.conf.json

参考角度文档

更新:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
            .allowedMethods("*")
            .allowedOrigins("http://localhost:4200");
        }
    };
}

有效,由于某种原因,Angular代理无法正常工作

worked, For some reason Angular proxy is not working