首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Grails 3拦截器应用于Spring执行器端点

如何将Grails 3拦截器应用于Spring执行器端点
EN

Stack Overflow用户
提问于 2016-02-02 03:30:42
回答 1查看 1.2K关注 0票数 0

我试图将带有uri匹配器的Grails 3.0.12拦截器应用到提供的管理端点。我将执行器management.context_path属性设置为/admin

在UrlMappings.groovy中映射的所有端点都被拦截,但那些由Spring Boot执行器管理没有被截获。相反,我在日志中看到了以下内容,表明正在绕过拦截器:

代码语言:javascript
复制
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()]

这是我的拦截器:

代码语言:javascript
复制
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中的管理配置

代码语言:javascript
复制
management:
  context_path: /admin

如何确保执行机构提供的端点被截获?

EN

回答 1

Stack Overflow用户

发布于 2016-02-02 05:17:45

我通过实现EndpointHandlerMappingCustomizer定制()方法找到了一种方法,其中将GrailsInterceptorHandlerInterceptorAdapter设置为拦截器。

代码语言:javascript
复制
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:

代码语言:javascript
复制
beans = {
    actuatorInterceptor(ActuatorInterceptor) {
        interceptorAdapter = ref('grailsInterceptorHandlerInterceptorAdapter')
    }
}

这并不理想,因为它是特定于的,不适用于端点。我希望看到一种使用Grails拦截器拦截所有URI映射的更为通用的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35144544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档