Feature Flags using Kotlin + Spring Boot

Bruna Pereira
Feb 3, 2020 · 3 min read

Not long ago, I wrote an article about Continuous Integration, giving a brief description of the concept and the importance of Feature Flags as part of this process.

Shortly thereafter, I wrote an article about the implementation of Feature Flags using Ruby on Rails, and now the time has come to write about implementation in Kotlin + Spring 😃

My motivation for writing this tutorial is the fact that I couldn’t find implementation anywhere on the internet that was straightforward about using Java/Kotlin, Spring Boot 2.x, and the use of a Console Web.

FF4J is one of the most popular open-source tools you’ll find, and it was the only one which met all the needs I had. Togglz was my first choice, but unfortunately it doesn’t support Kotlin ☹️

Let’s get started.

The base application contains the following dependencies:

  • Kotlin v1.3.50
  • Spring Boot v2.2.1.RELEASE
  • Spring Security
  • JDBC

By the end of this tutorial we’ll have:

  • Feature Flags (of course)
  • Storage in a database
  • Console web for feature management
  • Basic authentication for the console web

1. Add dependencies

The lib is divided into modules, so we need to include the core, the lib web, and the store-springjdbc.

We need thymeleaf to render the web page (it’s a pity that it doesn’t come directly from the ff4j-web component).

2. Lib configurations

The method getFF4J serves as a lib interceptor, and we use it to configure the connection with the database.

The DataSource which is injected in the configuration class is mounting the database connection and expecting the database credentials to be present in your configuration file, as follows:

It’s also necessary to create tables in the database. Using your migration tool of choice, create the script described in the documentation, or do it manually.

3.Servlet configurations

We need to configure the Servlet that will serve our Web Console, and the configuration is pretty simple:

4. Security configurations

Up to this point everything should be working, but since this API is open to the external world, I want to have some security that will make sure that only authorized persons can access this console and enable features in production.

To this end, I’ll use Spring Security’s HTTP basic auth. I’m already using Spring Security to protect the other routes of my application, so all I want is to extend the configuration and add the basic auth to the FF4J route only.

Take note that I wrote the class with @Order(1) because there are other configuration classes. This communicates that this one has precedence over the other file (otherwise my application’s authentication logic would only apply to my route).

I’m also using injected variables to define the username and password. I guarantee that if you were to run this code in production, it would be impossible to inject these values through an environment variable to keep them safe.

I created a class to represent the parameters of my configuration file, this is what it looks like:

If you start up your application and navigate to http://localhost:port/ff4j-web-console, you should see a pop-up asking for your established username and password, and then the FF4J dashboard!🍾 🙌 🎉

5. Using it in code

I’m not a big fan of the lib’s native language, which calls the ff4j method check to verify if a lib is activated. That’s why I extended the class using two methods, and they’re the ones I use in my code. This is what they looked like:

For more details about the functionalities, it’s worth taking a look at the site’s documentation because there’s lots of information, as well as some samples which have various use cases. They’re not always up to date but they’re very useful.

Interested in working with us? We’re always looking for people passionate about technology to join our crew! You can check out our openings here.

Creditas Tech

Our technologies, innovations, digital product management…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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