最近耐不住寂寞,又不想做新功能,所以闲来无事就把原本的springMVC框架换成了springboot,因为听说springboot能够提高开发效率,所以咳咳......
接下来我分别用springMVC和springboot两种方式分别实现拦截器,这也是我在本系统中的大体实现方式,首先是springMVC
1.先实现一个拦截器(本例用的是登录拦截器,是基于注解实现的,详情请往后看)
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
if (o instanceof HandlerMethod){
//检查是否有@LoginRequired登录注解
HandlerMethod handlerMethod = (HandlerMethod) o;
Method method = handlerMethod.getMethod();
LoginRequired annotation = method.getAnnotation(LoginRequired.class);
//没有直接放行,有的话进行相应的验证
if (annotation != null){
//具体验证逻辑略
}
}
return true;
}
&#