Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Solve 403 Error in Spring Boot Post Request

I am newbie in spring boot rest services. I have developed some rest api in spring boot using maven project.

I have successfully developed Get and Post Api. My GET Method working properly in postman and mobile. when i am trying to hit post method from postman its working properly but from mobile its gives 403 forbidden error.

This is my Configuration:

spring.datasource.url = jdbc:mysql://localhost/sampledb?useSSL=false spring.datasource.username = te spring.datasource.password = test spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update 

Please Suggest me how to solve error.

enter image description here

like image 904
Harshal Deshmukh Avatar asked May 23 '18 10:05

Harshal Deshmukh


People also ask

How do I fix a 403 postman error?

The simple answer is; “You need to be given the correct access”. Without being given the correct access you'd technically be hacking the server, as it is specifically set up to restrict said access.


1 Answers

you have to disable csrf Protection because it is enabled by default in spring security: here you can see code that allow cors origin.

import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;  @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{      @Override     protected void configure(HttpSecurity http) throws Exception{         http.cors().and().csrf().disable();     }      @Bean     CorsConfigurationSource corsConfigurationSource() {         CorsConfiguration configuration = new CorsConfiguration();         configuration.setAllowedOrigins(Arrays.asList("*"));         configuration.setAllowedMethods(Arrays.asList("*"));         configuration.setAllowedHeaders(Arrays.asList("*"));         configuration.setAllowCredentials(true);         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();         source.registerCorsConfiguration("/**", configuration);         return source;     }  } 
like image 183
Ezzat Eissa Avatar answered Sep 24 '22 17:09

Ezzat Eissa