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

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

之前写过一篇关于配置中心对配置内容加密解密的介绍:《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();
}

以下专题教程也许您会有兴趣

目录
相关文章
|
2天前
|
消息中间件 Java 数据安全/隐私保护
Spring Cloud 项目中实现推送消息到 RabbitMQ 消息中间件
Spring Cloud 项目中实现推送消息到 RabbitMQ 消息中间件
|
2天前
|
负载均衡 监控 Java
我把Spring Cloud的超详细资料介绍给你,面试官不会生气吧?geigei
我把Spring Cloud的超详细资料介绍给你,面试官不会生气吧?geigei
|
2天前
|
负载均衡 Java 应用服务中间件
Spring Cloud 负载平衡的意义什么?
负载平衡是指将网络流量在多个服务器之间分布,以达到提高系统性能、增强可靠性和提供更好用户体验的目的。在负载平衡的架构中,多个服务器被组织成一个集群,共同处理用户的请求。
26 4
|
3天前
|
监控 安全 Java
Spring cloud原理详解
Spring cloud原理详解
18 0
|
4天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
16 1
|
3天前
|
安全 Java Docker
|
4天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
38 6
|
4天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
4天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
4天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
30 1