之前的版本是 v0.2.1 保持登錄狀態(tài)的版本,這一次 v0.2.2 是對(duì)驗(yàn)證登錄過(guò)程的一個(gè)更新,用攔截器和過(guò)濾器來(lái)進(jìn)行,發(fā)現(xiàn)這種方式省去了一些邏輯的實(shí)現(xiàn)
Registration-login-interface2
Version 0.2.2
新增的功能都是在 v0.2.1 的基礎(chǔ)上進(jìn)行修改,刪去部分代碼
可通過(guò)點(diǎn)擊 tag 來(lái)查看之前的代碼:

1. 新增功能
使用 mvc 的攔截器來(lái)對(duì)特定頁(yè)面進(jìn)行攔截,攔截之后跳轉(zhuǎn)至登錄界面
使用過(guò)濾器來(lái)進(jìn)行相同功能的攔截
2. 刪去的代碼
先刪除 ExceptionService 中的 statusException 方法,刪去對(duì)應(yīng)的實(shí)現(xiàn),然后在 UserServiceImpl 和 Controller 中刪去調(diào)用它的代碼
這樣,就變成了不檢測(cè)用戶是否登錄
3. 攔截器
首先新建一個(gè)包:interceptor
然后新建一個(gè)類:LoginInterceptor,實(shí)現(xiàn)了 HandlerInterceptor 接口:
關(guān)于 HandlerInterceptor 的使用這里不講述,如果在 session 中沒(méi)有獲得 username 屬性,就跳轉(zhuǎn)到登錄頁(yè)面
攔截是有范圍的,在下文的配置文件中自行配置需要攔截的頁(yè)面
public class LoginInterceptor implements org.springframework.web.servlet.HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o) throws Exception {
HttpSession session = httpServletRequest.getSession();
if (session.getAttribute("username") == null){
httpServletResponse.sendRedirect("preLogin.action");
} else {
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
在 spring-mvc.xml 中添加攔截器的配置:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/userStatus.action"/>
<mvc:mapping path="/showInfo.action"/>
<mvc:mapping path="/setUserInfo.action"/>
<bean class="interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
對(duì)指定路徑進(jìn)行映射,LoginInterceptor 中的操作就是對(duì)映射的路徑而言的,然后指定一下攔截器所在位置,就完成了
效果
運(yùn)行之后,在瀏覽器地址欄輸入
- localhost:8080/userStatus.action
- localhost:8080/showInfo.action
- localhost:8080/setUserInfo.action
就會(huì)跳轉(zhuǎn)到 http://localhost:8080/preLogin.action
攔截器的部分就結(jié)束了
4. 過(guò)濾器
將 spring-mvc.xml 中配置的攔截器信息注釋掉,然后新建包:filter,包中新建類 LoginFilter,實(shí)現(xiàn) javax.servlet.Filter 接口:
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
if (((HttpServletRequest) servletRequest).getSession().
getAttribute("username") == null){
((HttpServletResponse)servletResponse).sendRedirect("preLogin.action");
} else {
filterChain.doFilter(servletRequest,
servletResponse);
}
}
}
如果檢測(cè)出沒(méi)有登錄,就會(huì)跳轉(zhuǎn)到登錄界面,有一點(diǎn)要注意:filterChain.doFilter(servletRequest, servletResponse);
是一定要寫(xiě)的,如果不寫(xiě),在登錄之后,就不能訪問(wèn)那些需要登錄才能訪問(wèn)的頁(yè)面的,相當(dāng)于請(qǐng)求已經(jīng)被過(guò)濾器中斷了,如果檢測(cè)出已登錄,就要加上這一行代碼才能方位個(gè)人信息頁(yè)面
然后在 web.xml 中添加一個(gè)過(guò)濾器的配置:
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/showInfo.action</url-pattern>
<url-pattern>/userStatus.action</url-pattern>
<url-pattern>/setUserInfo.action</url-pattern>
</filter-mapping>
這樣子,不同于 v0.2.1 的驗(yàn)證登錄方式就做完了