question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JwtBuilder compact() performance

See original GitHub issue

I use lib JJWT 0.9.0 to generate Token. I have write log code segment generate Token, and I see function compact() run verry slow (664 -151 = 513(ms)) . I used JDK 7. how to the function compact() run faster or How to generate tokens faster? My code:

public String createJWT(String id, String issuer, String subject, List<T> authories, String loginUserData,
            long ttlMillis) {
        //The JWT signature algorithm we will be using to sign the token
        long startTime = System.currentTimeMillis();
        LOGGER.info("createJWT id=" + id + ";issuer=" + ";subject=" + subject);
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;

        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        //We will sign our JWT with our ApiKey secret
        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(ApiSecretKey.getSecretKey());
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

        //Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now).setSubject(subject)
                .setIssuer(issuer);
        if (!CheckObjectUtils.isNullOrEmpty(authories)) {
            builder.claim(AUTHORITIES_KEY, authories);
        }
        if (!CheckObjectUtils.isNullOrEmpty(loginUserData)) {
            builder.claim(LOGIN_USER_DATA_KEY, loginUserData);
        }

        builder.signWith(signatureAlgorithm, signingKey);

        //if it has been specified, let's add the expiration
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }
        LOGGER.info("createJWT process id=" + id + ";issuer=" + ";subject=" + subject + ";time(ms)="
                + (System.currentTimeMillis() - startTime));
        //Builds the JWT and serializes it to a compact, URL-safe string        
        String token = builder.compact();
        LOGGER.info("createJWT end id=" + id + ";issuer=" + ";subject=" + subject + ";time(ms)="
                + (System.currentTimeMillis() - startTime));
        return token;
    }

JsonWebTokenRestApi<Long> jwtApi = new JsonWebTokenRestApiFactory(
                JsonWebTokenRestApiFactory.JWT_JJWT).getJsonWebTokenByType();
        String jwt = jwtApi.createJWT(null, null, "vinhhc_vsc", null, null, 18000);

My console log:


createJWT id=null;issuer=;subject=vinhhc_vsc
createJWT process id=null;issuer=;subject=vinhhc_vsc;time(ms)=151
createJWT end id=null;issuer=;subject=vinhhc_vsc;time(ms)=664

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lhazlewoodcommented, Oct 31, 2018

What does your maven or gradle configuration look like? Did you follow the Installation instructions ?

Please do not change the focus of this ticket which is specifically about compact() performance. If you have general purpose questions about JJWT and you don’t know if it is actually a bug, please ask them on StackOverflow.com and use the jjwt tag. Thanks!

1reaction
V1HVNcommented, Oct 17, 2018

I’ll try and answer you soon. Thank!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[JWT][JJWT] Function compact() very slow - Stack Overflow
Run just the compact() call in a loop and see what happens. ... //Let's set the JWT Claims JwtBuilder builder = Jwts.builder().setId(id).
Read more >
JwtBuilder (JJWT :: API 0.11.2 API) - javadoc.io
public interface JwtBuilder extends ClaimsMutator<JwtBuilder> ... Actually builds the JWT and serializes it to a compact, URL-safe string according to the ...
Read more >
JWTs and Java - Medium
JWT stands for JSON Web Token, is an open standard that defines a compact and self-contained way for securely sharing data between systems....
Read more >
Tutorial: Create and Verify JWTs in Java - Okta Developer
It's a compact way of structuring data built upon primitive types ... //Let's set the JWT Claims JwtBuilder builder = Jwts.builder().
Read more >
JSON Web Token (JWT) Signing Algorithms Overview - Auth0
JSON Web Tokens are used in the industry more and more. The spec which defines them (RFC7519) describes them as a compact, URL-safe...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found