Skip to main content

Secure your Spring Boot REST API with Keycloak

🕓 40 minutes

What you’ll learn

  • How to add security to your REST API built on SpringBoot.
  • How to install a Keycloak managed service in CodeNOW.
  • How to configure the Keycloak basics.
  • Basic concepts of authorization and authentication using Keycloak, OAUTH2.

Project Source

This sample project can be cloned from:


  • You are already familiar with CodeNOW basics.
  • You have successfully built a REST API application component with Spring Boot scaffolder. See the How To Create Simple Spring Boot REST API tutorial.
  • You have already set up the project in your local development environment and imported into your IDE.
  • You are already familiar with Postman (REST client) basics.

Keycloak installation and setup

Follow Get New Keycloak tutorial

Securing our SpringBoot application

  1. Add the following dependencies to pom.xml


    and also under the dependency management

  2. Add the Keycloak adapter configuration class as follows:

    package org.example.service.config;

    import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
    import org.keycloak.adapters.springsecurity.KeycloakConfiguration;
    import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
    import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Import;

    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class KeycloakAdapterConfig extends KeycloakWebSecurityConfigurerAdapter {

    /* Registers the KeycloakAuthenticationProvider with the authentication manager.*/

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

    /* Defines the session authentication strategy.*/

    protected NullAuthenticatedSessionStrategy sessionAuthenticationStrategy() {
    return new NullAuthenticatedSessionStrategy();
    protected void configure(HttpSecurity http) throws Exception
  3. Configure the connection to Keycloak service. Add the following values to our /codenow/config/application.yaml.

    realm: sample
    resource: api
    public-client: true
    bearer-only: true

    Please note:

    realm: is our realm configured in Keycloak admin.

    auth-server-url: is our authorization endpoint (see above).

    resource: is our Client ID.

  4. Finally, secure our endpoints with @PreAuthorize annotation. Don't forget to replace api-role parameter with the role name, that you've entered in Keycloak setup.

    public class CustomerController {
    CustomerService customerService;

    public List<Customer> getAll() {
    return customerService.getAll();

    public ResponseEntity<Customer> getCustomer(@PathVariable int id) {
    Customer customer = customerService.getById(id);

    if (customer != null)
    return new ResponseEntity<>(customer, HttpStatus.ACCEPTED);
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);

Testing in the local environment

  1. If you need to change the configuration parameters, for example the TCP port, please do so in codenow/config/application.yaml.

  2. Build and run our application. By default it runs on http://localhost:8080/customers

  3. Open the request in Postman, select "No Auth" as authorization type. You should receive a "401 Unauthorized" response

  4. Change the authorization type to "OAUTH2" and click on "Get New Access Token".

  5. Enter the Access Token URL, Username, Password and Client ID. Those have been set up in the "Keycloak installation and setup" chapter. Fill Scope field.

  6. You should get a new token, click on "Use Token"

  7. Send the request again and this time you should get the correct response.


Deploy to CodeNOW

If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment.

What’s next?

See our other developer tutorials: