Handle Exception – “bad request this combination of host and port requires TLS” or not

When running or planning to run a website, the most look after thing is security. The HTTP is not a secure way to communicate between server and client. We always go for HTTPS, and to enable it, we use the TLS (transport layer security) in the application. It is used to protect data between server and client in transit. TLS is the successor of SSL, and both are used interchangeably. When using TLS protocol, you may get ‘”bad request this combination of host and port requires TLS” or not’.  

You don’t need to worry as we can help you get rid of this exception with the best possible solutions. But before that, let’s have a look at how you encounter error warning

How the error occurs

When you are working with Spring Boot, you get the error once you compile the program. Have a look at the program that you are trying to use

http://localhost:8081/points/12345/search
then you may have added
server.ssl.key-store=classpath:KeyStore.jks
 server.ssl.key-store-password=test

Controller code

@RestController
 @SpringBootApplication
 public class MainApplication {
  @RequestMapping(value = "/points/{point}/search", method = RequestMethod.GET)
  public PointsType getPoint(@PathVariable(value = "point") String point) {
  System.out.println("hi"); // Never printed
  return null;
  }
  public static void main(String[] args) {
  SpringApplication.run(MainApplication.class, args);
  }
 }

The application.properties file

spring.application.name=sge
 server.port=8081
 server.ssl.key-store=classpath:KeyStore.jks
 server.ssl.key-store-password=test
 server.ssl.key-alias=homologation-staging
 server.ssl.trust-store=classpath:TrustStore.jks
 server.ssl.trust-store-password=test
 server.ssl.client-auth=need
 security.require-ssl=true
 server.tomcat.remote_ip_header=x-forwarded-for
 server.tomcat.protocol_header=x-forwarded-proto

As soon as you operate the program, you get the error warning

Could not get any response
 There was an error connecting to https://localhost:8081/points/12345/search.

This is how you land up in trouble. Have a look at the solution section

Solutions to Fix ‘”bad request this combination of host and port requires TLS” or not’

The wrong certification leads to error. The use of a server certificate is the main root cause of the error.  Check out the solutions

Solution 1 – Change the Protocol

A minor change can have a meaningful result. You may have already used the default protocol as https, but at the time of calling, you also need to use the same default protocol instead of http.

So, you need to use https instead of http. This solution can be helpful to get rid of the error warning.

Solution 2 – Have a client certificate

When you install a server certificate on your domain, you get an error warning pops up. There are times when you get confused between the client certificate and the server certificate. It may happen, but as soon as you get the error message, you need to check the certificate. To make a request, you need to use the client certificate. Using the client certificate can solve the error warning.

Conclusion

Here you have the solutions to handle the exception ‘”bad request this combination of host and port requires TLS” or not’. You can simply remove the error warning with a few fixes.

I hope you find it helpful!

If you like solutions, drop a message in the below comment box.

Reference Source:

https://stackoverflow.com/questions/58595470/bad-request-this-combination-of-host-and-port-requires-tls-with-spring-boot

Leave a Reply

Your email address will not be published. Required fields are marked *