Issue
I want to open port 80 for http and port 433 for https. The port 80 should be redirected to 433. I set the server port in application.properties
to port 80:
server.port=80
And to redirect I followed the official spring documentation and created this small class:
public class HTTPSRedirector {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.redirectToHttps();
return http.build();
}
}
But now the server times out and when I try using curl with the following command:
curl -v http://www.example.com/
I get a Bad request
response because SSL/TLS is enabled. If I try to ping / contact the https port like this:
curl -v https://www.example.com/
then I get the message that the port is not opened and the connection was refused. How can I open both ports 80 and 433 in my Java spring project?
Solution
This is the way how I finally fixed it:
I used the bean listed in my question. This bean redirects all http requests to https.
ADDITIONALLY to that I set the server port to:
server.port=443
which is the default port for https
. With these two changes the server works fine and everything is https.
Answered By - F_Schmidt