Spring Cloud Config对特殊字符加密处理的方法详解
作者:程序猿DD 时间:2021-11-22 15:16:48
前言
之前写过一篇关于配置中心对配置内容加密解密的介绍:《Spring Cloud构建微服务架构:分布式配置中心(加密解密) 》。在这篇文章中,存在一个问题:当被加密内容包含一些诸如=、+这些特殊字符的时候,使用上篇文章中提到的类似这样的命令curl localhost:7001/encrypt -d
去加密和解密的时候,会发现特殊字符丢失的情况。
比如下面这样的情况:
$ curl localhost:7001/encrypt -d eF34+5edo=
a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427
$ curl localhost:7001/decrypt -d a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427
eF34 5edo
可以看到,经过加密解密之后,又一些特殊字符丢失了。由于之前在这里也小坑了一下,所以抽空写出来分享一下,给遇到同样问题的朋友,希望对您有帮助。
问题原因与处理方法
其实关于这个问题的原因在官方文档中是有具体说明的,只能怪自己太过粗心了,具体如下:
If you are testing like this with curl, then use --data-urlencode (instead of -d) or set an explicit Content-Type: text/plain to make sure curl encodes the data correctly when there are special characters (‘+' is particularly tricky).
所以,在使用curl的时候,正确的姿势应该是:
$ curl localhost:7001/encrypt -H 'Content-Type:text/plain' --data-urlencode "eF34+5edo="
335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033
$ curl localhost:7001/decrypt -H 'Content-Type:text/plain' --data-urlencode "335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033"
eF34+5edo=
那么,如果我们自己写工具来加密解密的时候怎么玩呢?下面举个OkHttp的例子,以供参考:
private String encrypt(String value) {
String url = "http://localhost:7001/encrypt";
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes()))
.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
}
private String decrypt(String value) {
String url = "http://localhost:7001/decrypt";
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes()))
.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
ResponseBody responseBody = response.body();
return responseBody.string();
}
来源:http://blog.didispace.com/spring-cloud-config-sp-char-encrypt/
标签:springcloud,config,特殊字符
0
投稿
猜你喜欢
Java 处理超大数类型之BigInteger案例详解
2021-06-20 15:36:38
Maven+Tomcat8 实现自动化部署的方法
2023-01-03 06:44:20
C#入门教程之ListBox控件使用方法
2023-09-20 04:52:59
Android WorkManager浅谈
2023-03-24 11:26:46
java后台验证码生成的实现方法
2021-10-01 05:59:26
Android之高德地图定位SDK集成及地图功能实现
2023-12-06 07:33:16
使用Filter过滤器中访问getSession()要转化
2022-10-01 16:20:04
怎么把本地jar包放入本地maven仓库和远程私服仓库
2023-12-05 20:13:00
Spring框架学习常用注解汇总
2023-11-10 17:38:53
UnityWebRequest前后端交互实现过程解析
2021-09-07 01:24:42
Android+SQLite数据库实现的生词记事本功能实例
2023-06-18 10:41:35
Spring MVC通过添加自定义注解格式化数据的方法
2023-11-06 09:05:32
Android手动检查并申请权限方法
2023-08-04 23:14:17
SpringBoot深入分析运行原理与功能实现
2022-01-03 14:48:43
photoView实现图片多点触控效果
2023-06-21 20:16:03
详解Java中switch的新特性
2023-11-24 23:41:54
Java中synchronized关键字修饰方法同步的用法详解
2022-03-05 07:11:22
c# 如何将字符串转换为大写或小写
2023-01-04 20:44:28
不可不知道的10个java谎言
2022-01-21 10:25:40
Java实现SSL双向认证的方法
2023-09-22 10:34:35