• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
cnsdhzzl
博客园    首页    新随笔    联系   管理    订阅  订阅
springmvcIntercept(拦截器)

1.创建拦截器

public class MyIntercept implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        System.out.println("MyIntercept.afterCompletion()");
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("MyIntercept.postHandle()");
    }

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        System.out.println("MyIntercept.preHandle()");
        return true;
    }

}

2.配置applicationContext.xml

<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="cn.cnsdhzzl.intercept.MyIntercept">
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

访问效果

 

2. 设置不拦截方法1

设置不拦截器路径

 

<mvc:exclude-mapping path="/user/login"/>

 

 

 

 

 

<mvc:interceptors>
        <bean class="com.elin4it.ssm.interceptor.DataInterceptor"/>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/user/login"/>
            <bean class="cn.xxx.xxx.interceptor.DataInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

 

 

 

3. 设置不拦截方法2

在拦截器里注入一个属性List<String> exceptUrls

<mvc:interceptors>  
     <mvc:interceptor>
         <mvc:mapping path="/**"/>
         <bean class="cn.rhgk.yqfy.oa.intercept.LoginIntercept">
             <property name="exceptUrls">  
                 <list>  
                     <value>/login/auth</value>  
                     <value>/login/exit</value>
                 </list>  
             </property>
         </bean>  
     </mvc:interceptor>  
 </mvc:interceptors>

 

package cn.rhgk.yqfy.oa.intercept;

import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.rhgk.yqfy.oa.pojo.AppAdminLogin;

public class LoginIntercept implements HandlerInterceptor {

    private List<String> exceptUrls;

    public List<String> getExceptUrls() {
        return exceptUrls;
    }

    public void setExceptUrls(List<String> exceptUrls) {
        this.exceptUrls = exceptUrls;
    }

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, 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 response, Object handler)
            throws Exception {

        String requestUri = request.getRequestURI();
        if (requestUri.startsWith(request.getContextPath())) {
            requestUri = requestUri.substring(request.getContextPath().length(), requestUri.length());
        }
        // 系统根目录
        if (StringUtils.equals("/", requestUri)) {
            return true;
        }

        // 放行exceptUrls中配置的url
        for (String url : exceptUrls) {
            if (url.endsWith("/**")) {
                if (requestUri.startsWith(url.substring(0, url.length() - 3))) {
                    return true;
                }
            } else if (requestUri.startsWith(url)) {
                return true;
            }
        }

        HttpSession session = request.getSession();
        AppAdminLogin user = (AppAdminLogin) session.getAttribute("userInfo");

        if (null != user) {// 已登陆
            return true;
        } else {// 当前用户未登陆
            response.sendRedirect(request.getContextPath() + "/login/auth");
        }

        return false;
    }

}

 

posted on 2016-11-28 14:33  cnsdhzzl  阅读(2319)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3