spring boot 1.3如何在Interceptor preHandle方法中获取方法信息

uui*_*ode 5 spring-boot

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug(">>> handler: " + handler);
    }

    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Login login = handlerMethod.getMethod().getAnnotation(Login.class);
}
Run Code Online (Sandbox Code Playgroud)

我在 spring 3.X 中有上面的拦截器代码可以工作。我喜欢在 Spring Boot 1.3的 Controller 中使用此代码@CrossOrigin@RequestMapping方法。但发生以下错误。

如何preHandle在spring boot 1.3中的Interceptor方法中获取方法信息?

Caused by: java.lang.ClassCastException: org.springframework.web.servlet.handler.AbstractHandlerMapping$PreFlightHandler cannot be cast to org.springframework.web.method.HandlerMethod
Run Code Online (Sandbox Code Playgroud)

小智 5

添加到一部分以处理4.2 Spring之后添加的请求CORS这将再次处理“拦截器”。因此可以添加代码来检查对象类型的“处理程序”是否为“HandlerMethod”类型。

例如

if (handler instanceof HandlerMethod) {...}
Run Code Online (Sandbox Code Playgroud)