创建springboot的分布式的项目
导入依赖
这里先导入3个依赖,这个是JWT的依赖,这个从JWT的官网上面找到的
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.7</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.7</version>
<scope>runtime</scope>
</dependency>
写以上的3个工具包,这些工具包在分布式项目里面的不同的子项目里面都可以调用。我们可以利用这些工具生成公钥等,生成token等。在项目中调用这些工具类就可以了。
在测试类里面实现生成公钥和私钥
测试类
在D盘生成一个文件夹,里面放生成的私钥和公钥文件
测试类里面的代码
package com.itheima.utils;
import org.junit.Test;
public class RsaUtilsTest {
//私钥路径
private String privateFilePath = "D:\\auth_key\\id_key_rsa";
// 公钥路径
private String publicFilePath = "D:\\auth_key\\id_key_rsa.pub";
// 生成公钥和私钥
@Test
public void generateKey() throws Exception {
RsaUtils.generateKey(publicFilePath, privateFilePath, "jing", 2048);
}
// 获取公钥
@Test
public void getPublicKey() throws Exception {
System.out.println(RsaUtils.getPublicKey(publicFilePath));
}
// 获取私钥
@Test
public void getPrivateKey() throws Exception {
System.out.println(RsaUtils.getPrivateKey(privateFilePath));
}
}
依次运行完成之后
以上就是根据工具类生成的公钥和私钥,以后我们在项目里面直接使用工具类就可以生成了