自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Cloud Config對特殊字符加密的處理

企業(yè)動態(tài)
在這篇文章中,存在一個問題:當(dāng)被加密內(nèi)容包含一些諸如=、+這些特殊字符的時候,使用上篇文章中提到的類似這樣的命令curl localhost:7001/encrypt -d去加密和解密的時候,會發(fā)現(xiàn)特殊字符丟失的情況。

之前寫過一篇關(guān)于配置中心對配置內(nèi)容加密解密的介紹:《Spring Cloud構(gòu)建微服務(wù)架構(gòu):分布式配置中心(加密解密)》。在這篇文章中,存在一個問題:當(dāng)被加密內(nèi)容包含一些諸如=、+這些特殊字符的時候,使用上篇文章中提到的類似這樣的命令curl localhost:7001/encrypt -d去加密和解密的時候,會發(fā)現(xiàn)特殊字符丟失的情況。

比如下面這樣的情況:

  1. $ curl localhost:7001/encrypt -d eF34+5edo= 
  2. a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427 
  3. $ curl localhost:7001/decrypt -d a34c76c4ddab706fbcae0848639a8e0ed9d612b0035030542c98997e084a7427 
  4. eF34 5edo 

可以看到,經(jīng)過加密解密之后,又一些特殊字符丟失了。由于之前在這里也小坑了一下,所以抽空寫出來分享一下,給遇到同樣問題的朋友,希望對您有幫助。

問題原因與處理方法

其實關(guān)于這個問題的原因在官方文檔中是有具體說明的,只能怪自己太過粗心了,具體如下:

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的時候,正確的姿勢應(yīng)該是:

  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專欄作者“翟永超”的原創(chuàng)稿件,轉(zhuǎn)載請通過51CTO聯(lián)系作者獲取授權(quán)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來源: 51CTO專欄
相關(guān)推薦

2018-07-27 15:43:24

Spring Clou管理架構(gòu)

2023-09-12 13:12:23

服務(wù)器系統(tǒng)

2010-09-07 10:19:31

SQL語句

2010-09-26 16:51:03

SQL Server查

2018-05-16 15:45:19

SpringCloudConfig

2017-10-31 14:58:11

Spring Clou配置信息問題

2018-07-10 14:55:32

Git存儲配置

2009-05-14 10:44:54

JQuery特殊字符ID選擇器

2018-08-30 15:48:43

ConsulSpring Clou開源

2017-07-31 15:47:50

Zuul統(tǒng)一處理

2017-05-02 23:05:44

HTTPZuulCookie

2017-05-19 15:13:05

過濾器Spring ClouZuul

2017-05-18 14:14:25

過濾器Spring ClouZuul

2024-04-18 10:26:14

模塊Python

2021-11-16 11:45:00

SpringSpring ClouJava

2017-12-01 08:54:18

SpringCloudHystrix

2015-07-13 11:28:22

Linux文件名

2018-05-23 15:58:27

Spring Clou微服務(wù)架構(gòu)

2025-03-04 08:59:16

2021-05-24 15:25:22

加密貨幣比特幣貨幣
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號