|
| 1 | +# Keytool - Generate key and self-signed certificate |
| 2 | + |
| 3 | +This document includes instructions for generating a key and self-signed certificate using [Keytool](https://docs.oracle.com/en/java/javase/13/docs/specs/man/keytool.html). |
| 4 | + |
| 5 | +> :ballot_box_with_check: NOTE: Keytool is already installed with the Java Development Kit. No additional installed is required. |
| 6 | +## Generate Key and Self-Signed Certificate |
| 7 | + |
| 8 | +1. Open a terminal/command-prompt window. |
| 9 | + |
| 10 | +2. Move into the directory of your Spring Boot library project. |
| 11 | + |
| 12 | + ``` |
| 13 | + cd spring-boot-library |
| 14 | + ``` |
| 15 | +
|
| 16 | +3. In the terminal window, run this command to generate the key and certificate. This is one long command, copy/paste in its entirety. |
| 17 | +
|
| 18 | + ``` |
| 19 | + keytool -genkeypair -alias luv2code -keystore src/main/resources/luv2code-keystore.p12 -keypass secret -storeType PKCS12 -storepass secret -keyalg RSA -keysize 2048 -validity 365 -dname "C=US, ST=Pennsylvania, L=Philadelphia, O=luv2code, OU=Training Backend, CN=localhost" -ext "SAN=dns:localhost" |
| 20 | + ``` |
| 21 | +
|
| 22 | + | Argument | Description | |
| 23 | + | --- | --- | |
| 24 | + | -genkeypair | Generates a key pair | |
| 25 | + | -alias | Alias name of the entry to process | |
| 26 | + | -keystore | Name of output keystore file | |
| 27 | + | -keypass | Key password | |
| 28 | + | -storeType | Keystore type | |
| 29 | + | -storepass | Keystore password |
| 30 | + | -keyalg | Key algorithm name | |
| 31 | + | -keysize | Key bit size | |
| 32 | + | -validity | Validity number of days | |
| 33 | + | -dname | Distinguished name | |
| 34 | + | -ext | Add the given X.509 extension | |
| 35 | +
|
| 36 | + > Detailed docs available [here](https://docs.oracle.com/en/java/javase/13/docs/specs/man/keytool.html). |
| 37 | +3. The command generates the file: `src/main/resources/luv2code-keystore.p12` . |
| 38 | +
|
| 39 | +## Verify Results |
| 40 | +
|
| 41 | +1. View the contents of your certificate. |
| 42 | +
|
| 43 | + ``` |
| 44 | + keytool -list -v -alias luv2code -keystore src/main/resources/luv2code-keystore.p12 -storepass secret |
| 45 | + ``` |
| 46 | +
|
| 47 | + _Sample Output_ |
| 48 | + ``` |
| 49 | + Alias name: luv2code |
| 50 | + Creation date: Jul 11, 2021 |
| 51 | + Entry type: PrivateKeyEntry |
| 52 | + Certificate chain length: 1 |
| 53 | + Certificate[1]: |
| 54 | + Owner: C=US, ST=Pennsylvania, L=Philadelphia, O=luv2code, OU=Training Backend, CN=localhost |
| 55 | + Issuer: C=US, ST=Pennsylvania, L=Philadelphia, O=luv2code, OU=Training Backend, CN=localhost |
| 56 | + Serial number: 9f1898a717a75375 |
| 57 | + Valid from: Sun Jul 11 00:02:10 EDT 2021 until: Mon Jul 11 00:02:10 EDT 2022 |
| 58 | + Certificate fingerprints: |
| 59 | + SHA1: B6:14:8C:5F:0C:86:D6:32:3C:FC:D6:7E:2C:AD:AF:29:60:32:4F:38 |
| 60 | + SHA256: E1:B7:C7:ED:CE:1F:EE:C7:36:20:07:E4:51:5D:5D:3A:78:27:65:E5:E4:9C:EB:20:90:85:D8:1A:A4:EF:69:41 |
| 61 | + Signature algorithm name: SHA256withRSA |
| 62 | + Subject Public Key Algorithm: 2048-bit RSA key |
| 63 | + Version: 3 |
| 64 | + Extensions: |
| 65 | + #1: ObjectId: 2.5.29.17 Criticality=false |
| 66 | + SubjectAlternativeName [ |
| 67 | + DNSName: localhost |
| 68 | + ] |
| 69 | + #2: ObjectId: 2.5.29.14 Criticality=false |
| 70 | + SubjectKeyIdentifier [ |
| 71 | + KeyIdentifier [ |
| 72 | + 0000: 0B 7D AA 0B 04 0F A5 20 51 5B FB C2 A3 DC 9B 78 ....... Q[.....x |
| 73 | + 0010: 19 9F 85 48 ...H |
| 74 | + ] |
| 75 | + ] |
| 76 | + ``` |
| 77 | +
|
| 78 | +## Spring Boot HTTPS configs |
| 79 | +
|
| 80 | +1. Edit your `application.properties` file |
| 81 | +
|
| 82 | +1. Add this snippet for Spring Boot SSL configs to the end of your `application.properties` file |
| 83 | +
|
| 84 | + ``` |
| 85 | + ##### |
| 86 | + # |
| 87 | + # HTTPS configuration |
| 88 | + # |
| 89 | + ##### |
| 90 | + # Server web port |
| 91 | + server.port=8443 |
| 92 | + # Enable HTTPS support (only accept HTTPS requests) |
| 93 | + server.ssl.enabled=true |
| 94 | + # Alias that identifies the key in the key store |
| 95 | + server.ssl.key-alias=luv2code |
| 96 | + # Keystore location |
| 97 | + server.ssl.key-store=classpath:luv2code-keystore.p12 |
| 98 | + # Keystore password |
| 99 | + server.ssl.key-store-password=secret |
| 100 | + # Keystore format |
| 101 | + server.ssl.key-store-type=PKCS12 |
| 102 | + ``` |
| 103 | +
|
| 104 | +Congrats! You have successfully configured your Spring Boot project to use a self-signed certificate for https. You can now return to the videos and continue with the course. |
0 commit comments