码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc拦截器使用

时间:2017-10-26 18:52:09      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:login   ken   ret   lin   session   verify   单点登录   apach   break   

  举个例子说下吧,单点登录,直接看代码:

  spring-mvc.xml配置

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="index.html" />
            <mvc:exclude-mapping path="/login**" />
            <mvc:exclude-mapping path="/register**" />
            <mvc:exclude-mapping path="/getVerifyCode**" />
            <mvc:exclude-mapping path="/getMethod**" />
            <bean class="com.wulinfeng.test.testpilling.util.InterceptorUtil" />
        </mvc:interceptor>
    </mvc:interceptors>

  具体拦截器实现类:

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

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 登陆拦截
 *
 * @author wulinfeng
 * @version C10 2017年10月11日
 * @since SDP V300R003C10
 */
public class InterceptorUtil implements HandlerInterceptor
{
    /** 日志对象 */
    private static Logger logger = LogManager.getLogger(InterceptorUtil.class.getName());
    
    /** 是否启用cookie */
    private static final String IS_COOKIE = PropertiesConfigUtil.getProperty("iscookie");
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception
    {
        logger.debug("InterceptorUtil.doFilter requesturl: " + request.getRequestURL());
        
        String tokenId = null;
        
        if (IS_COOKIE.equals("1"))
        {
            Cookie[] cookies = request.getCookies();
            if (cookies != null)
            {
                for (Cookie cookie : cookies)
                {
                    if (cookie == null)
                    {
                        continue;
                    }
                    if (cookie.getName().equals("tokenId"))
                    {
                        tokenId = cookie.getValue();
                        break;
                    }
                }
            }
        }
        else
        {
            if (request.getSession() != null)
            {
                tokenId = (String)request.getSession().getAttribute("tokenId");
            }
        }
        
        if (StringUtils.isEmpty(tokenId))
        {
            response.sendRedirect("/login.html");
        }
        
        return true;
    }
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView)
        throws Exception
    {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception
    {
        // TODO Auto-generated method stub
        
    }
    
}

 

spring mvc拦截器使用

标签:login   ken   ret   lin   session   verify   单点登录   apach   break   

原文地址:http://www.cnblogs.com/wuxun1997/p/7738195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!