Spring Cloud Config对特殊字符加密的处理
2018-05-06 21:21
381 查看
之前写过一篇关于配置中心对配置内容加密解密的介绍:《Spring Cloud构建微服务架构:分布式配置中心(加密解密)》。在这篇文章中,存在一个问题:当被加密内容包含一些诸如
比如下面这样的情况:
可以看到,经过加密解密之后,又一些特殊字符丢失了。由于之前在这里也小坑了一下,所以抽空写出来分享一下,给遇到同样问题的朋友,希望对您有帮助。
If you are testing like this with curl, then use
所以,在使用
那么,如果我们自己写工具来加密解密的时候怎么玩呢?下面举个
以下专题教程也许您会有兴趣
Spring Boot基础教程
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/plainto 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(); }
以下专题教程也许您会有兴趣
Spring Boot基础教程
Spring Cloud基础教程
相关文章推荐
- Spring Cloud Config对特殊字符加密处理的方法详解
- SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)
- web.config中配置字符串中特殊字符的处理
- Spring cloud config 配置文件加密方式
- App.config/Web.config 中特殊字符的处理
- 使用spring+maven时配置文件中的特殊字符处理
- 在spring-config文件中对jdbc数据配置进行加密处理
- web.config文件中的特殊字符处理
- web.config文件中的特殊字符处理
- Spring Cloud Config服务端配置细节(二)之加密解密
- spring-cloud-config 非对称加密 keystore 文件加载异常
- Spring Cloud Config 加密和解密
- Spring 处理中文及特殊字符
- Spring Cloud Config 加密和解密
- 【转载】App.config/Web.config 中特殊字符的处理
- web.config中配置字符串中特殊字符的处理
- SpringCloud Config 加密和解密 (对称)
- Spring Cloud Config 加密和解密
- 特殊字符,# + %在url中传递,encode方式加密处理
- Spring Cloud Config 加密和解密