I have the following configurations:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class SecurityConfig {
private final JwtAuthFilter jwtAuthFilter;
private final AuthenticationProvider authenticationProvider;
private static final String[] PUBLIC_MATCHERS = {"/v1/auth/**"};
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http.csrf()
.disable()
.authorizeHttpRequests()
.requestMatchers(PUBLIC_MATCHERS)
.permitAll()
.anyRequest()
.authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authenticationProvider(authenticationProvider)
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
@Configuration
public class CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:4200")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "TRACE", "CONNECT");
}
}
And the endpoint:
@RestController
@RequestMapping("/v1/clients")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientRestController {
private final ClientService service;
private final ClientMapper mapper;
@GetMapping
public ResponseEntity<Page<ClientResponse>> getByUser(
@RequestParam(defaultValue = "0") final int page) {
final var clients = service.findAllByUserLogged(PageRequest.of(page, 10));
return ResponseEntity.ok().body(clients.map(mapper::toResponse));
}
But when I send the requests from angular application I get the this:
Access to XMLHttpRequest at 'http://localhost:8080/v1/clients' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I'm sending the Authorization header, in the get, but my application are not sending the header in options request, I don't if it is that is causing this problem. I get error in the two requests.
I tried a request right after receiving a 200 from my authenticate endpoint.