Search code examples
spring-boothttpsecuritykeycloakhttp-status-code-403

How to disable auto login redirect in Spring Boot Keycloak Adapter


I am writing an API in Spring Boot that I want to secure using Keycloak. After doing some setting up, I managed to get the keycloak adapter to work. While I was expecting a 403 on any non-authenticated request, I get an HTTP 302 redirect to login page instead. As I am working on an API, how can I disable the auto login-redirect and provide a 403 error message, so that I could add some frontend logic to start on the login process?


Solution

  • In your config, you do

    http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint())
    

    It will start returning 403 on every failed call and you can then catch it and whatever logic you need.