Configuring Spring Oauth2 with JWT & asymmetric RSA keypair
来源:互联网 发布:js修改input边框颜色 编辑:程序博客网 时间:2024/06/07 04:09
Step 1: Generate RSA key pair.
Here are the steps I took to create my RSA key pairs with Java keytoolcommand. Although I did this to configure my spring oauth2 jwt application, of course, it is not only restricted to that.
- Lets create our java keystore(.jks) file:
$ keytool -genkeypair -alias mytestkey -keyalg RSA \
-dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \
-keypass changeme -keystore server.jks -storepass letmein
We generated a keypair named mytestkey with an RSA algorithm. Option -keypass changeme is to access the specific keypair, which is mytestkey in our case & -storepass letmein is to access the whole keystore file. Export public key certificate file.
$ keytool -export -keystore server.jks -alias mytestkey -file example.cer
With this certificate file we can find get our public key in the next section.- Using openssl to print the public key.
openssl x509 -inform der -in example.cer -pubkey -noout
This command will show the public key:-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3Kd1vQNTLHLVhyMR0JHj
Q3CxJ9Roi6aZTzUk/HDerxJ+1ey8CdX4zf8bFA9Fh21KTojw87yt76A6GpCuru6P
zxCou0GLPwFwKCS1SFcsysOMSxRAhgIssjujGnbC2Q0XPDpsGYJVavnHGZ7cI7Hn
sXqHcL0dmbgEfI7NR7wCGHoo1NxjfwOQXtCGH3w/Tg2BLA3HNyRclrCfJuS3aj0y
tr7tOWdzgguztH6E4xoqKdn7FEMMtBEsggw7Z4H8uziUy37Z7iOMTdmwZvbpMrns
IUZElqnYcRFYLPRH5xsSl1Y129fAbW03WW63agzy9DWO5HhT44ePJDrkZqsEaHKw
/QIDAQAB
-----END PUBLIC KEY-----
Another way to achieve this using java code, which is bit more complex, is:
1
2
3
4
KeyPair keyPair =
new
KeyStoreKeyFactory(
new
ClassPathResource(
"server.jks"
),
"letmein"
.toCharArray())
.getKeyPair(
"mytestkey"
,
"changeme"
.toCharArray());
System.out.println(
new
String(Base64.encode(keyPair.getPublic().getEncoded())));
This code was inspired by spring oauth2
s JwtAccessTokenConverter
.
Step 2: Configure Spring Oauth2
- Authorization server:.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@EnableAuthorizationServer
protected
static
class
OAuth2Config
extends
AuthorizationServerConfigurerAdapter {
//other configurations are omitted.
@Bean
public
JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter =
new
JwtAccessTokenConverter();
KeyPair keyPair =
new
KeyStoreKeyFactory(
new
ClassPathResource(
"server.jks"
),
"letmein"
.toCharArray())
.getKeyPair(
"mytestkey"
,
"changeme"
.toCharArray());
converter.setKeyPair(keyPair);
return
converter;
}
}
- Resource Server:
In yourapplication.yml
file(note that spacing is messed up below, you should have a proper spacing):
spring:
oauth2:
resource:
jwt:
keyValue: |
—–BEGIN PUBLIC KEY—–
MIIBIjANBgkqhkiG9…
—–END PUBLIC KEY—–
阅读全文
0 0
- Configuring Spring Oauth2 with JWT & asymmetric RSA keypair
- spring boot security oauth2 jwt 服务端实现
- Spring Security OAuth2实现使用JWT
- Spring Security+OAuth2 + JWT认证以及携带用户信息
- java编程spring boot security oauth2 jwt完美整合例子
- spring boot security oauth2 jwt完美整合例子-java编程
- api安全-----oauth2&jwt
- 使用JWT的OAuth2的SSO分析
- SSO with OAuth2: Angular JS and Spring Security Part V
- spring 集成 jwt
- OAuth2 & Spring Security OAuth2 总结
- 第八章 SpringCloud Oauth2认证中心-基于JWT认证
- Spring Session 整合 JWT Token
- Spring-security-oauth2讲解
- spring security oauth2配置
- spring-security-oauth2整合
- spring oauth2相关资料
- Spring Security#OAuth2
- Java问题记录
- 配置eclipse使能打开当前文件所在目录
- 安装CUDA 出现libEGL.so.1 is not a symbolic link libcudnn.so.5 is not a symbolic link
- 网络中隐含层神经元节点的个数(需要学习的特征数目)
- 正则表达式15-25位数字包括一个减号,减号不能开头
- Configuring Spring Oauth2 with JWT & asymmetric RSA keypair
- Leetcode(4):Median of Sorted Arrays
- 天天记账
- android开发收集的第三方框架和库
- html限制文本框只能输入int类型的数字
- 不要告诉我你懂margin
- C++中指针与const
- mysql -- 查询表所有字段的属性和注释
- 分布式计算学习笔记