JWT - generate public and private key (YML example)

@SpringBootApplication

public class AuthServerApplication implements CommandLineRunner {


public static void main(String[] args) {

SpringApplication.run(AuthServerApplication.class, args);

}


@Override

public void run(String... args) throws Exception {

KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");

keyGenerator.initialize(1024);

KeyPair kp = keyGenerator.genKeyPair();

PublicKey publicKey = kp.getPublic();

PrivateKey privateKey = kp.getPrivate();


String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded());

String encodedPrivateKey = Base64.getEncoder().encodeToString(privateKey.getEncoded());


System.out.println(convertToPublicKey(encodedPublicKey));


System.out.println();


System.out.println(convertToPrivateKey(encodedPrivateKey));


}


private static String convertToPrivateKey(String key) {

        StringBuilder result = new StringBuilder();

        result.append("-----BEGIN PRIVATE KEY-----\n");

        result.append(key);

        result.append("\n-----END PRIVATE KEY-----");

        return result.toString();

    }


    private static String convertToPublicKey(String key) {

        StringBuilder result = new StringBuilder();

        result.append("-----BEGIN PUBLIC KEY-----\n");

        result.append(key);

        result.append("\n-----END PUBLIC KEY-----");

        return result.toString();

    }



***************************************************

YML


jwt:

  token:

    private-key: {generated-private-key}

    public-key: {generated-public-key}

    access-token-validity-time: 3600000

    refresf-token-validity-time: 86400000

Comments

Popular posts from this blog

Validation for AZ phone numbers with RegEx in Java

Java necə işləyir, kod nədir ?