因为我们是金融行业,所以对数据的安全是很重视,为了防止返回信息明文返回,我们对返回内容进行了处理
package com.demo.interceptor;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
/**
* 统一处理返回值信息
*/
@SuppressWarnings("rawtypes")
@ControllerAdvice
public class MyResponseBody implements ResponseBodyAdvice{
private static Logger logger = LoggerFactory.getLogger(MyResponseBody.class);
/*
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#supports(org.springframework.core.MethodParameter, java.lang.Class)
* 这里直接返回true,表示对任何handler的responsebody都调用beforeBodyWrite方法
*/
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
logger.info(" responseBody 处理 .............. in ");
logger.info("请求反回结果Object:{}", JSON.toJSONString(o));
//这里可以对返回结果进行加密、验签等操作
logger.info("responseBody 处理 .............. out {}", JSON.toJSONString(o));
return o;
}
}
注:通过supports方法,我们可以选择哪些类,或者哪些方法要对response进行处理,其余的则不处理