对request请求数据进行拦截

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值