55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
package com.tiesheng.encrypt.config;
|
|
|
|
import cn.hutool.core.annotation.AnnotationUtil;
|
|
import cn.hutool.log.LogFactory;
|
|
import com.tiesheng.util.CommonUtil;
|
|
import com.tiesheng.util.config.EncryptConfig;
|
|
import com.tiesheng.util.pojos.ApiResp;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.MethodParameter;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
import org.springframework.http.server.ServerHttpRequest;
|
|
import org.springframework.http.server.ServerHttpResponse;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
|
|
|
/**
|
|
* @author hao
|
|
*/
|
|
@ControllerAdvice
|
|
public class EncryptResponseBodyAdvice implements ResponseBodyAdvice<ApiResp> {
|
|
|
|
@Autowired
|
|
EncryptConfig encryptConfig;
|
|
|
|
@Override
|
|
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
return AnnotationUtil.getAnnotation(returnType.getContainingClass(), RestController.class) != null;
|
|
}
|
|
|
|
@Override
|
|
public ApiResp beforeBodyWrite(ApiResp body, MethodParameter returnType, MediaType selectedContentType, Class<? extends
|
|
HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
|
|
|
try {
|
|
|
|
Object data = body.getData();
|
|
if (data == null || !body.successful()) {
|
|
return body;
|
|
}
|
|
|
|
body.setEncrypt(true);
|
|
body.setData(encryptConfig.encrypt(CommonUtil.writeJsonString(data)));
|
|
return body;
|
|
} catch (Exception var17) {
|
|
LogFactory.get().info("加密数据异常", var17);
|
|
}
|
|
|
|
return body;
|
|
}
|
|
|
|
}
|