B4J Question How To Implement JWT on B4J web server

adjie

Member
Licensed User
Longtime User
Currently I'm trying to implement JWT (JSON Web Tokens) in B4J server. I'm not familiar with java.
from this site https://github.com/jwtk/jjwt i got information that to get the token, the quick start is like this (in java) :
B4X:
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.crypto.MacProvider;
import java.security.Key;

// We need a signing key, so we'll create one just for this example. Usually
// the key would be read from your application configuration instead.
Key key = MacProvider.generateKey();

String compactJws = Jwts.builder()
  .setSubject("Joe")
  .signWith(SignatureAlgorithm.HS512, key)
  .compact();


What I have done is add the jar file to additional file like this.
#AdditionalJar: jjwt-0.8.0-SNAPSHOT.jar
B4X:
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
    MainForm.RootPane.LoadLayout("frmMain") 
jo.InitializeStatic("io.jsonwebtoken.Jwts")
        MainForm.Show
End Sub

Sub cmdConnect_Action
    Dim cout As String
    jo.RunMethod("builder",Null)
    jo.runMethod("setSubject",Null)
    cout = jo.RunMethod("compact",Null)
    log(cout)
End Sub
but It just error on line jo.RunMethod("builder",Null)

how to implement it correctly in b4j ?

here is the complete error
Waiting for debugger to connect...
Program started.
Error occurred on line: 45
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:656)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:165)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1296242029.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/580024961.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/355629945.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
at io.jsonwebtoken.Jwts.builder(Jwts.java:116)
... 29 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 30 more
 
Last edited:

adjie

Member
Licensed User
Longtime User
Has adding the jackson jar #AdditionalJar: com.fasterxml.jackson.core
but still no luck.

I need it to convert my php web server to b4j web server. the web app using angularjs(javascript) wich using JWT (Json Web Token https://jwt.io/) . It supported by many languages like .net, node.js, python, java, perl, ruby, php, etc..

very similar with b4x encryption https://www.b4x.com/android/forum/threads/jb4xencryption.48178/#content
but I really need this cause the front end using javascript. Many website / web apps that use jwt as security token. Android coded in java also using jwt.
Any help erel ?
 
Upvote 0

adjie

Member
Licensed User
Longtime User
Here is the project and the two jars in one zip file.

Need two methods:
1. jwt builder
2. jwt verify. The java code is like this:
assert Jwts.parser().setSigningKey(key).parseClaimsJws(compactJws).getBody().getSubject().equals("Joe");
 

Attachments

  • jwt-test.zip
    321.5 KB · Views: 297
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
  #AdditionalJar: jjwt-0.8.0-SNAPSHOT.jar
  #AdditionalJar:jackson-core-2.0.0
   #AdditionalJar:jackson-databind-2.0.0
   #AdditionalJar:jackson-annotations-2.0.0

You can find the three jars here: http://repo1.maven.org/maven2/com/fasterxml/jackson/core/

B4X:
Log(jo.RunMethodJO("parser", Null).RunMethodJO("setSigningKey", Array(key)).RunMethodJO("parseClaimsJws", compactJws"). _
  RunMethodJO("getBody", Null).RunMethodJO("equals", Array("Joe"))
 
Upvote 0
Top