Description
This issue is being created to supersede similarly reported issues and represent a canonical issue to enable the following:
-
Check to see if
java.util.Base64
is available at runtime usingClasses.isAvailable
(or similar), and if so, use that. This is the best/fastest one available to most default environments and should be used whenever possible.java.util.Base64
is available on JDK >= 8 and Android level >= 26 -
If
java.util.Base64
is not available:-
If the runtime enviornment is Android, use android.util.Base64. This is available on Android API level >= 8 (i.e. >= Android 2.2, "Froyo")
-
Otherwise, assume < JDK8, and use
javax.xml.bind.DatatypeConverter
. JDK 7 has this included automatically.
-
-
In any case, enable
JwtBuilder
andJwtParser
methods that allow a user to specify their own Base64TextCodec
if/when desired, e.g.Jwts.parser().setBase64Codec(myBase64Codec)
This last option would allow the user to enable Base64 support in any environment that doesn't match 1 and 2 above. -
Ensure any codec implementation will throw exceptions on invalid inputs when possible. See Consider using a different Base64 decoder #143 as an example. Exceptions should be thrown when possible to indicate invalid input, wrapping/propagating the original codec exception.
Implementation note: Perhaps instead of using javax.xml.bind.DatatypeConverter
- which doesn't always fail on invalid inputs (see #143), we should embed the Commons Codec or MigBase64 implementation.