栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何用命令行生成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/loginhttps://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?https://leaks.wanari.com/2020/09/25/spring-jwt-with-rsa

   

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号