编写加密判断类/** * 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
推荐阅读
- 航天员|实拍返回舱内航天员画面:三人翻看工作手册、舱内装满“包裹”
- 最新百度信息流产品手册,带你全面了解百度产品
- MySQL使用规范手册,程序员必知必会
- 在 Linux 中遨游手册页的海洋
- Linux 监测运维速成手册
- 小茶人编辑出版茶叶小百科手册茶的妙用
- 企业会务工作操作手册,从会前准备到会议总结,涵盖各方面
- 这样用毛巾细菌超百万!这份“使用手册”值得收藏
- 旅游和春节更配 但你需要备好一份安全手册!
- SEO网站审查手册,有哪六个必备须知?
