spring interceptor 攔截方法,判斷用戶是否存在

//基於spring mvc+spring +mybatis的一個攔截方法,判斷用戶是否存在,從而進行頁面跳轉的小demo 核心文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  
    <mvc:annotation-driven />  
    <context:component-scan base-package="com.liyi.*" />  
    <!-- 引入jdbc配置文件 -->  
    <context:property-placeholder location="/WEB-INF/jdbc.properties" />  
     <!--建立jdbc數據源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
    </bean>  
  
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <!-- 建立SqlSessionFactory,同時指定數據源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
     <!-- Mapper接口所在包名,Spring會自動查找其下的Mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.liyi.dao" />  
    </bean>  
  
     <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    //攔截器配置
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.liyi.util.AuthInterceptor">
                <property name="includeUrls">
                    <list>
                        <value>/hello/test.do</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>  

//攔截器 首先要實現 HandlerInterceptor 裏面會重寫三個方法 根據業務需求選擇 寫代碼的邏輯塊
package com.liyi.util;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
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;

public class AuthInterceptor implements HandlerInterceptor{
    private List<String> includeUrls;
    
    public List<String> getIncludeUrls() {
        return includeUrls;
    }

    public void setIncludeUrls(List<String> includeUrls) {
        this.includeUrls = includeUrls;
    }
    /**
     * 在controller前攔截
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        // TODO Auto-generated method stub
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestUri.substring(contextPath.length());
//        String requestPath = ResourceUtil.getRequestPath(request);// 用戶訪問的資源地址
        HttpSession session = ContextHolderUtils.getSession();
        if(includeUrls.contains(url)){
            //若是當用戶登錄的狀況下 能夠查看用戶信息 若是未登陸跳轉到登錄頁面
            if (session.getAttribute("user") != null) {
                return true;
            }else{
                forwordweb(request, response);
                return false;
            }
        }else{
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        
    }
    /**
     * 在controller後攔截
     */

    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * 轉發前臺登陸
     * 
     * @param user
     * @param req
     * @return
     */
    public void forwordweb(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

//這樣每一個請求都會被攔截,去判斷是否在自定義路徑裏面,若是沒有,就返回true,若是有,就判斷用戶是否存在,若是不
//不存在就返回false,若是存在就返回true
相關文章
相關標籤/搜索