首先写一个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;
}