package com.krt.framework.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.krt.common.annotation.KrtIgnoreAuth;
import com.krt.common.bean.ReturnBean;
import com.krt.common.util.ServletUtils;
import com.krt.health.dto.UserPatientInfoDTO;
import com.krt.health.entity.UserPatientInfo;
import com.krt.health.utils.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Parameter;
@Component
public class MyHandlerIntercption extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod;
if (handler instanceof HandlerMethod) {
handlerMethod = ((HandlerMethod) handler);
} else {
return true;
}
MethodParameter[] p = handlerMethod.getMethodParameters();
for (MethodParameter methodParameter : p) {
Parameter parameter = methodParameter.getParameter();
Boolean x = checkParamer(request, response, parameter);
if (x != null) return x;
}
return true;
}
private Boolean checkParamer(HttpServletRequest request, HttpServletResponse response, Parameter parameter) throws IOException {
if (parameter.getName().indexOf("userPhone") != -1) {
String userPhone = request.getParameter(parameter.getName());
if (StringUtils.isNull(userPhone) != null || "undefined".equalsIgnoreCase(userPhone)) {
ServletUtils.printJson(response, JSON.toJSONString(ReturnBean.error("phone is null!")));
return false;
}
if (!(StringUtils.isPhone(userPhone) || StringUtils.isTelephone(userPhone))) {
ServletUtils.printJson(response, JSON.toJSONString(ReturnBean.error("phone is error!")));
return false;
}
} else if (parameter.getType().getSimpleName().equalsIgnoreCase("userPatientInfoDTO")) {
BufferedReader br;
br = request.getReader();
String str, wholeStr = "";
while ((str = br.readLine()) != null) {
wholeStr += str;
}
UserPatientInfoDTO userPatientInfoDTO = JSON.parseObject(wholeStr, UserPatientInfoDTO.class);
String userPhone = userPatientInfoDTO.getUserPhone();
if (StringUtils.isNull(userPhone) != null || "undefined".equalsIgnoreCase(userPhone)) {
ServletUtils.printJson(response, JSON.toJSONString(ReturnBean.error("phone is null!")));
return false;
}
if (!(StringUtils.isPhone(userPhone) || StringUtils.isTelephone(userPhone))) {
ServletUtils.printJson(response, JSON.toJSONString(ReturnBean.error("phone is error!")));
return false;
}
} else if ("messageId".equalsIgnoreCase(parameter.getName())) {
String messageId = request.getParameter(parameter.getName());
if (messageId != null && new Integer(messageId) < 1) {
ServletUtils.printJson(response, JSON.toJSONString(ReturnBean.error("没有查询到该留言!!!")));
return false;
}
}
return null;
}
}
对request请求数据进行拦截
最新推荐文章于 2024-08-01 21:16:32 发布