如何用命令行生成RSA的公钥和密钥文件用于Spring Security JWT的项目里

笔者最近在鼓捣对Spring Boot项目里应用Spring Security的JWT认证,用了它的官方的demo能跑通

https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/jwt/loginicon-default.png?t=M5H6https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/jwt/login

 它里面用到了两个文件: app.key, app.pub 是demo里提供的RSA的密钥对, 笔者想换成自己的,研究了怎么用命令行生成, 找了几个网页都不靠谱,后来找到一个能跑通,分享下,前提条件,在window下用git bash操作。

先建个目录,例如jwt, git bash命令行进入这个目录

ssh-keygen -t rsa -m PEM

按提示输入保存的文件名,例如 rsa, 其他直接回车

 就会发现当前文件夹生成了两个文件

导出符合X509规范的公钥内容

ssh-keygen -m PKCS8 -e

 把这公钥内容替换到Spring Security JWT里的app.pub文件里内容即可

导出私钥文件

openssl pkcs8 -topk8 -inform pem -in rsa -outform pem -nocrypt -out app.key

生成了app.key, 打开内容类似如下

 把这个文件替换Spring Security JWT里的app.key文件即可。

参考:

Spring JWT with RSA (asymmetric encryption algorithm)Let's say we have an "auth server" that signs tokens for us and a "resource server" where we store some pretty sensitive data. We trust the auth server and we want to validate that the JWT we get indeed comes from that trusted auth server. In other words, how can we be sure that the guy who wants to detonate our printers is using a JWT that is from that very auth server?icon-default.png?t=M5H6https://leaks.wanari.com/2020/09/25/spring-jwt-with-rsa

   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值