简体   繁体   中英

How to apply Grails 3 interceptors to Spring Boot Actuator endpoints

I am attempting to apply a Grails 3.0.12 interceptor with a uri matcher to the management endpoints provided by Spring Boot Actuator. I have the actuator management.context_path property set to /admin .

All endpoints mapped in UrlMappings.groovy are being intercepted but those managed by Spring Boot Actuator are not. I instead see the following in the logs indicating that the interceptor is being bypassed:

DEBUG: org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Looking up handler method for path /admin/metrics
DEBUG: org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping - Returning handler method [public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()]

Here is my interceptor:

class LoginInterceptor {

    def securityService

    int order = HIGHEST_PRECEDENCE

    LoginInterceptor() {
        match(uri: "/**")
    }

    boolean before() {
        if (!request.exception) {
            securityService.authenticateUser()
        }
        true
    }

    boolean after() { true }

    void afterView() { /* no-op */ }
}

Here is the management config in application.yml

management:
  context_path: /admin

How do I ensure that actuator-provided endpoints are intercepted?

I did find one way of doing this by implementing the EndpointHandlerMappingCustomizer customize() method where the GrailsInterceptorHandlerInterceptorAdapter is set as an interceptor.

import org.grails.plugins.web.interceptors.GrailsInterceptorHandlerInterceptorAdapter
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer

class ActuatorInterceptor implements EndpointHandlerMappingCustomizer {
    GrailsInterceptorHandlerInterceptorAdapter interceptorAdapter

    @Override
    public void customize(EndpointHandlerMapping mapping) {
        Object[] interceptors = [ interceptorAdapter ]
        mapping.setInterceptors(interceptors)
    }
}

resources.groovy:

beans = {
    actuatorInterceptor(ActuatorInterceptor) {
        interceptorAdapter = ref('grailsInterceptorHandlerInterceptorAdapter')
    }
}

This is less than ideal since it's specific to Spring Boot Actuator and won't work for, say, Spring Cloud Config endpoints. I would like to see a more generalized way to intercept all URI mappings with a Grails interceptor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM