changed gson to jakarta.json

This commit is contained in:
Sukalpo Mitra 2023-12-02 18:32:37 +08:00
parent 386a30045a
commit f80b23369d
4 changed files with 19 additions and 63 deletions

View File

@ -69,11 +69,6 @@
<artifactId>jul-to-slf4j</artifactId> <artifactId>jul-to-slf4j</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency> <dependency>
<groupId>com.auth0</groupId> <groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId> <artifactId>java-jwt</artifactId>

View File

@ -1,27 +0,0 @@
package com.sismics.model;
import java.util.List;
public class KeycloakCertKey {
public String kid;
public List<String> x5c;
public KeycloakCertKey() {
}
public List<String> getX5c() {
return x5c;
}
public void setX5c(List<String> x5c) {
this.x5c = x5c;
}
public String getKid() {
return kid;
}
public void setKid(String kid) {
this.kid = kid;
}
}

View File

@ -1,18 +0,0 @@
package com.sismics.model;
import java.util.List;
public class KeycloakCertKeys {
public List<KeycloakCertKey> keys;
public KeycloakCertKeys() {
}
public List<KeycloakCertKey> getKeys() {
return keys;
}
public void setKeys(List<KeycloakCertKey> keys) {
this.keys = keys;
}
}

View File

@ -10,11 +10,13 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.Base64; import java.util.Base64;
import com.google.gson.Gson;
import com.sismics.docs.core.constant.Constants; import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.UserDao; import com.sismics.docs.core.dao.UserDao;
import com.sismics.docs.core.model.jpa.User; import com.sismics.docs.core.model.jpa.User;
import com.sismics.model.KeycloakCertKeys; import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
@ -115,17 +117,21 @@ public class JwtBasedSecurityFilter extends SecurityFilter {
assert response.body() != null; assert response.body() != null;
if (response.isSuccessful()) { if (response.isSuccessful()) {
try (Reader reader = response.body().charStream()) { try (Reader reader = response.body().charStream()) {
Gson gson = new Gson(); try (JsonReader jsonReader = Json.createReader(reader)) {
KeycloakCertKeys keys = gson.fromJson(reader, KeycloakCertKeys.class); JsonObject jwks = jsonReader.readObject();
publicKey = keys.getKeys().stream().filter(k -> Objects.equals(k.getKid(), jwt.getKeyId())) JsonArray keys = jwks.getJsonArray("keys");
.findFirst() publicKey = keys.stream().filter(key -> Objects.equals(key.asJsonObject().getString("kid"),
.map(k -> k.getX5c().get(0)) jwt.getKeyId()))
.orElse(""); .findFirst()
log.info("Decoded public key - " + publicKey); .map(k -> k.asJsonObject().getJsonArray("x5c").getString(0))
var decode = Base64.getDecoder().decode(publicKey); .orElse("");
var certificate = CertificateFactory.getInstance("X.509") log.info("X5c is " + publicKey);
.generateCertificate(new ByteArrayInputStream(decode)); var decode = Base64.getDecoder().decode(publicKey);
rsaPublicKey = (RSAPublicKey)certificate.getPublicKey(); log.info("Decoded public key - " + publicKey);
var certificate = CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(decode));
rsaPublicKey = (RSAPublicKey) certificate.getPublicKey();
}
} }
} }
} catch (IOException e) { } catch (IOException e) {