首页 > 代码库 > SpringMVC拦截器

SpringMVC拦截器

springmvc的拦截器

需求:进行用户的访问控制,判断用户是否登陆,如果登陆进行正常访问,如果没有登陆跳转到登陆页面。

1自定义拦截器类

 1 package org.guangsoft.utils;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 import javax.servlet.http.HttpSession;
 6 
 7 import org.springframework.web.servlet.HandlerInterceptor;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 public class LoginInterceptor implements HandlerInterceptor
11 {
12 
13     /***
14      * afterCompletion:在整个handler中,处理请求的方法执行完毕,执行执行
15      *  场景:统一的异常处理,日志记录,资源的释放
16      * ****/
17     @Override
18     public void afterCompletion(HttpServletRequest request,
19             HttpServletResponse response, Object obj, Exception exp)
20             throws Exception
21     {
22     }
23 
24     /****
25      * postHandle:进入handler中处理本次的请求的方法,在return ModelAndView之前执行
26      * 场景:统一的model数据和view试图数据封装。
27      *     日志记录
28      * ***/
29     @Override
30     public void postHandle(HttpServletRequest request, HttpServletResponse response,
31             Object obj, ModelAndView mv) throws Exception
32     {
33     }
34 
35     /***
36      * preHandle:在handler(处理器的)处理请求的方法之前执行,只有该方法的返回值为true的情况下
37      * 放行:才会执行handler中的方法:
38      * 场景:权限控制,登陆的访问控制
39      * ***/
40     @Override
41     public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
42             Object obj) throws Exception
43     {
44         HttpSession session = request.getSession();
45         //获得客户端发送请求的路径
46         String url = request.getRequestURL().toString();
47         //获得session中特定值
48         Object user = session.getAttribute("user");
49         if(user == null)
50         {
51             response.sendRedirect("login.jsp");//跳转到登陆页面
52             return false;
53             
54         }
55         return true;
56     }
57 
58

2进行拦截器配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xmlns:p="http://www.springframework.org/schema/p"
 8     xsi:schemaLocation=
 9     "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
10     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
11     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
12 
13     <!-- 开启扫描注解 -->
14     <context:component-scan base-package="org.guangsoft.controller"></context:component-scan>
15     <!-- 开启映射注解和适配注解 -->
16     <mvc:annotation-driven></mvc:annotation-driven>
17     <!--  
18     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
19         <property name="defaultEncoding" value="http://www.mamicode.com/utf-8"></property>
20         <property name="maxUploadSize" value="http://www.mamicode.com/5000000"></property>
21         <property name="uploadTempDir" value="http://www.mamicode.com/upload"></property>
22     </bean>
23     -->
24     <mvc:interceptors>
25         <mvc:interceptor>
26             <mvc:mapping path="/**"/>
27             <mvc:exclude-mapping path="/login.action"/>
28             <bean class="org.guangsoft.utils.LoginInterceptor"></bean>
29         </mvc:interceptor>
30     </mvc:interceptors>
31 </beans>

 

SpringMVC拦截器