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>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<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.util.Base64;
import com.google.gson.Gson;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.UserDao;
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 okhttp3.Request;
import okhttp3.Response;
@ -115,17 +117,21 @@ public class JwtBasedSecurityFilter extends SecurityFilter {
assert response.body() != null;
if (response.isSuccessful()) {
try (Reader reader = response.body().charStream()) {
Gson gson = new Gson();
KeycloakCertKeys keys = gson.fromJson(reader, KeycloakCertKeys.class);
publicKey = keys.getKeys().stream().filter(k -> Objects.equals(k.getKid(), jwt.getKeyId()))
.findFirst()
.map(k -> k.getX5c().get(0))
.orElse("");
log.info("Decoded public key - " + publicKey);
var decode = Base64.getDecoder().decode(publicKey);
var certificate = CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(decode));
rsaPublicKey = (RSAPublicKey)certificate.getPublicKey();
try (JsonReader jsonReader = Json.createReader(reader)) {
JsonObject jwks = jsonReader.readObject();
JsonArray keys = jwks.getJsonArray("keys");
publicKey = keys.stream().filter(key -> Objects.equals(key.asJsonObject().getString("kid"),
jwt.getKeyId()))
.findFirst()
.map(k -> k.asJsonObject().getJsonArray("x5c").getString(0))
.orElse("");
log.info("X5c is " + publicKey);
var decode = Base64.getDecoder().decode(publicKey);
log.info("Decoded public key - " + publicKey);
var certificate = CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(decode));
rsaPublicKey = (RSAPublicKey) certificate.getPublicKey();
}
}
}
} catch (IOException e) {