springmvc拦截器



1.定义拦截器

package com.lx.interceptor;

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.lx.utils.RequestUtil;

public class LoginInterceptor implements HandlerInterceptor {
    private List<String> excludeUrls;
	public List<String> getExcludeUrls() {
		return excludeUrls;
	}

	public void setExcludeUrls(List<String> excludeUrls) {
		this.excludeUrls = excludeUrls;
	}

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		// 设置字符编码
				request.setCharacterEncoding("UTF-8");
				// 判断是否登录(只拦截前台登录)
				HttpSession session = request.getSession();				
				String requestUrl = RequestUtil.getRequestPath(request);				
				for (String excludeUrl : excludeUrls) {
					if (requestUrl.indexOf(excludeUrl) != -1) {
						return true;
					}
				}
				String s=(String) session.getAttribute("123");
				System.out.println(s);
				if("123".equals(s)) {
					System.out.println("session tong______");
					return true;}
				response.sendRedirect(request.getContextPath() + "/login.jsp");
				return false;
	}

}
2、在spring-mvc.xml中配置

<mvc:interceptors>
   <bean class="com.lx.interceptor.LoginInterceptor">
<property name="excludeUrls">
<list>
<value>/login</value>
</list>
</property>
</bean>
</mvc:interceptors>

3、RequestUtil中的内容

package com.lx.utils;

import javax.servlet.http.HttpServletRequest;


public class RequestUtil {

	
	public static String getRequestPath(HttpServletRequest request) {
		String requestPath = request.getRequestURI();
		requestPath = requestPath.substring(request.getContextPath().length());// 鍘绘帀椤圭洰璺緞
		return requestPath;
	}

	
	private static final String[] PROXY_REMOTE_IP_ADDRESS = { "X-Forwarded-For", "X-Real-IP" };
	
	
	public static String getRemoteIp( HttpServletRequest request ) {
	    for ( int i = 0 ; i < PROXY_REMOTE_IP_ADDRESS.length ; i++ ) {
	        String ip = request.getHeader( PROXY_REMOTE_IP_ADDRESS[i] );
	        if ( ip != null && ip.trim().length() > 0 ) {
	            return getRemoteIpFromForward( ip.trim() );
	        }
	    }
	    return request.getRemoteHost();
	}
	 
	
	private static String getRemoteIpFromForward( String xforwardIp ) {
	    int commaOffset = xforwardIp.indexOf( ',' );
	    if ( commaOffset < 0 ) {
	        return xforwardIp;
	    }
	    return xforwardIp.substring( 0 , commaOffset );
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值