MVC配置登錄攔截器


web.xml

代碼:
<servlet>
  <servlet-name>springDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springDispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 

applicationContext-mvc.xml
注意:<beans xsi:schemaLocation="">里面需要http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd,版本需要3.2以上才可以有后面的請求過濾mvc:exclude-mapping。

代碼:

<mvc:interceptors>
        <!-- 配置登陸攔截器 -->
        <mvc:interceptor>
            <!-- /** 表示所有的url,包括子url路徑 -->
            <mvc:mapping path="/**" />
            <!-- mvc:exclude-mapping過濾,js文件夾下的所有文件包括子路徑的文件都不攔截 -->

            <mvc:exclude-mapping path="/js/**"/>
            <!-- 配置攔截類  (com.***.filter包名;LoginHandlerIntercepter類名)-->

            <bean class="com.***.filter.LoginHandlerIntercepter"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    

  <!-- mvc:exclude-mapping配置的這里需要再配一次這個,主要是用來進行靜態資源的訪問,因為可以靜態資源(css.js)收到攔截器影響而獲取不到  -->
    <mvc:resources location="/js/" mapping="/js/**"/>

 

LoginHandlerIntercepter.java

代碼:

package com.***.filter;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.cyimayi.bean.User;

/**
 * 登陸攔截器 場景:用戶點擊查看的時候,我們進行登陸攔截器操作,判斷用戶是否登陸? 登陸,則不攔截,沒登陸,則轉到登陸界面; TODO 作者:原明卓
 * 時間:2016年1月8日 下午3:25:35 工程:SpringMvcMybatis1Demo
 */
public class LoginHandlerIntercepter implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object arg2, Exception arg3)
            throws Exception {
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse arg1, Object arg2) throws Exception {
        String requestURI = request.getServletPath();
        String[] arr = {"/user/login", ""};
        List<String> list = Arrays.asList(arr);
        
        if (!list.contains(requestURI)) {
            // 說明處在編輯的頁面
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                // 登陸成功的用戶
                return true;
            } else {
                session.setAttribute("lastUrl", requestURI);//保存路徑,登錄后跳回攔截前的頁面
                // 沒有登陸,轉向登陸界面
                request.getRequestDispatcher("/login.jsp").forward(request, arg1);
                return false;
            }
        } else {
            return true;
        }
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM