SpringBoot中如何灵活的实现接口数据的加解密...
- 自定义消息转换器
劣势:仅能对同一类型的MediaType进行加解密操作,不灵活。
- 使用spring提供的接口RequestBodyAdvice和ResponseBodyAdvice
二、实现原理
public interface RequestBodyAdvice {
boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType);
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
@Nullable
Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
}
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
MediaType contentType;
boolean noContentType = false;
try {
contentType = inputMessage.getHeaders().getContentType();
}
catch (InvalidMediaTypeException ex) {
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}
if (contentType == null) {
noContentType = true;
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
Class<?> contextClass = parameter.getContainingClass();
Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
if (targetClass == null) {
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
targetClass = (Class<T>) resolvableType.resolve();
}
HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod() : null);
Object body = NO_VALUE;
EmptyBodyCheckingHttpInputMessage message;
try {
message = new EmptyBodyCheckingHttpInputMessage(inputMessage);
for (HttpMessageConverter<?> converter : this.messageConverters) {
Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
GenericHttpMessageConverter<?> genericConverter =
(converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null);
if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) :
(targetClass != null && converter.canRead(targetClass, contentType))) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (message.hasBody()) {
HttpInputMessage msgToUse =
getAdvice().beforeBodyRead(message, parameter, targetType, converterType);
body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) :
((HttpMessageConverter<T>) converter).read(targetClass, msgToUse));
body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);
}
break;
}
}
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
}
if (body == NO_VALUE) {
if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
(noContentType && !message.hasBody())) {
return null;
}
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
}
return body;
}
public interface ResponseBodyAdvice<T> {
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
@Nullable
T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response);
}
if (selectedMediaType != null) {
selectedMediaType = selectedMediaType.removeQualityValue();
for (HttpMessageConverter<?> converter : this.messageConverters) {
GenericHttpMessageConverter genericConverter =
(converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null);
if (genericConverter != null ?
((GenericHttpMessageConverter) converter).canWrite(declaredType, valueType, selectedMediaType) :
converter.canWrite(valueType, selectedMediaType)) {
outputValue = (T) getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType,
(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
inputMessage, outputMessage);
if (outputValue != null) {
addContentDispositionHeader(inputMessage, outputMessage);
if (genericConverter != null) {
genericConverter.write(outputValue, declaredType, selectedMediaType, outputMessage);
}
else {
((HttpMessageConverter) converter).write(outputValue, selectedMediaType, outputMessage);
}
if (logger.isDebugEnabled()) {
logger.debug("Written [" + outputValue + "] as \"" + selectedMediaType +
"\" using [" + converter + "]");
}
}
return;
}
}
}
三、实战
- pom.xml中引入jar
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
</dependencies>
- 请求参数解密拦截类
/**
* 请求参数 解密操作
*
* @Author: Java碎碎念
* @Date: 2019/10/24 21:31
*
*/
@Component
@ControllerAdvice(basePackages = "com.example.springbootencry.controller")
@Slf4j
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {
return inputMessage;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
String dealData = null;
try {
//解密操作
Map<String,String> dataMap = (Map)body;
String srcData = dataMap.get("data");
dealData = DesUtil.decrypt(srcData);
} catch (Exception e) {
log.error("异常!", e);
}
return dealData;
}
@Override
public Object handleEmptyBody(@Nullable Object var1, HttpInputMessage var2, MethodParameter var3, Type var4, Class<? extends HttpMessageConverter<?>> var5) {
log.info("3333");
return var1;
}
}
- 响应参数加密拦截类
/**
* 请求参数 解密操作
*
* @Author: Java碎碎念
* @Date: 2019/10/24 21:31
*
*/
@Component
@ControllerAdvice(basePackages = "com.example.springbootencry.controller")
@Slf4j
public class EncryResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object obj, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
//通过 ServerHttpRequest的实现类ServletServerHttpRequest 获得HttpServletRequest
ServletServerHttpRequest sshr = (ServletServerHttpRequest) serverHttpRequest;
//此处获取到request 是为了取到在拦截器里面设置的一个对象 是我项目需要,可以忽略
HttpServletRequest request = sshr.getServletRequest();
String returnStr = "";
try {
//添加encry header,告诉前端数据已加密
serverHttpResponse.getHeaders().add("encry", "true");
String srcData = JSON.toJSONString(obj);
//加密
returnStr = DesUtil.encrypt(srcData);
log.info("接口={},原始数据={},加密后数据={}", request.getRequestURI(), srcData, returnStr);
} catch (Exception e) {
log.error("异常!", e);
}
return returnStr;
}
- 新建controller类
/**
* @Author: Java碎碎念
* @Date: 2019/10/24 21:40
*/
@RestController
public class TestController {
Logger log = LoggerFactory.getLogger(getClass());
/**
* 响应数据 加密
*/
@RequestMapping(value = "/sendResponseEncryData")
public Result sendResponseEncryData() {
Result result = Result.createResult().setSuccess(true);
result.setDataValue("name", "Java碎碎念");
result.setDataValue("encry", true);
return result;
}
/**
* 获取 解密后的 请求参数
*/
@RequestMapping(value = "/getRequestData")
public Result getRequestData(@RequestBody Object object) {
log.info("controller接收的参数object={}", object.toString());
Result result = Result.createResult().setSuccess(true);
return result;
}
}
- 其他类在源码中,后面有github地址
四、测试
- 访问响应数据加密接口
响应数据加密截图
接口=/sendResponseEncryData
原始数据={"data":{"encry":true,"name":"Java碎碎念"},"success":true}
加密后数据=vJc26g3SQRU9gAJdG7rhnAx6Ky/IhgioAgdwi6aLMMtyynAB4nEbMxvDsKEPNIa5bQaT7ZAImAL7
3VeicCuSTA==
- 访问请求数据解密接口
请求数据解密截图
更多Java学习资料可关注:gzitcast
SpringBoot中如何灵活的实现接口数据的加解密...相关推荐
- SpringBoot中如何灵活的实现接口数据的加解密功能?
数据是企业的第四张名片,企业级开发中少不了数据的加密传输,所以本文介绍下SpringBoot中接口数据加密.解密的方式. 本文目录 一.加密方案介绍二.实现原理三.实战四.测试五.踩到的坑 一.加密方 ...
- boot数据加解密 spring_SpringBoot实现接口数据的加解密功能
一.加密方案介绍 对接口的加密解密操作主要有下面两种方式: 自定义消息转换器 优势:仅需实现接口,配置简单. 劣势:仅能对同一类型的MediaType进行加解密操作,不灵活. 使用spring提供的接 ...
- SpringBoot 基于RequestBodyAdvice 和 ResponseBodyAdvice 实现数据的加/解密(采用 RSA 算法 ),“船新版本”!
一.前言: 数据是企业的第四张名片,企业级开发中少不了数据的加密传输.为了预防请求数据被劫持篡改,一般都会对传输的数据进行加密操作,如果每个接口都由我们自己去手动加密和解密,那么工作量太大而且代码冗余 ...
- Python-使用U盾完成数据的加解密(使用国密算法SKF接口)
Python-使用U盾完成数据的加解密(使用国密算法SKF接口) 1-涉及的内容 2-动态库涉及的函数,及结构体 2.1 相关结构体 2.2 相关函数 3-Python实现 4-测试结果 5-UI可视 ...
- 使用JDK中的安全包对数据进行加解密
本文以使用DES对称加密算法为例使用jdk对数据进行加密解密. 首先需要了解Provider类,它是jdk引入的密码服务提供者概念,实现了Java安全性的一部分或者全部.Provider 可能实现的服 ...
- 文件传输-对数据进行加解密的方法!
由于项目安全要求,需要使用RSA算法对部分关键数据进行加密,并使用OAEPWithSHA-256AndMGF1对数据进行填充.通过搜索最终选择较为通用OpenSSL库,但OpenSSL的RSA算法默认 ...
- Spring Boot 接口参数自动加解密
本文标题:Spring Boot 接口参数自动加解密 原始链接: https://www.shuibo.cn/102.html 许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链 ...
- java中密码修改核心代码_彻底告别加解密模块代码拷贝-JCE核心Cpiher详解
前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...
- java tar压缩工具类_分享apache的commons-compress的TarUtils压缩工具类对文件数据进行加解密、解析及格式化校验等操作...
一.前言 基于apache的commons-compress包中的org.apache.commons.compress.archivers.tar.TarUtils打包工具类对文件进行加解密.并对加 ...
最新文章
- Android中设置控件可见与不可见详解
- 普诺飞思获创新工场新一轮投资,加速神经拟态视觉传感技术商业化
- 一次性搞定权限树遍历(通用解决方案)
- hdu2433 spfa+mark[x][u][v]优化
- 周休2.5天是一种奢望?互联网人自愿加班成常态?
- iOS中的MVC设计模式
- android 判断 string 是否是字母数字,Android中判断字符串中必须包含字母或者数字...
- WORD 表格后面的空白页删不掉?
- UVALive - 4223(hdu 2926)
- 用c语言编写成绩单由高到低,C语言程序设计实验.doc
- Intel IPP密码库 IPPCP 2018——第一部分 基础开发指南
- cad转dwf简易教程
- python数据科学包第三天(股票数据分析、时间事件日志)
- c盘里appdata是什么文件夹?
- 有关注册表技术的各大网站列表
- Microsoft.NET Framework 3.5Service Pack1下载Windows功能失败原因
- Flutter系列之设置Dialog的宽度
- jenkins pipeline php,Jenkins + Pipeline 构建自动化发布
- 使用jol如何判断一个对象当前处于哪种锁
- 2017年苹果开发者账号申请——注册苹果账号
热门文章
- 花了十年攻下“5天工作制”,4天离我们还有多远
- Flowers(二分答案)
- idea项目debug模式无法启动
- Java怎么自定义类
- 基于1000家上市公司某年的财务指标数据,包括:下一年的净资产收益率(ROE),当年净资产收益率(ROEt),债务资本比率(LEV),主营业务增长率(GROWTH),市倍率(PB),公司资产的对数(A
- javascript ybmiaov
- 图数据库和知识图谱在微财风控系统中的探索和应用
- Adaptec by PMC 8系列产品在Windows环境中的性能表现(二)
- 登月者请注意:月球微生物潜在的惊人可能性
- 拓展欧几里得定理的应用