0

I am creating application with front-end using Angular-4 and backend using spring boot(RestAPI),JPA,MySQL DataBase. I am successfully able to retrieve/get stored data from backend to frontend, but while posting the data from frontend to backend getting an error as : "Response for preflight has invalid HTTP status code 403".


code snippet for frontend Angular code :

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() )

}

where productData passed from Controller class into above method argument is of Object type:

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

code snippet for backend Spring RestAPI code is :

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

    }

where Product is dto/Entity object 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
    }
6
  • Your frontend is forbidden to call that service. Check your configuration, or maybe authorization. Commented Feb 5, 2018 at 11:36
  • @M.Prokhorov Can you please provide the code snippet or any link which I can refer to.It will be help me allot. Commented Feb 5, 2018 at 12:01
  • Can you please provide a code snippet? You system is configured in a specific way which makes your code not work, and there is no configuration details in question. I cannot guess what the problem is. Commented Feb 5, 2018 at 16:11
  • @M.Prokhorov Thanks for your suggestion. But I have no much configuration in my frontend code. While calling the post method I am setting the required Headers as shown in above code. And in spring boot (backend code) Apart from application.properties where I have configure for MySql database there is no much configurations are done. Commented Feb 6, 2018 at 6:24
  • Try to disable csrf like this http.csrf().disable() in spring security or pass csrf-token with request header from angular Commented Feb 6, 2018 at 12:26

1 Answer 1

3

It was problem with spring boot configuration not with Angular application.As 403 Forbidden status means : Response to a request from a client for a web page or it may indicate that the server can be reached and process the request but server has refused to take any further action.

Finally I have added a class AppConfig which extends WebMvcConfigurerAdapter and overriden a method addCorsMappings() in order to allow me the access for all the HTTP method request.

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");
            }
        };
    }
}

And In my application.properties file I have disable springs basic security ie-

security.basic.enabled=false

Thanks for all your suggestions and comments for this problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.