//继承一个hand接口
public class MyInterceptor implements HandlerInterceptor
//实现里面的一个方法
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
/获取session中的对象
User u = (User) request.getSession().getAttribute("u");
//判断是否为空
if(u!=null) {
return true;
}else {
//为空返回到登录页面
response.sendRedirect("/index");
}
return false;
}
另一个类实现Webmvc的接口
实现@configuration注解
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/").excludePathPatterns("/index","/static/");
这里 addInterceptor(new MyInterceptor()) 这里new的是刚才那个类,
之后第一个是拦截内容,第二个是放行内容