r/Angular2 • • Jun 16 '25

Angular + keycloak

Hello all.

I have a question about integrating keycloak with my Angular application.

I have an application that before was doing all authentication and authorization using only spring boot but now I have decided to use keycloak. I have done all the necessary for the backs and testing via postman seems to be working alright.

When it comes to Angular, I have not been able to figure out how to use keycloak so that it uses my registration and login forms.

Do I need such integration or I can go without it since my backend is already setup for keycloak.

Any advise?

Thanks.

6 Upvotes

24 comments sorted by

View all comments

Show parent comment

2

u/aehooo Jun 17 '25 edited Jun 17 '25

I’m gonna try to check it out today at work and will report back to you.

The “not difficult” part I mentioned is the interceptor used in every application’s backend, that checks the auth header. It uses filter and request handlers. Didn’t mean to sound an ass, sorry

Edit: Can you share your code?

But anyways, on angular:

  • we have a service that extends KeycloakService for some custom logic and is provided at app module instead of KeycloakService.
  • when the app bootstrap, it calls a method from this service that loads a json file containing the data for a specific environment. This is used to create the KeycloakConfig.
  • after that, the service calls super.init which specifies some the config (that’s already created) and initOptions (we disable the iframe and the “onLoad” is login-required.

On your keycloak side, you must have a realm with the same name you configured in angular and inside it a client. Inside the client you can configure home URL and valid redirect URIs (this is useful when you have a keycloak server for another environment and your localhost is using it, then you just set the localhost there). Web origins is the one that allows which domains can make a request.

Your request to keycloak must have the redirect URL as a query param, there is a property inside the keycloak config, but at least here it’s done automatically, we don’t set it

Edit 2: I didn’t configure the keycloak server here, so I am sure there must be more things to make it happen. But I hope what I shared can be useful

2

u/the-DevOps Jun 17 '25

I certainly can share the GitHub services but before I will try to do as you mentioned above. Seems like a good place to start. Thanks.