===================================================================
一、什么是非对称加密(Asymmetric encryption)
非对称加密我们需要生成对应的公钥和私钥,jdk中提供的有java-keytool工具帮助我们生成,执行如下命令:
keytool -genkeypair -alias “config-info” -keyalg “RSA” -keystore c:\tools\encryp-info.keystore
======================================================================
创建一个SpringCloud项目
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
com.bobo
config-server-encryption-SRA
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-maven-plugin
spring.application.name=config-server-encryption-SRA
server.port=9060
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/
#Git 配置
spring.cloud.config.server.git.uri=https://gitee.com/dengpbs/config
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
#keytool -genkeypair -alias “config-info” -keyalg “RSA” -keystore c:\tools\encryp-info.keystore
keystore 文件的路径
encrypt.key-store.location=classpath:encryp-info.keystore
alias 指定密钥对的别名,该别名是公开的;
encrypt.key-store.alias=config-info
storepass 密钥仓库
encrypt.key-store.password=123456
keypass 用来保护所生成密钥对中的私钥
encrypt.key-store.secret=123456
将生成的keystore文件拷贝到classpath目录下
测试加密状态:http://localhost:9060/encrypt/status
加密
public class Test1 {
/**
-
通过RestTemplate来加密数据
-
@param args
*/
public static void main(String[] args) {
String url = “http://127.0.0.1:9060/encrypt”;
RestTemplate template = new RestTemplate();
ResponseEntity msg = template.postForEntity(url, “123456”, String.class);
System.out.println(msg.getBody());
}
}
=======================================================================
拷贝上个案例的客户端程序。
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
com.bobo
config-e-book-product-provider-sra
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.4
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
mysql
mysql-connector-java
5.1.47
com.bobo
e-book-product-service
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-config