当前位置: 首页 > 知识库问答 >
问题:

preflight响应在angular 4&spring boot(RestAPI)应用程序中的HTTP状态代码403无效

陈琪
2023-03-14
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() )

}
productData : any =  {
      price: 3500,
      productavaliabledate: "March 13, 2018",
      productcode: "GND-124",
      productid: 6,
      productimage: "https://i.imgur.com/sJUpSpt.jpg",
      productname: "dummy1",
      productrating: 3
      }
 @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对象IE:

@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
    }

共有1个答案

钱澄邈
2023-03-14

这是Spring Boot配置的问题,而不是Angular应用程序。403禁止状态表示:对来自客户端的web页面请求的响应,或者它可能指示可以到达服务器并处理该请求,但服务器拒绝采取任何进一步的操作。

最后,我添加了一个类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");
            }
        };
    }
}
security.basic.enabled=false
 类似资料: