How to integrate or make use of KeyCloak user database in my application?

后端 未结 1 1591
栀梦
栀梦 2020-12-30 13:19

So far I have been playing with KeyCloak and been able to set it up and running the customer-portal example successfully. Now I need to actually use it in my application, an

1条回答
  •  一生所求
    2020-12-30 13:45

    I'm in the process of a conversion almost exactly like this. I had users and roles in a home grown database and used Wildfly security via a custom UsernamePasswordLoginModule. I'm now moving to Keycloak.

    I too had database referential integrity for users to other things. What I've done is to not remove the user table completely but to move all of the user attributes over to Keycloak. I maintain a user table with a very minimal amount of information and a primary key that is the Keycloak "user name" (a GUID). You can get that from getting the principal:

    @Context
    private SecurityContext sc;
    
    ...
    
    String userId = sc.getUserPrincipal().getName();
    

    Now I have a key that I can use with JPA to get a user and tie them to anything they need to be tied to.

    There will be a further step where I get more data from Keycloak about the user. Right now I have enough in the AccessToken:

    KeycloakPrincipal kcPrincipal = (KeycloakPrincipal)(sc.getUserPrincipal());
    AccessToken accessToken = kcPrincipal.getKeycloakSecurityContext().getToken();
    
    String firstName = accessToken.getGivenName();
    String lastName = accessToken.getFamilyName();
    

    but I will eventually have custom user attributes pushed over to the Keycloak side that I'll need to get access to.

    0 讨论(0)
提交回复
热议问题