最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Spring Cloud Config对特殊字符加密处理的方法详解
时间:2022-11-14 23:26:55 编辑:袖梨 来源:一聚教程网
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://l*ocal*hos*t: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://l*o*calh*ost: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(); }
相关文章
- 米姆米姆哈黎光雀怎么打 黎光雀打法教学 10-16
- 境界刀鸣护廷十三队哪个角色比较强 护廷十三队角色推荐一览 10-16
- 境界刀鸣灵骸副本怎么过关 灵骸副本过关攻略 10-16
- 伊瑟水生绫智壳源器怎么选 水生绫智壳源器选择推荐 10-16
- 空洞骑士丝之歌圣咏外衣怎么获得 圣咏外衣获取攻略 10-16
- 崩坏星穹铁道丹恒腾荒光锥怎么带-丹恒腾荒光锥搭配推荐 10-16