Spring Cloud Config对特殊字符加密的处理

企业动态
在这篇文章中,存在一个问题:当被加密内容包含一些诸如=、+这些特殊字符的时候,使用上篇文章中提到的类似这样的命令curl localhost:7001/encrypt -d去加密和解密的时候,会发现特殊字符丢失的情况。

之前写过一篇关于配置中心对配置内容加密解密的介绍:《Spring Cloud构建微服务架构:分布式配置中心(加密解密)》。在这篇文章中,存在一个问题:当被加密内容包含一些诸如=、+这些特殊字符的时候,使用上篇文章中提到的类似这样的命令curl localhost:7001/encrypt -d去加密和解密的时候,会发现特殊字符丢失的情况。

比如下面这样的情况:

  1. $ curl localhost:7001/encrypt -d eF34+5edo= 
  2. a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427 
  3. $ curl localhost:7001/decrypt -d a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427 
  4. 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的时候,正确的姿势应该是:

  1. $ curl localhost:7001/encrypt -H 'Content-Type:text/plain' --data-urlencode "eF34+5edo=" 
  2. 335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033 
  3.  
  4. $ curl localhost:7001/decrypt -H 'Content-Type:text/plain' --data-urlencode "335e618a02a0ff3dc1377321885f484fb2c19a499423ee7776755b875997b033" 
  5. eF34+5edo= 

那么,如果我们自己写工具来加密解密的时候怎么玩呢?下面举个OkHttp的例子,以供参考:

 

  1. private String encrypt(String value) { 
  2.     String url = "http://localhost:7001/encrypt"
  3.     Request request = new Request.Builder() 
  4.             .url(url) 
  5.             .post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes())) 
  6.             .build(); 
  7.  
  8.     Call call = okHttpClient.newCall(request); 
  9.     Response response = call.execute(); 
  10.     ResponseBody responseBody = response.body(); 
  11.     return responseBody.string(); 
  12.  
  13. private String decrypt(String value) { 
  14.     String url = "http://localhost:7001/decrypt"
  15.     Request request = new Request.Builder() 
  16.             .url(url) 
  17.             .post(RequestBody.create(MediaType.parse("text/plain"), value.getBytes())) 
  18.             .build(); 
  19.  
  20.     Call call = okHttpClient.newCall(request); 
  21.     Response response = call.execute(); 
  22.     ResponseBody responseBody = response.body(); 
  23.     return responseBody.string(); 

 

 【本文为51CTO专栏作者“翟永超”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2018-07-27 15:43:24

Spring Clou管理架构

2023-09-12 13:12:23

服务器系统

2010-09-07 10:19:31

SQL语句

2010-09-26 16:51:03

SQL Server查

2017-10-31 14:58:11

Spring Clou配置信息问题

2018-05-16 15:45:19

SpringCloudConfig

2018-07-10 14:55:32

Git存储配置

2009-05-14 10:44:54

JQuery特殊字符ID选择器

2018-08-30 15:48:43

ConsulSpring Clou开源

2024-04-18 10:26:14

模块Python

2017-05-19 15:13:05

过滤器Spring ClouZuul

2017-05-18 14:14:25

过滤器Spring ClouZuul

2017-05-02 23:05:44

HTTPZuulCookie

2017-07-31 15:47:50

Zuul统一处理

2021-11-16 11:45:00

SpringSpring ClouJava

2018-05-23 15:58:27

Spring Clou微服务架构

2015-07-13 11:28:22

Linux文件名

2017-12-01 08:54:18

SpringCloudHystrix

2021-05-24 15:25:22

加密货币比特币货币

2017-09-20 09:46:38

Spring BootSpring Clou内存
点赞
收藏

51CTO技术栈公众号