springboot2.2.X手册:防抓包?快速实现API接口数据加密( 二 )

编写加密判断类/** * All rights Reserved, Designed By 林溪 * Copyright:Copyright(C) 2016-2020 * Company溪云阁 . */package com.module.boots.api.de;import org.springframework.core.MethodParameter;/** * 是否需要加密解密 * @author:溪云阁 * @date:2020年6月4日 */public class NeedDe {/*** 判断是否需要加密* @author 溪云阁* @param returnType* @return boolean*/public static boolean needEncrypt(MethodParameter returnType) {boolean encrypt = false;// 获取类上的注解final boolean classPresentAnno = returnType.getContainingClass().isAnnotationPresent(ResponseEncrypt.class);// 获取方法上的注解final boolean methodPresentAnno = returnType.getMethod().isAnnotationPresent(ResponseEncrypt.class);if (classPresentAnno) {// 类上标注的是否需要加密encrypt = returnType.getContainingClass().getAnnotation(ResponseEncrypt.class).value();// 类不加密,所有都不加密if (!encrypt) {return false;}}if (methodPresentAnno) {// 方法上标注的是否需要加密encrypt = returnType.getMethod().getAnnotation(ResponseEncrypt.class).value();}return encrypt;}}编写加密拦截/** * All rights Reserved, Designed By 林溪 * Copyright:Copyright(C) 2016-2020 * Company溪云阁 . */package com.module.boots.api.de;import org.springframework.beans.factory.annotation.Value;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.servlet.mvc.method.annotation.ResponseBodyAdvice;import com.module.boots.api.de.utils.AesUtils;import com.module.boots.api.message.ResponseMsg;/** * 对接口数据进行加密 * @author:溪云阁 * @date:2020年6月4日 */@ControllerAdvicepublic class ResponseEncryptAdvice implements ResponseBodyAdvice<Object> {@Value("${module.boots.response.aes.key}")private String key;@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return true;}/*** 在写入之前更改body的值* @author 溪云阁* @param body* @param returnType* @param selectedContentType* @param selectedConverterType* @param request* @param response* @return* @return*/@SuppressWarnings({ "unchecked", "rawtypes" })@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,ServerHttpResponse response) {// 判断是否需要加密final boolean encrypt = NeedDe.needEncrypt(returnType);if (!encrypt) {return body;} else {// 如果body是属于ResponseMsg类型,只需要对data里面的数据进行加密即可if (body instanceof ResponseMsg) {final ResponseMsg responseMsg = (ResponseMsg) body;final Object data = https://www.isolves.com/it/cxkf/jiagou/2020-06-20/responseMsg.getData();if (data == null) {return body;} else {responseMsg.setData(AesUtils.encrypt(data.toString(), key));return responseMsg;}} else {return body;}}}}加入密钥# aes的密钥module.boots.response.aes.key: xy934yrn9342u0ry4br8cn-9u2编写加密解密接口/** * All rights Reserved, Designed By 林溪 * Copyright:Copyright(C) 2016-2020 * Company溪云阁 . */package com.boots.api.de.view.de.view;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.GetMApping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.boots.api.de.view.de.vo.GetEncryptVO;import com.module.boots.api.de.ResponseEncrypt;import com.module.boots.api.de.utils.AesUtils;import com.module.boots.api.message.ResponseMsg;import com.module.boots.api.utils.MsgUtils;import com.module.boots.exception.CommonRuntimeException;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.SneakyThrows;/** * 加密数据接口 * @author:溪云阁 * @date:2020年6月4日 */@SuppressWarnings("deprecation")@Api(tags = { "web服务:加密数据接口" })@RestController@RequestMapping("view/deView")public class DeView {@Value("${module.boots.response.aes.key}")private String key;/*** 获取加密数据* @author 溪云阁* @return ResponseMsg<GetDeVO>*/@ApiOperation(value = https://www.isolves.com/it/cxkf/jiagou/2020-06-20/"获取加密数据")@GetMapping(value = "/getEncrypt", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@SneakyThrows(CommonRuntimeException.class)@ResponseEncryptpublic ResponseMsg getEncrypt() {final GetEncryptVO vo = new GetEncryptVO();vo.setId("b037123c");vo.setUserName("xnwqr98urx");return MsgUtils.buildSuccessMsg(vo);}/*** 获取解密数据* @author 溪云阁* @return ResponseMsg


推荐阅读