攔截器和過(guò)濾器驗(yàn)證登錄

之前的版本是 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)證登錄方式就做完了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,551評(píng)論 19 139
  • 攔截器是Struts2框架的核心,它主要完成解析請(qǐng)求參數(shù)、將請(qǐng)求參數(shù)賦值給Action屬性、執(zhí)行數(shù)據(jù)校驗(yàn)、文件上傳...
    重山楊閱讀 4,077評(píng)論 2 13
  • 1.前沿 在web.xml中各個(gè)元素的執(zhí)行順序是這樣的:context-param-->listener-->fi...
    OzanShareing閱讀 3,605評(píng)論 0 10
  • 不知道從什么時(shí)候開(kāi)始 我害怕夜的到來(lái) 原來(lái)的一切 在沒(méi)有太陽(yáng)的夜底下 變的陌生 卻不敢讓人讓接近 啊……陌生的夜 ...
    狂野的青春閱讀 289評(píng)論 0 4
  • 九月一日我與W相識(shí),他問(wèn)我你的愛(ài)好是什么,你喜歡什么樂(lè)器,我突然蒙了,其實(shí)這些年我一直在為了改變而學(xué)習(xí)或者工作,似...
    無(wú)言小傘閱讀 320評(píng)論 6 2

友情鏈接更多精彩內(nèi)容