Too many redirects loop issue with Spring Boot, Keycloak and Angular when deploying to another...

Discussão em 'Angular' iniciado por Mahenina RANDRIANARISOA, Fevereiro 21, 2025.

  1. I'm experiencing a redirect loop ("too many redirects") problem with a Spring Boot + Keycloak + Angular application. On my local machine everything works fine; but when I test on another computer, I am systematically sent to an error page indicating too many redirects. I configured Keycloak to handle authentication, then the Spring backend creates cookies (access token, refresh token) and redirects the user to the frontend. I use setSecure(true) for these cookies and a possible setDomain(...). I checked my Valid Redirect URIs in Keycloak, as well as the redirect-uri properties in application.properties, but the loop persists. Do you have any idea what could be causing these infinite redirects and how to fix it? Thank you in advance for your help!

    Here is my methods which allows you to create the cookie

    public void loginRedirect(HttpServletResponse response, HttpSession httpSession) throws IOException {
    var principal = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication();



    var cookie = new Cookie(OIDC_TOKEN, clientService.loadAuthorizedClient(principal.getAuthorizedClientRegistrationId(), principal.getName()).getAccessToken().getTokenValue());
    cookie.setSecure(true);
    cookie.setPath("/");
    cookie.setMaxAge(86400 * 60);

    var refreshTokenCookie = new Cookie(OIDC_REFRESH_TOKEN, clientService.loadAuthorizedClient(principal.getAuthorizedClientRegistrationId(), principal.getName()).getRefreshToken().getTokenValue());
    refreshTokenCookie.setSecure(true);
    refreshTokenCookie.setPath("/");
    refreshTokenCookie.setMaxAge(86400 * 60);

    if (domain != null && !domain.isEmpty()) {
    cookie.setDomain(domain);
    refreshTokenCookie.setDomain(domain);
    }

    response.addCookie(cookie);
    response.addCookie(refreshTokenCookie);

    response.sendRedirect(redirectUrl + "/login");

    httpSession.invalidate();
    }


    and this is my application properties:

    #Spring security
    spring.security.oauth2.resourceserver.jwt.issuer-uri= myUrl
    spring.security.oauth2.account.uri=myUrl
    spring.security.oauth2.client.provider.keycloak.jwk-set-uri= myUrl

    spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
    spring.security.oauth2.client.registration.keycloak.client-id=client-id
    spring.security.oauth2.client.registration.keycloak.client-secret=client-secret
    spring.security.oauth2.client.registration.keycloak.redirect-uri=http://localhost:8080/login/oauth2/code/{registrationId}
    spring.security.oauth2.client.registration.keycloak.scope=openid,roles


    in my computer this works but when i change computer in local , it's didn't work . I have this screen. enter image description here

    But when I am in my PC in local mod with the same profile, I get a redirection and I have but cookie, here is a photo which shows that. Can you help me?please! enter image description here

    I integrated Spring Boot with Keycloak and Angular by configuring Keycloak as the OAuth2 provider. I set up the Keycloak client with valid redirect URIs and configured Spring Security with the proper redirect-uri (e.g., http://localhost:8080/login/oauth2/code/{registrationId}). I also implemented a custom login redirect method that creates secure cookies for the access token and refresh token using setSecure(true) and, when applicable, assigned a domain. This setup worked perfectly on my local machine.

    I expected that when a user authenticated successfully, Keycloak would redirect back to my Spring Boot application, the cookies would be properly created and sent to the browser, and the authentication flow would complete without issues. However, on another machine, I encountered a "too many redirects" error, indicating that the authentication loop was not breaking, possibly due to cookie misconfiguration or mismatched redirect URLs.

    Continue reading...

Compartilhe esta Página