The “io.jsonwebtoken.MalformedJwtException: JWT strings must contain exactly 2 period characters” is an exception that occurs when parsing a JSON Web Token (JWT) string, and the string does not adhere to the expected format.
The JWT format consists of three parts: a header, a payload, and a signature, with each part being separated by a period character. This exception is thrown when the given JWT string does not contain the expected number of period characters.
To illustrate this with an example, let’s assume we have the following JWT string:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
As you can see, this JWT string contains two period characters, which separate the header, payload, and signature. However, if we remove one of the period characters from the string like this:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
Now the JWT string only contains one period character. When trying to parse this string, the “MalformedJwtException” will be thrown, indicating that the JWT string does not conform to the expected format.
To fix this error, you need to ensure that your JWT string contains exactly two period characters, separating the header, payload, and signature. Make sure that the JWT string you are working with is valid and has been generated correctly.