ssm登录拦截器

 //继承一个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的是刚才那个类,
之后第一个是拦截内容,第二个是放行内容

猜你喜欢

转载自blog.csdn.net/qq_41930208/article/details/86561492