오늘의 인기 글
최근 글
최근 댓글
Today
Total
05-01 02:52
관리 메뉴

우노

[Keycloak] CURL POST를 사용한 액세스 토큰 발급 본문

Network & Security/Keycloak

[Keycloak] CURL POST를 사용한 액세스 토큰 발급

운호(Noah) 2023. 6. 22. 11:39

Username과 Password를 사용해 Client에 접근하고 있다면

  • 아래 curl post 명령어를 통해 토큰을 발급 받을 수 있습니다.

      curl -X POST '<Keycloak Base URL>/realms/<Realm Name>/protocol/openid-connect/token' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data-urlencode 'grant_type=password' \
      --data-urlencode 'client_id=<Client ID>' \
      --data-urlencode 'client_secret=<Client Secret>' \
      --data-urlencode 'username=<User ID>' \
      --data-urlencode 'password=<User Password>' \
      --data-urlencode 'redirect_uri=<Redirect URI>'

Google Login을 사용해 Client에 접근하고 있다면

  • Google Login을 사용해 Client에 접근하고 있다면, curl post 매개변수를 수정해야합니다.

  • grant_type은 authorization_code로 수정해야하며,

  • code는 Authorization Endpoint 접근 및 구글 로그인 이후 리디렉션된 URL에 포함된 인증 코드(Authorization Code)를 사용해야합니다.

    • 먼저, 브라우저로 아래 경로에 접속한 뒤 구글 로그인합니다.

        <Keycloak Base URL>/realms/<Realm Name>/protocol/openid-connect/auth
        ?response_type=code
        &client_id=<Client ID>
        &redirect_uri=<Redirect URI>
    • 로그인에 성공하면, 아래와 같이 파라미터로 넘겨준 redirect uri로 redirect되고, url 파라미터에 code 값이 붙어있는 것을 확인할 수 있습니다.

        <Redirect URI>?session_state=<>&code=<>
  • 위에서 확인한 code 값을 아래 curl post 매개변수에 삽입해 토큰을 발급 받을 수 있습니다.

      curl -X POST '<Keycloak Base URL>/realms/<Realm Name>/protocol/openid-connect/token' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --data-urlencode 'grant_type=authorization_code' \
      --data-urlencode 'client_id=<Client ID>' \
      --data-urlencode 'client_secret=<Client Secret>' \
      --data-urlencode 'code=<Authorization Code>' \
      --data-urlencode 'redirect_uri=<Redirect URI>'

참고

Comments