B4J Question [Solved] Http to Https

magi6162

Well-Known Member
Licensed User
Erel Code:
B4X:
Private Sub ConfigureSSL (SslPort As Int)
    'example of SSL connector configuration
    Dim ssl As SslConfiguration
    ssl.Initialize
    ssl.SetKeyStorePath(File.DirApp & "/cert", "mysite.it.keystore") 'path to keystore file
    ssl.KeyStorePassword = "123456789"
    ssl.KeyManagerPassword = "987654321"
    srvr.SetSslConfiguration(ssl, SslPort)
    'add filter to redirect all traffic from http to https (optional)
    srvr.AddFilter("/*", "HttpsFilter", False)
End Sub
and 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

but if I write http://mysite.it
it does not redirect to https://mysite.it

if I connect with https://mysite.it everything is ok

where am I wrong?
thank you
 

Cableguy

Expert
Licensed User
The redirect has to be made in your server settings.
You can however also set a filter, like the ABMRoot, to redirect from your non-ssl port to the ssl one
 

magi6162

Well-Known Member
Licensed User
Sorry, Is this code correct? (I created a module with the name: 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 do not understand why it does not redirect correctly.
 

OliverA

Expert
Licensed User
What port is your non ssl port set to for your server?
 

magi6162

Well-Known Member
Licensed User
If I write in the browser's address bar:https://...
all right
if I write mysite.it error "no connection"
without specifying the door in both cases
the web app is started with:
sudo nohup java -jar mysite.jar &
 

OliverA

Expert
Licensed User
What port did you set your non-ssl port to?
 

magi6162

Well-Known Member
Licensed User
I would like that by writing mysite.it redirect to https.
no-SSL port: 51045
SSL port : 443
 

OliverA

Expert
Licensed User
Than make your non-ssl port 80. Your server only serves the port that you configure it for. Without giving your URL a port # for http access (not https), the port # is 80. If your non-ssl port is not configured for port 80, then no redirection will occur (since the server does not handle port 80).
 

magi6162

Well-Known Member
Licensed User
thanks,
Now works.
Thanks, I found out that the GCE instance allows port 80 to open,
so I solved it.:)
B4X:
dim port as int = 80
myapp.StarterServer(srvr,"srvr",port)
Ok?
 

OliverA

Expert
Licensed User
Yes
 
Top