I have a problem. If I send a request from the server to http://localhost:8082/finance/rate/getActualRate I get the following response
Access to XMLHttpRequest at 'http://localhost:8082/finance/rate/getActualRate' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
app.component.ts:45 HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: 'Unknown Error', url: 'http://localhost:8082/finance/rate/getActualRate', ok: false, …}
app.component.ts:42 GET http://localhost:8082/finance/rate/getActualRate net::ERR_FAILED 403 (Forbidden)
But if I send the request to the same address via Postman, everything works fine
I had already added a proxy.conf.json file and was running the application using it
{
{ "/finance": {
{ "target": "http://localhost:8082",
"secure": false
}
}
And added Cors configuration to my Gateway(I use Eureka server and redirect requests from front end to microservices via gateway)
package ua.com.alevel.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowedOriginPatterns(Arrays.asList("http://localhost:4200")); // Разрешенные источники (здесь можно указать нужные источники)
corsConfig.addAllowedMethod("*");
corsConfig.addAllowedHeader("*");
corsConfig.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfig);
return new CorsFilter(source);
}
}