En el frontend (Angular 14) se realizan varias solicitudes http al backend (Springboot) las cuales funcionan sin problemas salvo por una que cumple la funcion de enviar el email del usuario para resetear el password.
resetPassword(email : string): Observable<CustomHttpResponse | HttpErrorResponse>{
return this.http.get<CustomHttpResponse>(`${this.host}/user/resetPassword/${email}`);
}
El error es este
Access to XMLHttpRequest at 'https://supportapp-production.up.railway.app/user/resetPassword/[object%20Object]' 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.
CORS en default en la configuracion de Spring
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth.requestMatchers(SecurityConstant.PUBLIC_URLS).permitAll().anyRequest().authenticated())
.cors(Customizer.withDefaults())
.csrf(config -> config.disable())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(this.jwtAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class)
.exceptionHandling().accessDeniedHandler(jwtAccessDeniedHandler)
.authenticationEntryPoint(this.jwtAuthenticationEntryPoint());
return http.build();
}
metodo de configuracion
@Bean
public CorsConfigurationSource corsConfigurationSource(){
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin(CorsConstant.SUPPORTAPPWEB_LOCAL);
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowedHeaders(Arrays.asList(CorsConstant.ALLOWED_HEADERS));
corsConfiguration.setExposedHeaders(Arrays.asList(CorsConstant.EXPOSED_HEADERS));
corsConfiguration.setAllowedMethods(Arrays.asList(CorsConstant.HTTP_METHODS));
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return urlBasedCorsConfigurationSource;
}
la constante SUPPORTAPPWEB_LOCAL contiene el valor "http://localhost:4200"
tambien puse la anotacion CrossOrigin en el endpoint del controlador
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/resetPassword/{email}")
public ResponseEntity<HttpResponse> resetPassword(@PathVariable("email") String email) throws EmailNotFoundException, MessagingException {
this.userService.resetPassword(email);
return response(HttpStatus.OK, "Email sent to: " + email);
}
el endpoint desde postman funciona bien. no funciona cuando la solicitud se hace desde localhost:4200
/user/resetPassword/[object%20Object]
parece que la url no está bien formada