#Spring Boot: HTTPS redirect

1 messages · Page 1 of 1 (latest)

fading lichen
#

Hi. I have a web application with HTTPS protocol (self-signed certificate) set up. However, whenever I try to access it (i.e. localhost:8443/) I'll get:

Bad Request
This combination of host and port requires TLS.

When I explicitly add the schema to the URL (i.e. https://localhost:8443/, everything works fine. How do I redirect the localhost:8443/ to https://localhost:8443?

grave orbitBOT
#

<@&1004656351647117403> please have a look, thanks.

grave orbitBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

he SSL certificate error occurs because the self-signed certificate is not trusted by your browser. To fix this, you can either import the self-signed certificate into your browser's trusted certificate store or configure your Spring Boot application to redirect HTTP requests to HTTPS.

To redirect HTTP requests to HTTPS in Spring Boot, you can use the TomcatServletWebServerFactory bean and configure it to enable the RedirectConnector. Here's an example:

#
import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpsConfig {

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(org.apache.catalina.Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(createHttpConnector());
        return tomcat;
    }

    private Connector createHttpConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}
#

In this example, we're creating a TomcatServletWebServerFactory bean and overriding its postProcessContext method to add a security constraint that enforces HTTPS for all requests. We're also adding an additional HTTP connector on port 8080 that redirects to HTTPS on port 8443.

With this configuration, when you access http://localhost:8080/, it will automatically redirect to https://localhost:8443/.

Note that you'll need to adjust the ports and other settings based on your specific setup.

fading lichen
#

The above does not help: I do not want to redirect from HTTP to HTTPS. I want the server to understand that no schema means HTTPS schema.

errant island
#

I am just guessing, but it looks like the problem is not your server per say, it is that browser does not know what protocol to use, since it does not recognize the port.
I don't know the answer, but what I would do is look into the network communication, try to figure out how to get the server to instruct browser to switch, rather than answer with http response.
Which is kinda like redirect, but not really