filter过滤器特殊情况下直接放行

首先写一个filter类

@Component("accessLogFilter")
public class AccessLogFilter implements Filter, Serializable {


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		
	}
}

 

 

 

第二步:在web.xml里面配置filter,根据自己的需要配置需要拦截的请求

<filter>
		<filter-name>DelegatingFilterProxy</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetBeanName</param-name>
			<param-value>accessLogFilter</param-value>
		</init-param>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>DelegatingFilterProxy</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>

第三步:重写dofiter方法,举例:我这里是不拦截特定的url,即如果是特定的url就放行,不做任何操作。

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("jaxwan 我是操作日志");
		if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
			HttpServletRequest req = (HttpServletRequest) request;
			try {
				final String uri = req.getRequestURI().startsWith("/") ? req.getRequestURI().substring(1) : req.getRequestURI();
				//特殊情况 判断一下url是不是指定的url
				if(uri.contains("openapi/mobileServiceBLH_sendActMqInfo") || uri.contains("openapi/mobileServiceBLH_sendAcdmInfo")){
					chain.doFilter(request, response);
					return;
				}
				//正常拦截之后做的操作
				IUser user = (IUser) req.getSession().getAttribute("login_user");
				if (user != null) {
					chain.doFilter(request, response);
				} else {
					chain.doFilter(request, response);
				}
			} catch (Exception e) {
				chain.doFilter(request, response);
			}
		}
		return;
	}final String uri = req.getRequestURI().startsWith("/") ? req.getRequestURI().substring(1) : req.getRequestURI();
				//特殊情况 判断一下url是不是指定的url
				if(uri.contains("openapi/mobileServiceBLH_sendActMqInfo") || uri.contains("openapi/mobileServiceBLH_sendAcdmInfo")){
					chain.doFilter(request, response);
					return;
				}
				//正常拦截之后做的操作
				IUser user = (IUser) req.getSession().getAttribute("login_user");
				if (user != null) {
					chain.doFilter(request, response);
				} else {
					chain.doFilter(request, response);
				}
			} catch (Exception e) {
				chain.doFilter(request, response);
			}
		}
		return;
	}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

buyaopingbixiazai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值