我正在尝试将Grails 3.0.12拦截器与uri匹配器应用于Spring Boot Actuator提供的管理端点。我将执行器 management.context_path 属性设置为 / admin 。
正在截取UrlMappings.groovy中映射的所有端点,但managed by Spring Boot Actuator不会截获。我在日志中看到以下内容,表明拦截器被绕过:
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()]
这是我的拦截器:
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 */ }
}
这是application.yml中的管理配置
management:
context_path: /admin
如何确保拦截致动器提供的端点?
答案 0 :(得分:0)
我确实通过实现EndpointHandlerMappingCustomizer customize()方法找到了一种方法,其中GrailsInterceptorHandlerInterceptorAdapter被设置为拦截器。
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')
}
}
这不太理想,因为它特定于Spring Boot Actuator,并且不适用于Spring Cloud Config端点。我希望看到一种更通用的方法来拦截所有带有Grails拦截器的的URI映射。