Spring-Boot actuator endpoints in Angular

  Kiến thức lập trình

I get this error when i try to expose the health endpoint in my Angular app

default:1 Access to XMLHttpRequest at ‘http://localhost:8081/actuator/health’ from origin ‘http://localhost:4200’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource

while having this configurations to deal with Cors
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(“/**”)
.allowedMethods( “GET”,”POST”, “PUT”, “DELETE”, “OPTIONS”) // Allowed HTTP methods
.allowedHeaders(““) // Allowed headers
.allowedOrigins(“
“)
.allowedHeaders(allowedHeaders)
.maxAge(MAX_AGE_SECS);

    // Allow CORS for Actuator endpoints
    registry.addMapping("/actuator/**")
            .allowedOrigins("*")
            .allowedMethods(allowedMethods)
            .allowedHeaders(allowedHeaders)
            .maxAge(MAX_AGE_SECS);
}

my application.yml is like this
management:
endpoint:
health:
probes:
enabled: true
show-details: always
web:
exposure:
include: ‘*’
health:
liveness-state:
enabled: true
readiness-state:
enabled: true

New contributor

Adolf Simelane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT