JWT available after restart #9

Merged
harb merged 1 commits from JWTFix into main 2026-01-21 20:02:40 +01:00
4 changed files with 82 additions and 15 deletions
+1
View File
@@ -41,3 +41,4 @@ build/
### Secrets ###
src/main/resources/firebase-private-key.json
src/main/resources/firebase-private-key_disable.json
/secrets/
+6
View File
@@ -1,6 +1,7 @@
volumes:
nominatim-data:
postgres_data:
jwt_keys:
services:
backend:
@@ -11,6 +12,11 @@ services:
- "8080:8080"
env_file:
- .env
environment:
RSA_PRIVATE_KEY_PATH: /data/keys/jwt-private.pem
RSA_PUBLIC_KEY_PATH: /data/keys/jwt-public.pem
volumes:
- jwt_keys:/data/keys
depends_on:
- db
restart: unless-stopped
@@ -1,36 +1,96 @@
package SuMoC.backend.security.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
@Component
public class RSAKeyProperties {
private RSAPublicKey publicKey;
private RSAPrivateKey privateKey;
private final RSAPublicKey publicKey;
private final RSAPrivateKey privateKey;
public RSAKeyProperties(){
KeyPair pair = KeyGeneratorUtility.generateRsaKey();
this.publicKey = (RSAPublicKey) pair.getPublic();
this.privateKey = (RSAPrivateKey) pair.getPrivate();
public RSAKeyProperties(
@Value("${RSA_PUBLIC_KEY_PATH:/data/keys/jwt-public.pem}") String publicKeyPath,
@Value("${RSA_PRIVATE_KEY_PATH:/data/keys/jwt-private.pem}") String privateKeyPath
) {
try {
Path pubPath = Paths.get(publicKeyPath);
Path privPath = Paths.get(privateKeyPath);
if (pubPath.getParent() != null) Files.createDirectories(pubPath.getParent());
if (privPath.getParent() != null) Files.createDirectories(privPath.getParent());
if (Files.exists(pubPath) && Files.exists(privPath)) {
this.publicKey = readPublicKey(pubPath);
this.privateKey = readPrivateKey(privPath);
return;
}
KeyPair pair = KeyGeneratorUtility.generateRsaKey();
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
RSAPrivateKey priv = (RSAPrivateKey) pair.getPrivate();
writePublicKey(pubPath, pub);
writePrivateKey(privPath, priv);
this.publicKey = pub;
this.privateKey = priv;
} catch (Exception e) {
throw new IllegalStateException("Failed to load or generate RSA keys", e);
}
}
public RSAPublicKey getPublicKey() {
return publicKey;
public RSAPublicKey getPublicKey() { return publicKey; }
public RSAPrivateKey getPrivateKey() { return privateKey; }
// ---------- Read ----------
private static RSAPublicKey readPublicKey(Path path) throws Exception {
String pem = Files.readString(path, StandardCharsets.UTF_8)
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s", "");
byte[] der = Base64.getDecoder().decode(pem);
return (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(der));
}
public void setPublicKey(RSAPublicKey publicKey) {
this.publicKey = publicKey;
private static RSAPrivateKey readPrivateKey(Path path) throws Exception {
String pem = Files.readString(path, StandardCharsets.UTF_8)
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s", "");
byte[] der = Base64.getDecoder().decode(pem);
return (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(der));
}
public RSAPrivateKey getPrivateKey() {
return privateKey;
// ---------- Write ----------
private static void writePublicKey(Path path, RSAPublicKey key) throws Exception {
writePem(path, "PUBLIC KEY", key.getEncoded());
}
public void setPrivateKey(RSAPrivateKey privateKey) {
this.privateKey = privateKey;
private static void writePrivateKey(Path path, RSAPrivateKey key) throws Exception {
writePem(path, "PRIVATE KEY", key.getEncoded());
}
private static void writePem(Path path, String type, byte[] derBytes) throws Exception {
String base64 = Base64.getMimeEncoder(64, new byte[]{'\n'}).encodeToString(derBytes);
String pem = "-----BEGIN " + type + "-----\n" +
base64 + "\n" +
"-----END " + type + "-----\n";
Path tmp = Paths.get(path.toString() + ".tmp");
Files.writeString(tmp, pem, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
Files.move(tmp, path, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/sumoc
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
logging.level.org.springframework.security=Trace