Problem:
CORS is working with GET requests that do not have @AuthenticatedPrincipal in the method parameter.
I'm using
- OKTA as my authentication server.
- Spring REST controllers
- Spring Data (CrudRepository)
For some reason, I'm getting this error everytime, I am making a POST, PUT or unsafe request that has @AuthenticatedPrincipal in the method parameter, but it is working for GET requests:
I have read every possible Stackoverflow question on CORS settings with Spring Boot and configured my CORS like this but to no avail:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.csrf().disable()
// .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
// .and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers( "/courses", "/testing/*").permitAll()
.anyRequest().authenticated().and()
.oauth2Login().and()
.oauth2ResourceServer().jwt();
http.headers().frameOptions().disable();
}
// @Bean
// CorsConfigurationSource corsConfigurationSource() {
// CorsConfiguration configuration = new CorsConfiguration();
// configuration.setAllowedOrigins(Arrays.asList("*"));
// configuration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "OPTIONS", "HEAD", "DELETE"));
// configuration.setAllowedHeaders(Arrays.asList("Content-Type"));
// configuration.setExposedHeaders(Arrays.asList("Authorization", "Link", "X-Total-Count", "Access-Control-Allow-Headers"));
// configuration.setAllowCredentials(true);
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// source.registerCorsConfiguration("/**", configuration);
// return source;
// }
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration().applyPermitDefaultValues();
configuration.setAllowedOrigins(ImmutableList.of("*"));
configuration.setAllowedMethods(ImmutableList.of("OPTIONS", "HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
// setAllowCredentials(true) is important, otherwise:
// The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the
// request's credentials mode is 'include'.
configuration.setAllowCredentials(true);
// setAllowedHeaders is important! Without it, OPTIONS preflight request
// will fail with 403 Invalid CORS request
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
My Controllers
@CrossOrigin
@RestController
@RequestMapping(EndPoints.TEST_PATH)
public class TestController {
private final TestService tService;
@Autowired
public TestController(TestService tService) {
this.tService = tService;
}
@PostMapping(path="/create")
public ResponseEntity<String> addNewTest(@Valid @NonNull @RequestBody TestDTO data,
@AuthenticationPrincipal Jwt principal){
tokenUserEmail = token.getClaimAsString("sub");
tokenUid = token.getClaimAsString("uid");
tokenFullName = token.getClaimAsString("firstName") + " "
+ token.getClaimAsString("lastName");
TestEntity testEntity = new TestEntity();
testEntity.setName(tokenFullName);
testEntity.setGrade(data.grade);
testEntity.setNumber(data.number);
tService.addNewTest(testEntity);
}
}
I have exhausted every single possible Stackoverflow question, created WebMvcConfigurerAdapter(), set and unset @CrossOrigin. At this point, I don't know what else can I do ... Anyone knows how do I fix this??
