SpringMVC 攔截器HandlerInterceptorAdapter


<!-- 測試公共校驗攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/crm/**"/>
<mvc:exclude-mapping path="*/crm/resource/download,*/crm/home/homeInit"/>
<bean class="com.test.interceptor.DemoInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>




import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.alibaba.fastjson.JSONObject;
import com.test.exception.DemoValidateException;
import com.test.common.PublicResponse;
import com.test.bean.UserInfo;
import com.test.service.CrmService;
/**
* 測試公共校驗攔截器
*/
public class DemoInterceptor extends HandlerInterceptorAdapter {
private static final Log logger = LogFactory.getLog(DemoInterceptor.class);
@Autowired
private CrmService crmService;
/**
* 在業務處理器處理請求之前被調用
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
PrintWriter out=null;
String logStr="DemoInterceptor.preHandle...";
logger.info(logStr+"攔截器preHandle方法");
try{
UserInfo userInfo=crmService.validateMethod(request);
logger.info(logStr+String.format("攔截校驗通過:token=%s, servletPath=%s, uri=%s, url=%s", request.getHeader("TOKEN_KEY_NAME_DEMO"),request.getServletPath(),request.getRequestURI(),request.getRequestURL()));
/*
打印結果:
token=CEEADEC1C6591A84E08DCE54136608BC3,
servletPath=, //取不到
uri=/crm/stu/init,
url=http://192.168.12.100:9898/crm/stu/init
*/
request.setAttribute("CRM_REQUEST_USERINFO_DEMO", userInfo);
return true;
}catch(DemoValidateException e){
logger.error(logStr+"公共校驗異常");
PublicResponse rsp=new PublicResponse();
rsp.setRspCode(e.getCode());
rsp.setRspMsg(e.getMessage());
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out = response.getWriter();
out.print(JSONObject.toJSONString(rsp));
logger.info(logStr+String.format("攔截校驗未通過:token=%s, rsp=%s", request.getHeader("TOKEN_KEY_NAME_DEMO"),JSONObject.toJSONString(rsp)));
out.flush();
}finally {
if(null!=out){
out.close();
}
}
return false;
}

/**
* 在業務處理器處理請求完成之後,生成視圖之前執行
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
String logStr="DemoInterceptor.postHandle...";
logger.info(logStr+"攔截器postHandle方法");
super.postHandle(request, response, handler, modelAndView);
}
/**
* 在DispatcherServlet完全處理完請求之後被調用,可用於清理資源
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
super.afterCompletion(request, response, handler, ex);
}
/**
* 處理異步請求(當Controller中有異步請求方法的時候會觸發該方法)
*/
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
super.afterConcurrentHandlingStarted(request, response, handler);
}

}



@RequestMapping(value = "/stu/init", method = RequestMethod.GET)
@ResponseBody
public PublicResponse testControllerMethod(final HttpServletRequest request, final HttpServletResponse response){
PublicResponse rsp=new PublicResponse();
//add code here
try{
//從攔截器過來的request中獲取UserInfo
UserInfo stu=(UserInfo)request.getAttribute("CRM_REQUEST_USERINFO_DEMO");
logger.info(logStr+"測試stu="+stu.toString());
}catch(Exception e){
e.printStackTrace();
logger.error("接口異常");
}
logger.info("接口響應rsp="+rsp.toString());
return rsp;
}


參考博客:
-- SpringMvc中使用Interceptor與Filter的注意點
[url]https://www.cnblogs.com/FlyHeLanMan/p/6640941.html[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章