【发布时间】:2018-11-24 17:33:58
【问题描述】:
我正在从 Spring Boot 1.x 升级到 Spring Boot 2.0,并注意到我的 HandlerInterceptors 中开始出现类转换错误。
例如,在一个HandlerInterceptor 中,我查看控制器方法/端点是否用@AdminOnly 注释以限制对某些端点的访问。
@Component
public class AdminOnlyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Object handler) {
HandlerMethod hm = (HandlerMethod) handler;
Method method = hm.getMethod();
if (method.getDeclaringClass().isAnnotationPresent(RestController.class) && (method.isAnnotationPresent(AdminOnly.class) || method.getDeclaringClass().isAnnotationPresent(AdminOnly.class))) {
// Some Logic returning true or false
}
return true;
}
}
这在 Spring Boot 1.5.x 中有效。
升级后我现在得到以下异常:
java.lang.ClassCastException: org.springframework.web.servlet.resource.ResourceHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod
我在migration guide 中找不到任何相关内容。如何升级但保持上面的拦截器正常工作?
【问题讨论】:
标签: java spring spring-boot