spring 拦截器

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
	xsi:schemaLocation="
	            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/oxm http://www.springframework.org/schema/oxm/spring-oxm-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	<!-- 激活Spring注解方式:自动扫描,并注入bean -->
	<context:component-scan base-package="com.cmcc.aoi.selfhelp.action" /> 
	<context:component-scan base-package="com.cmcc.aoi.selfhelp.wuxiancity.action" />

	
	<!-- 配置视图解析 -->
    <bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	 <!-- 默认的注解映射的支持 -->
	<mvc:annotation-driven/>
	
	
	
	
	<bean id="authInterceptor" class="com.cmcc.aoi.selfhelp.interceptor.AuthInterceptor" /> 
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="interceptors"> 
			<list>
			</list>
		</property>
	</bean> 
	
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/selfhelp/**" />
			<bean class="com.aoi.selfhelp.interceptor.AoiLoginAuthInterceptor" >
				<property name="excludeUrls">
					<list> 
					    <value>/selfhelp/sysUser/create</value>
						<value>/selfhelp/sysUser/checkLoginName</value>
						<value>/selfhelp/sysUser/checkEmail</value>
						<value>/selfhelp/sysUser/save</value>
						<value>/selfhelp/email/sendActivationEmail</value>
						<value>/selfhelp/email/sendRetrievePasswordEmail</value>
						<value>/selfhelp/activation/prepareActivation</value>
						<value>/selfhelp/activation/activate</value>
						<value>/selfhelp/retrievePassword/retrievePassword</value>
						<value>/selfhelp/retrievePassword/sendEmail</value>
						<value>/selfhelp/retrievePassword/rePassword</value> 
						<value>/selfhelp/tag/report</value>
					</list>
				</property>
			</bean>
		</mvc:interceptor>
		<!-- 
		<mvc:interceptor>
			<mvc:mapping path="/selfhelp/deliverWebRequest/**" />
			<bean class="com.aoi.selfhelp.interceptor.AoiAuthInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/selfhelp/multiple/**" />
			<bean class="com.aoi.selfhelp.interceptor.AoiAuthInterceptor" />
		</mvc:interceptor>
		 -->
	</mvc:interceptors>

	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
		<property name="exceptionMappings">
			<props>
				<prop key="com.aoi.selfhelp.exception.AuthException">redirect:/user/login.action</prop>
				<prop key="com.aoi.selfhelp.exception.SelfHelpException">redirect:/user/staticPageAction.action?action=error</prop>
			</props>
		</property>
		<property name="defaultErrorView" value="../../common/error"></property>
	</bean>
	 
	
</beans>

 

 

package com.aoi.selfhelp.interceptor;

import java.util.List;

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

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

import com.aoi.selfhelp.entity.SysUser;

public class AoiLoginAuthInterceptor implements HandlerInterceptor
{
	private List<String> excludeUrls;

	public List<String> getExcludeUrls()
	{
		return excludeUrls;
	}

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

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception
	{
		String requestUri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestUri.substring(contextPath.length());

		if (excludeUrls != null && excludeUrls.contains(url))
		{
			return true;
		}
		else if (request.getSession().getAttribute("user") == null)
		{
			response.sendRedirect("/user/login.action");
			return false;
		}
		// System.out.println("preHandle " + arg2);
		return true;
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception
	{
		// System.out.println("afterCompletion " + arg2);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception
	{
		// System.out.println("postHandle " + arg2);
	}
}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,spring,Web,拦截器)