https://www.cnblogs.com/mcl2238973568/p/11461689.html
SpringMvc配置拦截器
- 创建一个类 实现接口 HandlerInterceptor
2.重写接口中的方法
复制代码
1 public class adminInterceptoor implements HandlerInterceptor{
2
3 @Override
4 public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
5 throws Exception {
6 // TODO Auto-generated method stub
7
8 }
9
10 @Override
11 public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
12 throws Exception {
13 // TODO Auto-generated method stub
14
15 }
16
17 @Override
18 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
19 Object admin = request.getSession().getAttribute(“admin”);
20 if(admin!=null) {
21 return true;
22 }else {
23 response.sendRedirect("…/login.jsp");
24 return false;
25 }
26 }
复制代码
-
把创建的类配置到springMVC配置文件中
-
拦截器(拦截都是控制层的地址。 filter: )
-
创建一个类 实现接口 HandlerInterceptor
-
重写接口的中方法
-
把创建的类配置到springmvc文件中。