【问题标题】:Response for preflight has invalid HTTP status code 403 in angular 4 & spring boot (RestAPI) application预检响应在 Angular 4 和 Spring Boot (RestAPI) 应用程序中具有无效的 HTTP 状态代码 403
【发布时间】:2018-02-05 11:25:58
【问题描述】:

我正在创建前端使用 Angular-4 和后端使用 spring boot(RestAPI)、JPA、MySQL 数据库的应用程序。 我能够成功地从后端检索/获取存储的数据到前端,但是在将数据发布时从前端到后端出现错误: “预检响应包含无效的 HTTP 状态代码 403”。


前端 Angular 代码的代码 sn-p :

myFoo(productData : Product) : Observable<any>{

   let headers = new Headers();
     headers.append( 'Content-Type', 'application/json' );
     let myOptions = new RequestOptions({ headers: headers });

    return this._http
      .post("http://localhost:8081/..(URI)../productslist",productData,myOptions)
      .map( (resp : Response) => resp.json() )

}

其中从 Controller 类传递到上述方法参数的 productData 是 Object 类型的:

productData : any =  {
      price: 3500,
      productavaliabledate: "March 13, 2018",
      productcode: "GND-124",
      productid: 6,
      productimage: "https://i.imgur.com/sJUpSpt.jpg",
      productname: "dummy1",
      productrating: 3
      }

后端 Spring RestAPI 代码的代码 sn-p 是:

 @RestController
 public class OnlineShoppingController {

    @Autowired
    private ProductService productService;

    @RequestMapping(path="/productslist",method =RequestMethod.POST)
    public Product createProduct(@Valid @RequestBody Product product) {
        return productService.createProduct(product);
    }

    }

Product 是 dto/Entity 对象,即:

@Entity
@Table(name="product_table")
public class Product {
    @Id

    @Column(name="productid", columnDefinition="INT(100)")
    private int  productid;

    @Column(name="productname")
    private String   productname;

    @Column(name="productcode")
    private String    productcode;

    @Column(name="productavaliabledate")
    private String  productavaliabledate;

    @Column(name="price" , columnDefinition="INT(100)")
    private int  price;

    @Column(name="productrating",  columnDefinition="INT(50)")
    private int  productrating;

    @Column(name="productimage")
    private String   productimage;

    //getter,setter,constructor using field are also provided
    }

【问题讨论】:

  • 您的前端is forbidden 调用该服务。检查你的配置,或者授权。
  • @M.Prokhorov 能否请您提供代码 sn-p 或任何我可以参考的链接。它将帮助我分配。
  • 能否提供一个代码sn-p?您的系统以特定方式配置,这使您的代码无法正常工作,并且没有任何配置细节存在问题。我猜不出问题出在哪里。
  • @M.Prokhorov 感谢您的建议。但是我的前端代码中没有太多配置。在调用 post 方法时,我正在设置所需的 Headers,如上面的代码所示。在spring boot(后端代码)中,除了我为MySql数据库配置的application.properties之外,没有做太多的配置。
  • 尝试在 Spring Security 中禁用 csrf 这样的 http.csrf().disable() 或使用来自 angular 的请求标头传递 csrf-token

标签: java spring angular rest spring-boot


【解决方案1】:

这是 spring boot 配置问题 不是 Angular 应用程序。因为 403 禁止状态意味着: 对来自客户端的网页请求的响应,或者它可能表明可以访问服务器并处理该请求,但服务器拒绝采取任何进一步的行动。

最后,我添加了一个类 AppConfig,它扩展了 WebMvcConfigurerAdapter 并覆盖了方法 addCorsMappings() 以允许我访问所有 HTTP 方法请求。

AppConfig.java-

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {


    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
            }
        };
    }
}

在我的 application.properties 文件中,我禁用了 springs 基本安全性,即-

security.basic.enabled=false

感谢您对此问题的所有建议和 cmet。

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 2016-02-08
    • 2018-04-11
    • 2018-09-22
    • 2016-04-14
    • 2018-01-25
    • 2018-04-05
    • 2018-06-18
    • 2016-12-26
    相关资源
    最近更新 更多