08springmvc拦截器

springmvc 编写拦截器类,主要实现 org.springframework.web.servlet.HandlerInterceptor 接口
package com.fz.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by webrx on 2017-06-29.
*/
public class TestInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
System.out.println("preHandle");
return true;//通过拦截器
}

@Override
public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mv) throws Exception {
System.out.println("postHandle");
}

@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object o, Exception e) throws Exception {
System.out.println("afterCompletion");
}
}

 

public class MyInterceptor implements HandlerInterceptor {

@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object o, Exception e) throws Exception {
System.out.println("拦截器最后执行");
}

@Override
public void postHandle(HttpServletRequest req, HttpServletResponse res, Object o, ModelAndView mv) throws Exception {
System.out.println("拦截器-处理后");
}

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object o) throws Exception {
System.out.println("处理前");
//return false; 拦截器 不通过拦截器
return true; //通过拦截器
}
}

src/main/resources/mvc.xml 核心配置文件配置拦截器
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/mydown"/>
<bean class="com.fz.interceptor.TestInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

<!-- 配置项目所有拦截器 -->
<!-- 项目拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/admin/**"/>
<mvc:mapping path="/**"/> 拦截所有路径
<mvc:exclude-mapping path="/ok"/> exclude 排除相关的路径
<mvc:exclude-mapping path="/ccc"/>
<!--<mvc:exclude-mapping path="/user/**"/>--> 排除
<bean class="com.interceptors.MyInterCeptor"/>
</mvc:interceptor>
</mvc:interceptors>


编写实现安全登录的拦截器
--------------------------------------------------------------------------
编写拦截器类,实现org.springframework.web.servlet.HandlerInterceptor
src/main/java/com/fz/interceptor/AuthInterceptor.java
package com.fz.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Created by webrx on 2017-06-30.
*/
public class AuthInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object o) throws Exception {
boolean flag = true;
HttpSession s = req.getSession();
if(s.getAttribute("admin")==null){
flag = false;
//转发跳转
req.getRequestDispatcher("login.jsp").forward(req,res);
//res.sendRedirect("login.jsp"); 重定向跳转
}
return flag;
}

@Override
public void postHandle(HttpServletRequest req, HttpServletResponse res, Object o, ModelAndView mv) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object o, Exception e) throws Exception {
}
}

配置resources/mvc.xml 文件
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 拦截器简单测试 -->
<mvc:interceptor>
<mvc:mapping path="/mydown"/>
<bean class="com.fz.interceptor.TestInterceptor"/>
</mvc:interceptor>

<!-- 安全登录拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/admin/**"/>
<mvc:exclude-mapping path="/admin/login"/>
<mvc:exclude-mapping path="/admin/login.jsp"/>
<mvc:exclude-mapping path="/admin/check"/>
<bean class="com.fz.interceptor.AuthInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

posted @ 2017-07-18 15:49  归然  阅读(135)  评论(0编辑  收藏  举报