B4J Question help with jserver BasicAuthentication

bigluo

Member
when try to implement @Erel's basicauthentication:
B4X:
Sub AppStart (Args() As String)
    srv.Initialize("server")
    srv.Port = 51042
    srv.AddFilter("/*", "BasicAuthenticationFilter", False)
    srv.AddHandler("/test/*", "testHandler", False)
    srv.Start

in module BacisAuthenticationFilter:
B4X:
Sub Process_Globals
    
End Sub

'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
    If req.GetSession.GetAttribute2("logged in", False) = True Then Return True
    Dim auths As List = req.GetHeaders("Authorization")
    If auths.Size = 0 Then
        resp.SetHeader("WWW-Authenticate", $"Basic realm="Realm""$)
        resp.SendError(401, "authentication required")
        Return False
    Else
        If CheckCredentials(auths.Get(0)) Then
            req.GetSession.SetAttribute("logged in", True)
            Return True
        Else
            resp.SendError(401, "authentication required")
            Return False
        End If
    End If
End Sub

Private Sub CheckCredentials (auth As String) As Boolean
    Dim success As Boolean = False
    If auth.StartsWith("Basic") = True Then
        Dim b64 As String = auth.SubString("Basic ".Length)
        Dim su As StringUtils
        Dim b() As Byte = su.DecodeBase64(b64)
        Dim raw As String = BytesToString(b, 0, b.Length, "utf8")
        Dim UsernameAndPassword() As String = Regex.Split(":", raw)
        If UsernameAndPassword.Length = 2 Then
            'up to you to decide which credentials are allowed <---------------------------
            If UsernameAndPassword(0) = "Username" And UsernameAndPassword(1) = "Password" Then
                success = True
            End If
        End If
    End If
    Return success
End Sub
but when execute, there are errors:
B4X:
Waiting for debugger to connect...
Program started.
2022-01-06 15:33:21.274:INFO::main: Logging initialized @751ms to org.eclipse.jetty.util.log.StdErrLog
Error occurred on line: 21 (Main)
java.lang.NoSuchMethodException: MY.B4J.Server02.basicauthenticationfilter.innerInitializeHelper(anywheresoftware.b4a.BA)
    at java.base/java.lang.Class.getDeclaredMethod(Class.java:2476)
    at anywheresoftware.b4j.object.JServlet.getInitializeMethod(JServlet.java:56)
    at anywheresoftware.b4j.object.JServlet.<init>(JServlet.java:48)
    at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:181)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:673)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:240)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
    at MY.B4J.Server02.main.main(main.java:29)
what's wrong with it?
Thanks!
 

aeric

Expert
Licensed User
Longtime User
Are you using filter class?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…