<p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">1、日志记录:</code><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。</code></p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">2、权限检查:</code><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;</code></p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">3、性能监控:</code><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);</code></p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">4、通用行为:</code><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实现。</code></p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">5、OpenSessionInView:</code><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">如Hibernate,在进入处理器打开Session,在完成后关闭Session。</code></p>
<p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><strong style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">preHandle:</strong>预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器;<code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;"></code></p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><code style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;"> <span> </span></code> 返回值:true表示继续流程(如调用下一个拦截器或处理器);</p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"> false表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器,此时我们需要通过response来产生响应;</p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><strong style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">postHandle:</strong>后处理回调方法,实现处理器的后处理(但在渲染视图之前),此时我们可以通过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行处理,modelAndView也可能为null。</p><p style="padding: 0px; margin: 0px; font-size: 14px; font-family: verdana, arial, helvetica, sans-serif; line-height: 21px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><strong style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">afterCompletion:</strong>整个请求处理完毕回调方法,即在视图渲染完毕时回调,如性能监控中我们可以在此记录结束时间并输出消耗时间,还可以进行一些资源清理,类似于try-catch-finally中的finally,但仅调用处理器执行链中<strong style="padding: 0px; margin: 0px; font-size: 14px; line-height: 1.5em;">preHandle返回true的拦截器的afterCompletion</strong>。</p>
package com.bibenet.middleware.components.aspect.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.bibenet.middleware.components.config.ConfigFacade;
import com.bibenet.middleware.components.orm.page.Page;
import com.bibenet.middleware.utils.DataSection;
/**
* 配合orm分页使用的拦截器,拦截所有格式为xxx/xxpage.xx(htm/json)的请求<br>
* 如果请求包含分页参数pageNum 和 pageSize 且两个值为有效值则按照上传规则分页,<br>
* 如果请求中不包含分页参数,则pageNum=1 pageSize=defaultSize<br>
* defaultSize可在配置拦截器的时候注入,若未注入则使用默认值10
* @author fanwt
*
*/
public class PageInteceptor implements HandlerInterceptor {
public static final String PAGE = "page";
private int defaultSize = ConfigFacade.getCfg().getInt("page.defaultPageSize");
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
if(StringUtils.substringBeforeLast(uri, ".").endsWith("Page")){
String pageNum = request.getParameter("pageNum");
String pageSize = request.getParameter("pageSize");
String sortField = request.getParameter("sortField");
String sortType = request.getParameter("sortType");
Page page = new Page();
page.setPageNum(isPositiveNumber(pageNum) ? Integer.parseInt(pageNum.trim()) : 1);
page.setPageSize(isPositiveNumber(pageSize) ? Integer.parseInt(pageSize.trim()) : defaultSize);
page.setSortField(sortField);
page.setSortType(sortType);
DataSection.put(page);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//以用户请求返回优先,此处用在多系统之间调用时覆盖查询结果
if(modelAndView != null && modelAndView.getModelMap().containsKey(PAGE)){
return ;
}
//如果当前线程中包含有Page对象,则将其添加到modelAndView中
if(DataSection.containsKey(Page.class) && modelAndView != null){
modelAndView.getModelMap().addAttribute(PAGE, DataSection.get(Page.class));
}
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
//请求完全结束时要清理当前请求在本线程的数据遗留,避免影响到其他请求
DataSection.removeAll();
}
public int getDefaultSize() {
return defaultSize;
}
public void setDefaultSize(int defaultSize) {
this.defaultSize = defaultSize;
}
private boolean isPositiveNumber(String str){
return StringUtils.isNotBlank(str) && NumberUtils.isNumber(str.trim()) && Integer.parseInt(str.trim()) > 0;
}
}
Spring拦截器
最新推荐文章于 2020-09-09 10:22:00 发布