Jwt list of audience in Spring

763 Views Asked by At

When creating the token, I've set a list of audience as follows:

JwtClaims claims = new JwtClaims();
        claims.setIssuer(issuer);
        claims.setAudience(Lists.newArrayList(audiences));
        claims.setExpirationTimeMinutesInTheFuture(60);
        claims.setJwtId(keyId);
        claims.setIssuedAtToNow(); 
        claims.setNotBeforeMinutesInThePast(2);
        claims.setSubject(subject);

The problem comes on the consumer side that is not giving me the expected audience. This is what I've done on the consumer side:

JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                .setRequireExpirationTime()
                .setAllowedClockSkewInSeconds(30)
                .setRequireSubject()
                .setExpectedIssuer(issuer)
                .setExpectedAudience(String.valueOf(Lists.newArrayList(audiences)))
                .setVerificationKey(rsaJsonWebKey.getKey())
                .build();

There's something wrong with the setExpectedAudience but I can't find the problem. This is what I got in the console.

Invalid JWT! org.jose4j.jwt.consumer.InvalidJwtException: JWT (claims->{"iss":"EXAMPLEISSUER","aud":["test1","test2","test3"],"exp":1657880599,"jti":"EXAMPLE_SHA1withRSA","iat":1657876999,"nbf":1657876879,"sub":"example"}) rejected due to invalid claims or other invalid content. Additional details: [[8] Audience (aud) claim [test1, test2, test3] doesn't contain an acceptable identifier. Expected [test1, test2, test3] as an aud value.]
1

There are 1 best solutions below

3
unconditional On

setExpectedAudience() accepts varargs, so make an ordinary String[] array from your audiences and use it as an argument.