From 24b4f02a70922ec027387be0f9e5fe608913d03c Mon Sep 17 00:00:00 2001 From: leesam Date: Wed, 20 Mar 2024 11:05:57 +0800 Subject: [PATCH] =?UTF-8?q?[bugfix]=E8=AF=BB=E5=8F=96jwk=E7=9A=84Bug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/conf/security/JwtUtils.java | 36 +++++++------------ src/main/resources/jwk.json | 1 + 2 files changed, 13 insertions(+), 24 deletions(-) create mode 100644 src/main/resources/jwk.json diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java index 949bcba9..eacff188 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/security/JwtUtils.java @@ -24,14 +24,9 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.List; @@ -86,29 +81,22 @@ public class JwtUtils implements InitializingBean { /** * 创建密钥对 + * * @throws JoseException JoseException */ private RsaJsonWebKey generateRsaJsonWebKey() throws JoseException { RsaJsonWebKey rsaJsonWebKey = null; - try { - URL url = getClass().getClassLoader().getResource("jwk.json"); - if (url != null) { - URI uri = url.toURI(); - Path path = Paths.get(uri); - if (Files.exists(path)) { - byte[] allBytes = Files.readAllBytes(path); - String jwkJson = new String(allBytes, StandardCharsets.UTF_8); - final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jwkJson); - List jsonWebKeys = jsonWebKeySet.getJsonWebKeys(); - if (!jsonWebKeys.isEmpty()) { - JsonWebKey jsonWebKey = jsonWebKeys.get(0); - if (jsonWebKey instanceof RsaJsonWebKey) { - rsaJsonWebKey = (RsaJsonWebKey) jsonWebKey; - } - } + try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("/jwk.json"), StandardCharsets.UTF_8))) { + String jwkJson = reader.readLine(); + JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jwkJson); + List jsonWebKeys = jsonWebKeySet.getJsonWebKeys(); + if (!jsonWebKeys.isEmpty()) { + JsonWebKey jsonWebKey = jsonWebKeys.get(0); + if (jsonWebKey instanceof RsaJsonWebKey) { + rsaJsonWebKey = (RsaJsonWebKey) jsonWebKey; } } - } catch (URISyntaxException | IOException e) { + } catch (Exception e) { // ignored } if (rsaJsonWebKey == null) { diff --git a/src/main/resources/jwk.json b/src/main/resources/jwk.json new file mode 100644 index 00000000..912b8f39 --- /dev/null +++ b/src/main/resources/jwk.json @@ -0,0 +1 @@ +{"keys":[{"kty":"RSA","kid":"3e79646c4dbc408383a9eed09f2b85ae","n":"rThRAlbMRceko3NkymeSoN2ICVaDlNBLWv3cyLUeixjWcmuhnPv2JpXmgoxezKZfhH_0sChBof--BaaqSUukl9wWMW1bWCyFyU5qNczhQk3ANlhaLiSgXsqD-NKI3ObJjB-26fnOZb9QskCqrPW1lEtwgb9-skMAfGlh5kaDOKjYKI64DPSMMXpSiJEDM-7DK-TFfm0QfPcoH-k-1C02NHlGWehVUn9FUJ0TAiDxpKj28qOmYh7s1M7OU_h-Sso7LM-5zbftpcO6SINe81Gw9JPd7rKPCRxkw8ROSCCq-JH_zshM80kTK2nWcseGvhQ_4vKQIBp9PrAgCrGJHM160w","e":"AQAB","d":"AwS2NKo6iQS_k7GREg3X-kGh-zest00h4wYFcOHnFFlsczX47PlfArEeASxdAofrpi1soB0zd5UzRHnxAbH1vkexg076hoDQG__nzeQyEKu2K7xCZgdxW_V_cziH9gF3hZ-P2mfl9tPsng6OatElRt5BqaEingy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