B4J Question SSLConnection tutorial & Http2 example.

R2B2

Member
Licensed User
Hi

I have a feeling this might be embarrassing :) but I'm not getting these examples to work.

SSL Connection tutorial. https://www.b4x.com/android/forum/threads/server-ssl-connections.40130/#content

As well as the Http2 example from this thread -> https://www.b4x.com/android/forum/threads/server-http-2-configuration.61416/

So first off: I created a keystore file with the keytool.

Main.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
 #CommandLineArgs:
 #MergeLibraries: True 
#End Region
Sub Process_Globals
 Public srvr As Server
End Sub
Private Sub ConfigureSSL (SslPort As Int)
 'example of SSL connector configuration
 Dim ssl As SslConfiguration
 ssl.Initialize
 ssl.SetKeyStorePath(File.DirApp, "testing1.keystore") 'path to keystore file
 ssl.KeyStorePassword = "123456"
 ssl.KeyManagerPassword = "654321"
 srvr.SetSslConfiguration(ssl, SslPort)
 'add filter to redirect all traffic from http to https (optional)
 srvr.AddFilter("/*", "HttpsFilter", False)
End Sub
Sub AppStart (Args() As String)
 srvr.Initialize("")
 srvr.Port = 8080
 
 ConfigureSSL(8443)
 
 srvr.Start
 StartMessageLoop
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub

HttpsFilter
B4X:
'Filter class
Sub Class_Globals
 
End Sub
Public Sub Initialize
 
End Sub
'Return True to allow the request to proceed.
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As Boolean
 If req.Secure Then
 Return True
 Else
 resp.SendRedirect(req.FullRequestURI.Replace("http:", "https:") _
  .Replace(Main.srvr.Port, Main.srvr.SslPort))
 Return False
 End If
End Sub

I'm getting a warning about testing1.keystore is not used, and the error i'm getting is:
B4X:
java.lang.IllegalStateException: no valid keystore


Http2 example: Made a keystore file with both passwords set to 123456, named keystore.

B4X:
'Non-UI application (console / server application)
#Region  Project Attributes 
 #CommandLineArgs:
 #MergeLibraries: True 
 #VirtualMachineArgs: -Xbootclasspath/p:alpn-boot-8.1.11.v20170118.jar
#End Region
Sub Process_Globals
 Public srvr As Server
End Sub
Sub AppStart (Args() As String)
 srvr.Initialize("")
 srvr.AddHandler("/test", "Test", False)
 srvr.Port = 51041 'non ssl
 ConfigureSSL(51042)
 srvr.Http2Enabled = True
 srvr.Start
 StartMessageLoop
End Sub
Private Sub ConfigureSSL (SslPort As Int) 
 'example of SSL connector configuration
 Dim ssl As SslConfiguration
 ssl.Initialize
 ssl.SetKeyStorePath(File.DirApp, "keystore") 'path to keystore file
 ssl.KeyStorePassword = "123456"
 ssl.KeyManagerPassword = "123456"
 srvr.SetSslConfiguration(ssl, SslPort)
End Sub


My Java version is:
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Error:
B4X:
Waiting for debugger to connect...
Program started.
2017-08-25 08:45:44.472:INFO::main: Logging initialized @600ms to org.eclipse.jetty.util.log.StdErrLog
Error occurred on line: 18
java.lang.RuntimeException: ALPN is not configured properly (required by HTTP2)
 at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:122)
 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:498)
 at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:657)
 at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
 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:498)
 at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
 at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
 at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
 at b4j.example.main.main(main.java:29)
 

Similar Threads

Top