I have an angular application which connects to my spring boot application which I generated with jhipster. When I send a post request I get this error:
Access to XMLHttpRequest at 'http://localhost:8081/api/materials-add' 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 tried activating Cors in my Application.dev.yml and then start with dev Profile but it did not work.
I tried putting this method in my Configuration Class:
@Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**").allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"); }
But still no success.
3.
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = jHipsterProperties.getCors();
if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
log.debug("Registering CORS filter");
source.registerCorsConfiguration("/api/**", config);
source.registerCorsConfiguration("/management/**", config);
source.registerCorsConfiguration("/v2/api-docs", config);
source.registerCorsConfiguration("http://localhost:4200/**",config);
}
return new CorsFilter(source);
}
This was a code which already existed I just added the last line with localhost:4200. But still no luck. Could someone help me or give me an advice what could I try next?
Intellij error:
2020-12-25 20:17:47.229 WARN 38287 --- [ XNIO-1 task-3] o.z.problem.spring.common.AdviceTraits : Unauthorized: Full authentication is required to access this resource
2020-12-25 20:17:47.231 WARN 38287 --- [ XNIO-1 task-3] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource]
But I am logged in authorization via my gateway and my gateway and my Microservice both register in the same registry, so I should be authorized is this error due to CORS?
I just read this part in my application.dev.yml: CORS is disabled by default on microservices, as you should access them through a gateway.
In my gateway I already allowed cors via the application.dev.yml like this:
cors:
allowed-origins: '*'
allowed-methods: '*'
allowed-headers: '*'
exposed-headers: 'Authorization,Link,X-Total-Count'
allow-credentials: true
max-age: 1800