`
knight_black_bob
  • 浏览: 821396 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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);
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics