Azure-API-Management-Deploy

Azure-API-Management-Deploy

이 글에서는 Azure API Management를 구축해보겠다.

API Management에서 APIs에서 미리 만들어둔 App Service를 browse하여 Api를 만든다

생성된 api의 test 탭에 들어가면

그냥 url 만 복사하여 요청하면

1
curl --location 'https://apimgmt-az01-og084501-dev-ktintelliagent-test-01.azure-api.net/test20250131'

아래와 같이 401 에러가 난다

1
2
3
4
{
"statusCode": 401,
"message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API."
}
  • Ocp-Apim0Subscription-Key가 없어서 그렇다

Ocp-Apim-Subscription-Key

API Management의 규격이다

Header에 넣어주면 된다

Ocp-Apim-Trace:c도 넣어야 할까?

  • APIs에 Subscription에 Built-in all-access subscription 항목에 있는 키를 가져오면 된다
  • Pruduct(제품) 전용 키라고 생각하면 된다.

API Management에 GET

1
2
3
curl --location 'https://apimgmt-az01-og084501-dev-ktintelliagent-test-01.azure-api.net/test20250131' \
--header 'Ocp-Apim-Subscription-Key: f917567d3d2c4c46a5ddf04c33550ef8' \
--header 'Ocp-Apim-Trace: true'

API Management에 POST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
curl --location 'https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net/hello' \
--header 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
--header 'Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7' \
--header 'Cache-Control: no-cache' \
--header 'Connection: keep-alive' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Origin: https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net' \
--header 'Pragma: no-cache' \
--header 'Referer: https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net/' \
--header 'Sec-Fetch-Dest: document' \
--header 'Sec-Fetch-Mode: navigate' \
--header 'Sec-Fetch-Site: same-origin' \
--header 'Sec-Fetch-User: ?1' \
--header 'Upgrade-Insecure-Requests: 1' \
--header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
--header 'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"' \
--header 'sec-ch-ua-mobile: ?0' \
--header 'sec-ch-ua-platform: "macOS"' \
--header 'Ocp-Apim-Subscription-Key: f917567d3d2c4c46a5ddf04c33550ef8' \
--header 'Ocp-Apim-Trace: true' \
--data-urlencode 'name=kahlua'
  • 위 헤더들에서 줄여서 아래처럼 보내도 무방하다
1
2
3
4
5
6
7
curl --location 'https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net/hello' \
--header 'Origin: https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net' \
--header 'Referer: https://appsvc-az01-dev-ktintelliagent-prototype-01-daeya8a3dwdcegfs.koreacentral-01.azurewebsites.net/' \
--header 'Ocp-Apim-Subscription-Key: f917567d3d2c4c46a5ddf04c33550ef8' \
--header 'Ocp-Apim-Trace: true' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'name=kahlua'

Azure API Management(APIM)에 OAuth2.0 인증 설정

1. API Management에 OAuth 2.0 인증 설정

Azure API Management에서 OAuth 2.0을 사용하여 액세스 토큰을 발급하고 API 요청을 인증하려면, 먼저 OAuth 2.0 제공자를 설정해야 합니다.

(1) Azure Active Directory (Azure AD)에서 App 등록

  1. Azure Portal에서 Azure Active Directory > 앱 등록 > 새 등록을 클릭
  2. 리디렉션 URIhttps://oauth.pstmn.io/v1/browser-callback 또는 API Management 엔드포인트로 설정
  3. 애플리케이션(클라이언트) ID디렉터리(테넌트) ID 저장

(2) 클라이언트 자격 증명 생성

  1. 인증서 및 암호 > 새 클라이언트 암호 생성
  2. 생성된 클라이언트 암호 값 저장

(3) API 권한 추가

  1. API 권한 > 권한 추가
  2. Microsoft Graph 또는 사용할 API 선택 > Application permissions 또는 Delegated permissions 선택
  3. access_as_user 또는 필요한 권한 부여

2. API Management에서 OAuth 2.0 제공자 등록

  1. Azure API Management 포털로 이동
  2. OAuth 2.0 인증 제공자 > 새 OAuth 2.0 제공자 추가

  1. 인증 엔드포인트토큰 엔드포인트 입력

    • 인증 엔드포인트:

      1
      https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize
    • 토큰 엔드포인트:

      1
      https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token
  2. 클라이언트 ID 및 클라이언트 암호 입력

  3. 권한 부여 방식으로 Authorization Code 또는 Client Credentials 선택


3. API Management 정책 설정 (액세스 토큰 인증)

API 요청이 들어올 때 액세스 토큰을 검증하는 정책을 설정해야 합니다.

(1) API Management 정책 추가

  1. Azure API Management > APIs > 대상 API 선택
  2. 정책 편집기(Inbound Processing)에서 다음 정책 추가
1
2
3
4
5
6
7
8
9
10
<inbound>
<base />
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Invalid token.">
<openid-config url="https://login.microsoftonline.com/{tenant_id}/v2.0/.well-known/openid-configuration" />
<required-claims>
<claim name="aud">YOUR-CLIENT-ID</claim>
</required-claims>
</validate-jwt>
</inbound>

  • {tenant_id}: Azure AD 테넌트 ID
  • YOUR-CLIENT-ID: 앱 등록한 클라이언트 ID

4. API 호출 테스트

  1. 액세스 토큰 발급 (OAuth 2.0 client_credentials 사용)

    1
    2
    3
    curl -X POST "https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&scope=api://{client_id}/.default"
  2. 응답 받은 액세스 토큰을 사용하여 API 호출

    1
    2
    curl -X GET "https://{api-management-endpoint}/api/resource" \
    -H "Authorization: Bearer {access_token}"

Author

hamin

Posted on

2025-01-21

Updated on

2025-02-25

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.