0

I am configuring jwt into my spring boot project. The jwt token is generated with the username

public String generateTokenFromUsername(String username) {
    return Jwts.builder().setSubject(username).setIssuedAt(new Date())
            .setExpiration(new Date((new Date()).getTime() + jwtExpirationMs)).signWith(SignatureAlgorithm.HS256, jwtSecret)
            .compact();
}
public String generateJwtToken(UserDetailsImpl userPrincipal) {
        logger.info(userPrincipal.getUsername());
        return generateTokenFromUsername(userPrincipal.getUsername());
    }

I am able to successfully register a user however when it is parsed by the validation method

        Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);

The complete validation method:

 public boolean validateJwtToken(String authToken) {
    try {
        Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken);
        return true;
    } catch (SignatureException e) {
        logger.error("Invalid JWT signature: {}", e.getMessage());
    } catch (MalformedJwtException e) {
        logger.error("Invalid JWT token: {}", e.getMessage());
    } catch (ExpiredJwtException e) {
        logger.error("JWT token is expired: {}", e.getMessage());
    } catch (UnsupportedJwtException e) {
        logger.error("JWT token is unsupported: {}", e.getMessage());
    } catch (IllegalArgumentException e) {
        logger.error("JWT claims string is empty: {}", e.getMessage());
    }

It throws back a malformed error

Invalid JWT token: Unable to read JSON value: �z��&�r#�$�3S"

This is a sample json response from postman:

{
"accessToken": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJsdWN5MjEiLCJpYXQiOjE2NDI0NTM4NjQsImV4cCI6MTY0MjU0MDI2NH0.Kynh2wIDCoQD6spaA4q5Q7z4F6l5TL6CXJQ_dqNYZqRl5zoBArubLoIFrphXfC4iB9qY2UfdcW9vU_MI0WB9SA",
"type": "Bearer",
"refreshToken": "3deb73e2-a31f-43de-9423-4a561978e506",
"id": "9e7a208d-770b-4395-b2a3-255d72d59518",
"name": "John Doe",
"email": "lucy21",
"role": [
    "READER"
]}

I don't know where the error is generated from. Could anyone please help?

Sommy
  • 18
  • 3

1 Answers1

0

In my case the error io.jsonwebtoken.MalformedJwtException: Unable to read JSON value: ?z&?r#$3#Sb was related to a simple mistake: instead of a token itself "eyJhbGc..." JWT was trying to decode token with Bearer prefix: "Bearer eyJhbGc....".

Shalguev
  • 81
  • 3