Skip to content

Commit 1a72914

Browse files
committed
First PEM version
1 parent 70539ea commit 1a72914

File tree

7 files changed

+72
-62
lines changed

7 files changed

+72
-62
lines changed

README.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
[![Java](https://img.shields.io/badge/Java-21-red?logo=openjdk)](https://openjdk.org/)
44
[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.x-brightgreen?logo=springboot)](https://spring.io/projects/spring-boot)
5-
[![AWS Parameter Store](https://img.shields.io/badge/AWS-Parameter%20Store-orange?logo=amazonaws)](https://aws.amazon.com/systems-manager/)
65
[![License](https://img.shields.io/github/license/rickypat03/SpringSecurityTemplate)](LICENSE)
76

87
A **Spring Boot starter template** with **Spring Security** pre-configured.
98
This project gives you a clean foundation to quickly build **secure web applications**.
109

1110
👉 In this first version you’ll need:
12-
1. A **database** (PostgreSQL, MySQL, etc.)
13-
2. **AWS Parameter Store** (📌 coming soon: version without it)
11+
- A **database** (PostgreSQL, MySQL, etc.);
12+
- A **RSA key pair** to put into src/main/resources/keys as .pem files;
1413

1514
---
1615

@@ -23,43 +22,51 @@ git clone https://github.com/rickypat03/SpringSecurityTemplate.git
2322

2423
---
2524

26-
### 2. Open the project
25+
### 2. Checkout the branch that you want to use
26+
27+
```bash
28+
git checkout template/pem-local
29+
```
30+
31+
---
32+
33+
### 3. Open the project
2734
```bash
2835
cd SpringSecurityTemplate
2936
```
3037

31-
- Import it into your favorite IDE (IntelliJ IDEA, Eclipse, VS Code).
38+
- Import it into your favorite IDE (IntelliJ IDEA, Eclipse, VS Code);
3239

33-
- Rename the project as you like.
40+
- Rename the project as you like;
3441

3542
---
3643

37-
### 3. Configure
44+
### 4. Configure
3845

39-
- 🔍 Look for TODO comments in the code – they guide you on how to adapt the template.
46+
1. 🔍 Look for TODO comments in the code – they guide you on how to adapt the template.
4047

41-
- Edit application.properties to match your setup.
48+
2. Edit application.properties to match your setup.
4249

43-
- Check and adjust pom.xml if needed.
50+
3. Check and adjust pom.xml if needed.
4451

4552
---
4653

47-
### 4. Run & Build
54+
### 5. Run & Build
4855

4956
You now have a working Spring Boot + Security application!
5057
Start adding your own:
5158

52-
- Controllers
59+
- Controllers;
5360

54-
- Services
61+
- Services;
5562

56-
- Repositories
63+
- Repositories;
5764

5865
---
5966

6067
### 🤝 Contributing
6168

62-
- Fork the repo, create a branch, and open a pull request.
69+
- Fork the repo, create a branch, and open a pull request;
6370

6471
- Found a bug? Have an idea? → Open an issue!
6572

@@ -69,10 +76,10 @@ Start adding your own:
6976

7077
### 📌 Next steps
7178

72-
1. Add a version without AWS Parameter Store
79+
- Add example controllers and endpoints;
7380

74-
2. Add example controllers and endpoints
81+
- Provide Docker support;
7582

76-
3. Provide Docker support
83+
- Add the Authorization Server version;
7784

7885
---

logs/app.log

Whitespace-only changes.

src/main/java/com/template/security/key/JwtKeyProperties.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/template/security/key/PemUtils.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.template.security.key;
22

3-
import lombok.RequiredArgsConstructor;
3+
import org.springframework.core.io.ClassPathResource;
44
import org.springframework.stereotype.Component;
55

6+
import java.io.InputStream;
7+
import java.nio.charset.StandardCharsets;
68
import java.security.KeyFactory;
79
import java.security.PrivateKey;
810
import java.security.PublicKey;
@@ -11,41 +13,58 @@
1113
import java.util.Base64;
1214

1315
@Component
14-
@RequiredArgsConstructor
1516
public class PemUtils {
1617

17-
private final JwtKeyProperties jwtKeyProperties;
18+
/**
19+
* Read the key from the param path and cleans it.
20+
* @param path The path of the key inside resource directory
21+
* @return The key cleaned
22+
* @throws Exception If there is some problem with the cleaning
23+
*/
24+
private String readKey(String path) throws Exception {
25+
26+
ClassPathResource resource = new ClassPathResource(path);
27+
28+
try (InputStream is = resource.getInputStream()) {
29+
30+
String key = new String(is.readAllBytes(), StandardCharsets.UTF_8);
31+
32+
return key.replace("-----BEGIN PUBLIC KEY -----", "")
33+
.replace("-----END PUBLIC KEY-----", "")
34+
.replace("-----BEGIN PRIVATE KEY -----", "")
35+
.replace("-----END PRIVATE KEY -----", "")
36+
.replaceAll("\\s+", "");
37+
}
38+
}
1839

1940
/**
20-
* Loads an RSA PrivateKey from a PEM formatted string.
41+
* Loads an RSA PrivateKey from the private.pem file inside resources/keys/
2142
*
2243
* @return the PrivateKey
2344
* @throws Exception if there is an error during key loading
2445
*/
2546
public PrivateKey loadPrivateKey() throws Exception {
26-
String cleaned = jwtKeyProperties.getPrivateKey()
27-
.replace("-----BEGIN PRIVATE KEY-----", "")
28-
.replace("-----END PRIVATE KEY-----", "")
29-
.replaceAll("\\s+", "");
3047

31-
byte[] decoded = Base64.getDecoder().decode(cleaned);
48+
String keyPath = "keys/private.pem";
49+
String keyPEM = readKey(keyPath);
50+
51+
byte[] decoded = Base64.getDecoder().decode(keyPEM);
3252
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
3353
return KeyFactory.getInstance("RSA").generatePrivate(spec);
3454
}
3555

3656
/**
37-
* Loads an RSA PublicKey from a PEM formatted string.
57+
* Loads an RSA PublicKey from the public.pem file inside resources/keys/
3858
*
3959
* @return the PublicKey
4060
* @throws Exception if there is an error during key loading
4161
*/
4262
public PublicKey loadPublicKey() throws Exception {
43-
String cleaned = jwtKeyProperties.getPublicKey()
44-
.replace("-----BEGIN PUBLIC KEY-----", "")
45-
.replace("-----END PUBLIC KEY-----", "")
46-
.replaceAll("\\s+", "");
4763

48-
byte[] decoded = Base64.getDecoder().decode(cleaned);
64+
String keyPath = "keys/public.pem";
65+
String keyPEM = readKey(keyPath);
66+
67+
byte[] decoded = Base64.getDecoder().decode(keyPEM);
4968
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
5069
return KeyFactory.getInstance("RSA").generatePublic(spec);
5170
}

src/main/java/com/template/service/JwtService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.security.PrivateKey;
1010
import java.security.PublicKey;
1111
import java.util.Date;
12+
import java.util.Map;
1213

1314
@Slf4j
1415
@Service
@@ -18,23 +19,23 @@ public class JwtService {
1819
private final PrivateKey privateKey;
1920
private final PublicKey publicKey;
2021

22+
//TODO: When you'll call this function remember to create a Map<String, Object> variable where you'll insert your claims
23+
// ex: Map<String, Object> claims = new HashMap();
24+
// claims.put("username", "bobby");
2125
/**
2226
* Generates a JWT token for the given user details.
2327
*
2428
* @param userId the ID of the user
25-
* @param username the username of the user
26-
* @param role the role of the user
29+
* @param claims the claims of the token
2730
* @return a JWT token as a String
2831
*/
2932
public String generateToken(Long userId,
30-
String username,
31-
String role) {
33+
Map<String, Object> claims) {
3234

3335
String jwt = Jwts.builder()
3436
.setSubject(String.valueOf(userId))
35-
.claim("role", role) // ROLE_USER, ROLE_MANAGER, ...
36-
.claim("username", username)
3737
.setIssuedAt(new Date(System.currentTimeMillis()))
38+
.addClaims(claims)
3839
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60))
3940
.signWith(privateKey, SignatureAlgorithm.RS256)
4041
.compact();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
your key data here
3+
-----END PRIVATE KEY-----

src/main/resources/keys/public.pem

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
your key data here
3+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)