最近一直在學習javaWeb,并且自己嘗試著寫個項目練手,今天實現(xiàn)了登錄驗證功能,整理一下做個筆記。
有兩種方法可以實現(xiàn)這個功能:
- Servlet過濾器Filter
- SpringMVC攔截器
我在項目中使用的是第二種方式來實現(xiàn)的。
1.創(chuàng)建Interceptor攔截器
- 通過實現(xiàn)
HandlerInterceptor接口 - 通過繼承
HandlerInterceptorAdapter抽象類 - 通過實現(xiàn)
WebRequestInterceptor接口
1.1 實現(xiàn)HandlerInterceptor接口
廢話少說,直接上代碼
/**
* 登陸攔截器
* Created by LiuZongRui on 17/2/20.
*/
public class LoginInterceptor implements HandlerInterceptor {
/**
* 該方法將在Controller處理之前進行調(diào)用,return false則請求中止
*/
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
// 獲取請求的url
String url = httpServletRequest.getRequestURI();
if (StringUtils.equals(url, "/console/login.do")) {
// 登陸接口,放行
return true;
} else {
HttpSession session = httpServletRequest.getSession();
User user = (User) session.getAttribute(Constans.USER);
if (user != null) {
return true;
}
}
// 不符合條件的跳轉到登錄界面
// 客戶端跳轉
httpServletResponse.sendRedirect("/console/login.do");
return false;
}
/**
* Controller處理之后,ModelAndView返回之前調(diào)用這個方法
*/
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
/**
* 在ModelAndView視圖渲染之后調(diào)用這個方法,一般此方法用于清理資源
*/
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
1.2 繼承HandlerInterceptorAdapter抽象類
/**
*通過查看HandlerInterceptorAdapter的源碼可以發(fā)現(xiàn),它其實也是實現(xiàn)HandlerInterceptor接口的,
*只是進一步進行了封裝,重寫對應的方法即可。
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
super.preHandle(request, response, handler);
return false;
}
}
1.3 實現(xiàn)WebRequestInterceptor接口
public class LoginWebInterceptor implements WebRequestInterceptor {
/**
*在請求處理之前執(zhí)行,該方法主要是用于準備資源數(shù)據(jù)的,然后可以把它們當做請求屬性放到WebRequest中。
*它與HandlerInterceptor的preHandle是不同,它沒有返回值。
*/
public void preHandle(WebRequest webRequest) throws Exception {
//這個是放到request范圍內(nèi)的,所以只能在當前請求中的request中獲取到
request.setAttribute("request", "request", WebRequest.SCOPE_REQUEST);
//這個是放到session范圍內(nèi)的,如果環(huán)境允許的話它只能在局部的隔離的會話中訪問,否則就是在普通的當前會話中可以訪問
request.setAttribute("session", "session", WebRequest.SCOPE_SESSION);
//如果環(huán)境允許的話,它能在全局共享的會話中訪問,否則就是在普通的當前會話中訪問
request.setAttribute("globalSession", "globalSession", WebRequest.SCOPE_GLOBAL_SESSION);
}
/**
* 該方法將在Controller執(zhí)行之后,返回視圖之前執(zhí)行,ModelMap表示請求Controller處理之后返回的Model對象,所以可以在
* 這個方法中修改ModelMap的屬性,從而達到改變返回的模型的效果。
*/
public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
}
public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
}
}
大概總結了一下SpringMVC攔截器的三種實現(xiàn)方法。萬變不離其中,都是針對請求的三個階段進行攔截,進行相應的操作。在實現(xiàn)登錄驗證的功能時,只需要處理preHandle()方法就行了,并且第三種方式不可取,因為它用于準備資源數(shù)據(jù)。
2.在spring-mvc的xml配置文件中配置攔截器
直接上代碼
<!--攔截器-->
<mvc:interceptors>
<!--多個攔截器,順序執(zhí)行-->
<mvc:interceptor>
<!--匹配多級路徑-->
<mvc:mapping path="/**"/>
<!--登陸權限攔截器-->
<bean class="com.ws.console.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
配置代碼簡單易懂,但是映射路徑還是需要注意:
<mvc:mapping path="/*"/>這個不是匹配所有的請求,而是代表著根目錄的第一級請求,如請求路徑為:http://localhost:8080/project/path1,如上配置可以攔截,如果請求路徑為:http://localhost:8080/project/path1/path2,如上配置則不能攔截,需要修改配置為: <mvc:mapping path="/*/*"/>。如果想攔截http://localhost:8080/project/path1/path2,而不想攔截http://localhost:8080/project/path2/path1,可以配置為:<mvc:mapping path="/path1/*">。
通過SpringMVC攔截器可以非常方便的實現(xiàn)登錄攔截功能,以上就是全部的實現(xiàn)代碼了,不過還存在兩個問題,由于篇幅原因,將另外寫一篇文章來記錄。