Configuring Spring Oauth2 with JWT & asymmetric RSA keypair

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.

  1. 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.

  2. 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.

  3. 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 oauth2JwtAccessTokenConverter.

Step 2: Configure Spring Oauth2

  1. 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;
}
 
}
  1. Resource Server:
    In your application.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值