Login to Keycloak using API

后端 未结 4 2053
执念已碎
执念已碎 2021-01-30 17:38

I have 2 different applications: say Application1 and Application2.

  1. I have integrated Application2 with keyc

相关标签:
4条回答
  • 2021-01-30 17:43

    YES- You can login to the Application-1 with out using keycloak login interface.

    Various client adapters are available for achieving this. here you didn't mentioned your application frame work.

    To know more about the keyclaok client adapters : click here

    For example if you are choosing Node.js adapter then you can follow the link : node.js adapter

    keycloak implementation with node.js adapter, details about the REST api's and token validation mechanism are well explained in this link click for example

    0 讨论(0)
  • 2021-01-30 17:50

    You are effectively asking your users to trust that Application1 will manage their keycloak credentials securely. This is not recommended because

    1. better security is achieved if the user is redirected to keycloak to enter their credentials. In an ideal world no client application should be handling or have access to user credentials.
    2. It defeats the purpose of single sign in where a user should only need to enter their credentials for the first application they need to access (provided their session has not expired)

    But if you control and can trust Application1 and need to do this due to legacy or other reasons then you can enable the Resource Owner Credentials Flow called "Direct Access" on the Keycloak Client Definition, and then POST the user's credentials as a form-urlencoded data type to

    https://<keycloak-url>/auth/realms/<realm>/protocol/openid-connect/token
    

    The paramaters will be

    grant_type=password
    client_id=<Application1's client id>
    client_secret=<the client secret>
    username=<the username>
    password=<the password>
    scope=<space delimited list of scope requests>
    

    The response will be a valid JWT object or a 4xx error if the credentials are invalid.

    0 讨论(0)
  • 2021-01-30 17:50

    If I got your question correctly you are trying to call a bearer-only service through another application that's already logged in, you also didn't mention if you are using Spring Boot or another framework like it, so I'll suppose that you are using the Spring Boot for your server-side application.

    The following example reflects into a simple call of an authenticated API to another one, both using Spring Boot:

    import org.keycloak.KeycloakPrincipal;
    import org.keycloak.adapters.RefreshableKeycloakSecurityContext;
    import org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.context.SecurityContextHolder;
    
    @Component
    public class AnotherServiceClient {
        public TypeOfObjectReturnedByAnotherService getFromAnotherService() {
            RestTemplate restTemplate = new RestTemplate();
            String endpoint = "http://localhost:40030/another/service/url";
            String bearerToken = getAuthorizationToken();
    
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "bearer " + bearerToken);
    
            HttpEntity entity = new HttpEntity(headers);
    
            ResponseEntity<TypeOfObjectReturnedByAnotherService> response = restTemplate.exchange(endpoint, HttpMethod.GET, entity, TypeOfObjectReturnedByAnotherService.class);
    
            return response.getBody();
        }
    
        private String getAuthorizationToken() {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            SimpleKeycloakAccount details = (SimpleKeycloakAccount) authentication.getDetails();
    
            KeycloakPrincipal<?> keycloakPrincipal = (KeycloakPrincipal<?>) details.getPrincipal();
    
            RefreshableKeycloakSecurityContext context = (RefreshableKeycloakSecurityContext) getPrincipal().getKeycloakSecurityContext();
    
            return context.getTokenString();
        }
    }
    

    By that way is possible to send the actual valid token generated by your origin service to another service.

    0 讨论(0)
  • 2021-01-30 17:59

    In the Application2, you have used a grant type called The Authorization Code grant; it is one of several grant types which specified by the OAuth framework.

    The method through which Application1 can gain the Access Token is called password Grant, this grant is no longer recommended to be used except if you trust your app.

    You can find out here the different strategies use to get integration of keycloak with a javascript app in the right way

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