繼承DispatcherServlet覆蓋noHandlerFound方法
#### web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>com.peppa.crm.web.manage.common.CrmDispatchServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
#### 代碼
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.peppa.crm.web.manage.controller.CommonController;
import com.peppa.crm.web.manage.viewmodel.Response;
import com.peppa.domain.dto.EmployeeDto;
import com.peppa.domain.enums.SystemNoticeTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @program: 404錯(cuò)誤攔截并記錄
* @description:
* @author: zhoupengfei
* @create: 2019-05-28 16:17
**/
public class CrmDispatchServlet extends DispatcherServlet {
protected static final Logger logger = LoggerFactory.getLogger(CrmDispatchServlet.class);
/**圖片忽略**/
public static final String[] ignoreUrl={"favicon.ico","unauthorized"};
public static final String crmVersion="crmVersion";
public static final String logout="logout";
@Override
public void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
//忽略的url
for(String ignore:ignoreUrl){
if(request.getRequestURL().toString().contains(ignore)){
super.noHandlerFound(request,response);
return;
}
}
//獲取客戶端版本
int clientVersion=0;
String v=getRequestVersion(request);
if(StringUtils.isNotEmpty(v)){
clientVersion=Integer.parseInt(v);
}
Subject subject = SecurityUtils.getSubject();
Session session=subject.getSession();
EmployeeDto employeeDto= (EmployeeDto) session.getAttribute("CurrentUser");
if(clientVersion<CommonController.crmVersion){
//版本判斷
if(employeeDto!=null){
HashMap<String,Object> param=new HashMap<>(8);
param.put("bizType", SystemNoticeTypeEnum.WS_GO_URL.getValue());
List<Integer> userIdList=new ArrayList();
userIdList.add(employeeDto.getId());
param.put("ids", userIdList);
if(CommonController.crmVersion-clientVersion>1){
//中間有多個(gè)版本暫時(shí)直接退出
param.put("msg",logout);
}else{
param.put("msg",CommonController.versionAction);
}
HttpRequest.post("http://"+CommonController.wsServerUrl+"/pushMsgController/pushMsg")
.body(JSON.toJSONString(param))
.execute();
}
}
StringBuffer log=new StringBuffer("資源不存在url="+request.getRequestURL());
if(employeeDto!=null){
log.append(",用戶Id="+employeeDto.getId());
}
logger.error(log.toString());
Response r=new Response();
r.failure("您訪問(wèn)的資源不存在,url="+request.getRequestURL()+",請(qǐng)退出重新登錄.").code(HttpStatus.NOT_FOUND.value());
response.setContentType("application/json;charset=utf-8");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
response.getWriter().print(JSON.toJSONString(r));
}
/**
* 獲取請(qǐng)求的版本
*/
private String getRequestVersion(HttpServletRequest httpRequest) {
//從header中獲取token
String version = httpRequest.getHeader(crmVersion);
//如果header中不存在token,則從參數(shù)中獲取token
if (StringUtils.isEmpty(version)) {
version = httpRequest.getParameter(crmVersion);
}
if (StringUtils.isEmpty(version)) {
//獲取cookie數(shù)組
Cookie[] cookies = httpRequest.getCookies();
if (cookies == null) {
return null;
}
//遍歷cookie數(shù)組
for (Cookie cookie : cookies) {
if (cookie.getName().equals(crmVersion)) {
version = cookie.getValue();
break;
}
}
}
return version;
}
|