Android Question JJWT problem: jwtSign error.1io.jsonwebtoken.lang.UnknownClassException: Unable to find an implementation for interface io.jsonwebtoken.io.Serializer

rosippc64a

Active Member
Licensed User
Longtime User
Hi All!
I found here a code snippet how to use the JJWT library. I did it in B4J, where it works well. But that was only a test, I need to work on android.
So I ported the code into B4A, but there is a strange error. I think there is missing some more import, but I don't know what.
Here is the code:
B4X:
#AdditionalJar: slf4j-api-1.7.36
#AdditionalJar: slf4j-simple-1.7.36.jar
#AdditionalJar: sslcontext-android
#AdditionalJar: jjwt-api-0.11.5.jar
#AdditionalJar: jjwt-impl-0.11.5.jar
#AdditionalJar: jjwt-orgjson-0.11.5.jar
#AdditionalJar:jackson-core-2.12.6.jar
#AdditionalJar:jackson-databind-2.12.6.1.jar
#AdditionalJar:jackson-annotations-2.12.6
...

#if JAVA
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Serializer;
import java.security.Key;
import javax.crypto.SecretKey;
import java.util.Base64;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;

public static String jwtSign(String json,String b64key) throws Exception {
    String jws = "";
    String s = "0";
    try {
        SignatureAlgorithm algorithm = SignatureAlgorithm.RS256;
                byte[] valueDecoded = Base64.getDecoder().decode(b64key);
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(valueDecoded);
                KeyFactory rsaFact = KeyFactory.getInstance("RSA");
                RSAPrivateKey key = (RSAPrivateKey) rsaFact.generatePrivate(keySpec);
        s = "1";
          //after this happen the error:
          //jwtSign error.1io.jsonwebtoken.lang.UnknownClassException:
          //Unable to find an implementation for interface
          //io.jsonwebtoken.io.Serializer using java.util.ServiceLoader.
          //Ensure you include a backing implementation .jar in the classpath,
          //for example jjwt-impl.jar, or your own .jar for custom implementations.
          //
        jws = Jwts.builder()
            .setHeaderParam("alg", "RS256")
            .setPayload(json)
            //.signWith(SignatureAlgorithm.RS256, b64key)
            //.signWith(algorithm,Keys.hmacShaKeyFor(Decoders.BASE64.decode(b64key)))
            .signWith(algorithm,key)
          .compact();
         s = "3";
    } catch (Exception e) {
        BA.Log("jwtSign error." + s + e);
    }

    return jws;
}
#End If
What bothers me is that it worked under B4J, but not under B4A.
Can somebody help me?
 
Top