当前位置:   article > 正文

HandlerMethodArgumentResolver_handlermethodargumentresolver csdn

handlermethodargumentresolver csdn

HandlerMethodArgumentResolver

    <!-- json post请求 解码,登录校验 -->
    <mvc:annotation-driven>
        <mvc:argument-resolvers>
            <bean class="ccx.common.springext.resolver.JsonArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <!-- 自定义异常处理器 -->
    <bean id="exceptionHandler" class="ccx.common.springext.resolver.DefaultExceptionResolver"/>
    <!-- code加载 -->
    <bean id="resultLoad" name="ResultLoad" class="com.ccx.framework.result.ResultLoad"></bean>

    如果遇到
org.xml.sax.SAXParseException; lineNumber: 44; columnNumber: 26; cvc-complex-type.2.1: 元素 'mvc:annotation-driven' 必须不含字符或元素信息项 [子级], 因为该类型的内容类型为空。

可以改为3.1以上,3.0 不支持mvc:annotation-driven
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
package ccx.common.springext.resolver;

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import ccx.common.code.IResult;
import ccx.common.code.ResultKey;
import ccx.common.entity.Request;
import ccx.common.exception.ParameterException;
import ccx.common.util.StringUtil;
import ccx.common.util.UserTokenTools;
import ccx.common.util.rsa.RSA;
import ccx.common.util.rsa.RSAKeys;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * json格式校验
 * 
 * @author sunlihuo
 *
 */
public class JsonArgumentResolver implements HandlerMethodArgumentResolver {

    private static final Logger logger = LogManager.getLogger(JsonArgumentResolver.class);

    private static final String JSONBODYATTRIBUTE = "JSON_REQUEST_BODY";

    private IResult resultLoad;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(JsonRequestBody.class)) {
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            resultLoad = (IResult) wac.getBean("resultLoad");
            return true;
        }

        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
            NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        String uri = servletRequest.getRequestURI();

        // 取 JsonRequestBody 参数
        // boolean isArray = false;
        // Annotation[] methodAnnotations = parameter.getParameterAnnotations();
        // for (Annotation annotation : methodAnnotations) {
        // if (annotation instanceof JsonRequestBody) {
        // JsonRequestBody ssonRequestBody = (JsonRequestBody) annotation;
        // isArray = ssonRequestBody.isArray();
        // }
        // }

        // 取请求参数
        String jsonEncode = getRequestBody(webRequest);

        // json解密
        String jsonDecode = JsonDecode(jsonEncode);

        // 转json
        JSONObject json = toJsonObject(jsonDecode);

        // 校验登录
        CheckLogin(uri, json);

        // jsonobject 转bean
        Object obj = null;
        Method method = parameter.getMethod();
        Class<?>[] zlasss = method.getParameterTypes();
        for (Class<?> zlass : zlasss) {
            if (Request.class.isAssignableFrom(zlass)) {
                try {
                    obj = JSONObject.toBean(json, zlass);
                } catch (Exception e) {
                    throw new ParameterException(20000, "json to bean error", e);
                }
                continue;
            }
        }
        return obj;

    }

    /**
     * app json统一解密
     * 
     * @param jsonEncode
     * @return
     */
    private String JsonDecode(String jsonEncode) {
        String requestBody = jsonEncode;
        // boolean isproductEnv = PropertiesConfig.getProperty("is_product_env",
        // 0) == 1;// 是否生产环境(1:是,0:否)
        if (true) {
            // RSA解密
            try {
                requestBody = RSA.decrypt(jsonEncode, RSAKeys.default_private_key, "utf-8");
            } catch (Exception e) {
                // 解密失败
                logger.error("请求报文解密失败,非法请求 = " + requestBody, e);
                throw new ParameterException(resultLoad.getResult(ResultKey.JSON_FORMAT_ERROR));
            } finally {
                if (StringUtil.isEmpty(requestBody)) {
                    // 返回异常结果
                    throw new ParameterException(resultLoad.getResult(ResultKey.msg_unpass_fail));
                }
            }
        }
        return requestBody;
    }

    /**
     * 
     * @param jsonDecode
     * @return
     */
    private JSONObject toJsonObject(String jsonDecode) {
        JSONObject val = null;
        try {
            val = JSONObject.fromObject(jsonDecode);
        } catch (Exception e) {
            throw new ParameterException(resultLoad.getResult(ResultKey.JSON_FORMAT_ERROR));
        }
        return val;
    }

    private JSONArray toJsonArray(String jsonDecode) {
        JSONArray array = null;
        try {
            array = JSONArray.fromObject(jsonDecode);
        } catch (Exception e) {
            throw new ParameterException(resultLoad.getResult(ResultKey.JSON_FORMAT_ERROR));
        }
        return array;
    }

    /**
     * 校验登录
     * 
     * @param requestJson
     * @return
     */
    private void CheckLogin(String uri, JSONObject requestJson) {
        boolean checkLogin = false;
        if (uri.toLowerCase().indexOf("/private") != -1) {
            checkLogin = true;
        }

        // 校验登录
        if (checkLogin) {
            // 判断token是否有效
            String token = requestJson.getString("token");
            if (StringUtil.isEmpty(token)) {
                // 返回登录
                throw new ParameterException(resultLoad.getResult(ResultKey.login_expire));
            }

            String checkToken = UserTokenTools.refreshToken(token);
            if (null == checkToken) {
                // token已失效
                throw new ParameterException(resultLoad.getResult(ResultKey.login_expire));
            }
        }

    }

    /**
     * 取请求参数
     * 
     * @param webRequest
     * @return
     */
    private String getRequestBody(NativeWebRequest webRequest) {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);

        String jsonBody = (String) webRequest.getAttribute(JSONBODYATTRIBUTE, NativeWebRequest.SCOPE_REQUEST);
        if (jsonBody == null) {
            try {
                // 把reqeust的body读取到StringBuilder
                BufferedReader reader = servletRequest.getReader();
                StringBuilder sb = new StringBuilder();
                char[] buf = new char[1024];
                int rd;
                while ((rd = reader.read(buf)) != -1) {
                    sb.append(buf, 0, rd);
                }
                jsonBody = sb.toString();

                webRequest.setAttribute(JSONBODYATTRIBUTE, jsonBody, NativeWebRequest.SCOPE_REQUEST);
            } catch (IOException e) {
                throw new ParameterException(20000, "Request reader 失败");
            } catch (IllegalStateException e) {
                throw new ParameterException(20000, "getInputStream() has already been called for this request");
            }
        }
        return jsonBody;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/917055
推荐阅读
相关标签
  

闽ICP备14008679号