You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/04/08 01:48:39 UTC
[james-project] branch master updated: [PERF] Reuse JwtTokenVerifier parsers (#956)
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push:
new a94223cbe9 [PERF] Reuse JwtTokenVerifier parsers (#956)
a94223cbe9 is described below
commit a94223cbe9dd5aff4f94ec7d0db5f51133bf377a
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Fri Apr 8 08:48:34 2022 +0700
[PERF] Reuse JwtTokenVerifier parsers (#956)
- Provides an Immutable concurrent friendly JWT parser
- Avoids heavy instanciations that takes up to 70% of CPU
- Avoids deprecation warnings
---
.../org/apache/james/jwt/JwtTokenVerifier.java | 27 +++++++++++++---------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/server/protocols/jwt/src/main/java/org/apache/james/jwt/JwtTokenVerifier.java b/server/protocols/jwt/src/main/java/org/apache/james/jwt/JwtTokenVerifier.java
index 55c1f7faf7..c5bc4ff9b8 100644
--- a/server/protocols/jwt/src/main/java/org/apache/james/jwt/JwtTokenVerifier.java
+++ b/server/protocols/jwt/src/main/java/org/apache/james/jwt/JwtTokenVerifier.java
@@ -25,9 +25,12 @@ import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.collect.ImmutableList;
+
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
+import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
@@ -44,10 +47,13 @@ public class JwtTokenVerifier {
private static final Logger LOGGER = LoggerFactory.getLogger(JwtTokenVerifier.class);
- private final List<PublicKey> publicKeys;
+ private final List<JwtParser> jwtParsers;
public JwtTokenVerifier(PublicKeyProvider pubKeyProvider) {
- this.publicKeys = pubKeyProvider.get();
+ this.jwtParsers = pubKeyProvider.get()
+ .stream()
+ .map(this::toImmutableJwtParser)
+ .collect(ImmutableList.toImmutableList());
}
public Optional<String> verifyAndExtractLogin(String token) {
@@ -56,14 +62,14 @@ public class JwtTokenVerifier {
}
public <T> Optional<T> verifyAndExtractClaim(String token, String claimName, Class<T> returnType) {
- return publicKeys.stream()
- .flatMap(key -> verifyAndExtractClaim(token, claimName, returnType, key).stream())
+ return jwtParsers.stream()
+ .flatMap(parser -> verifyAndExtractClaim(token, claimName, returnType, parser).stream())
.findFirst();
}
- private <T> Optional<T> verifyAndExtractClaim(String token, String claimName, Class<T> returnType, PublicKey publicKey) {
+ private <T> Optional<T> verifyAndExtractClaim(String token, String claimName, Class<T> returnType, JwtParser parser) {
try {
- Jws<Claims> jws = parseToken(token, publicKey);
+ Jws<Claims> jws = parser.parseClaimsJws(token);
T claim = jws
.getBody()
.get(claimName, returnType);
@@ -88,10 +94,9 @@ public class JwtTokenVerifier {
}
}
- private Jws<Claims> parseToken(String token, PublicKey publicKey) throws JwtException {
- return Jwts
- .parser()
- .setSigningKey(publicKey)
- .parseClaimsJws(token);
+ private JwtParser toImmutableJwtParser(PublicKey publicKey) {
+ return Jwts.parserBuilder()
+ .setSigningKey(publicKey)
+ .build();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org