Https Redirect: Spring boot app running on GKE

In this we will talk about how to redirect http traffic to https.

For this we will be using a Spring boot app which is running on Google kubernetes engine. We will also use the Tuckey url re-writer.

For this we assume you already know how to run a spring boot service on GKE. A sample repository can be found here

Extend the UrlRewriteFilter which re-writes the url based on the rules provided in the xml config.

Below is one of the rule configuration which we have used in multiple apps.

In the rule you can see 3 different urls which we are re-writing. The http rules are obvious but why are we re-writing rule.

As we want to run this service behind gke ingress controller which only supports one https certificate per domain and also doesn’t support wild card certificates. We had generated the certificate using so any url without www will give certificate error.

If you deploy your spring boot application using just above two changes then ingress will not be able to ping your service.

The ping request expects 200 response code, where as this would give 302 which is a redirect response. The reason behind this is GKE ingress does the ssl termination and ingress to GKE is http request.

The last piece of trick is to set following properties in your spring boot app

Above would ensure that your service gets the protocol and ip address of the end client and not the ingress server.

If you follow the above steps you should be able to re-direct any of your http endpoints to https.

Let us know if have some issues settings things up.

Follow us on twitter:

Simplifying writing cloud connected Apps.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store