Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 3.83 KB

File metadata and controls

128 lines (91 loc) · 3.83 KB

MCP OAuth2 Demo

Proyek ini adalah aplikasi Spring Boot minimal yang berfungsi sebagai:

  • Spring Authorization Server (mengeluarkan token akses JWT melalui flow client_credentials), dan
  • Resource Server (melindungi endpoint /hello miliknya sendiri).

Ini mencerminkan pengaturan yang ditunjukkan dalam posting blog Spring (2 Apr 2025).


Mulai cepat (lokal)

# build & run
./mvnw spring-boot:run

# obtain a token
curl -u mcp-client:secret -d grant_type=client_credentials \
     http://localhost:8081/oauth2/token | jq -r .access_token > token.txt

# call the protected endpoint
curl -H "Authorization: Bearer $(cat token.txt)" http://localhost:8081/hello

Menguji Konfigurasi OAuth2

Anda dapat menguji konfigurasi keamanan OAuth2 dengan langkah-langkah berikut:

1. Pastikan server berjalan dan terlindungi

# This should return 401 Unauthorized, confirming OAuth2 security is active
curl -v http://localhost:8081/

2. Dapatkan token akses menggunakan client credentials

# Get and extract the full token response
curl -v -X POST http://localhost:8081/oauth2/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Basic bWNwLWNsaWVudDpzZWNyZXQ=" \
  -d "grant_type=client_credentials&scope=mcp.access"

# Or to extract just the token (requires jq)
curl -s -X POST http://localhost:8081/oauth2/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Basic bWNwLWNsaWVudDpzZWNyZXQ=" \
  -d "grant_type=client_credentials&scope=mcp.access" | jq -r .access_token > token.txt

Catatan: Header Basic Authentication (bWNwLWNsaWVudDpzZWNyZXQ=) is the Base64 encoding of mcp-client:secret.

3. Akses endpoint yang dilindungi menggunakan token

# Using the saved token
curl -H "Authorization: Bearer $(cat token.txt)" http://localhost:8081/hello

# Or directly with the token value
curl -H "Authorization: Bearer eyJra...token_value...xyz" http://localhost:8081/hello

Respon berhasil dengan "Hello from MCP OAuth2 Demo!" menandakan konfigurasi OAuth2 berfungsi dengan benar.


Membangun Container

docker build -t mcp-oauth2-demo .
docker run -p 8081:8081 mcp-oauth2-demo

Deploy ke Azure Container Apps

az containerapp up -n mcp-oauth2 \
  -g demo-rg -l westeurope \
  --image <your-registry>/mcp-oauth2-demo:latest \
  --ingress external --target-port 8081

Ingress FQDN menjadi issuer Anda (https://<fqdn>).
Azure provides a trusted TLS certificate automatically for *.azurecontainerapps.io).


Integrasi dengan Azure API Management

Tambahkan kebijakan inbound ini ke API Anda:

<inbound>
  <validate-jwt header-name="Authorization">
    <openid-config url="https://<fqdn>/.well-known/openid-configuration"/>
    <audiences>
      <audience>mcp-client</audience>
    </audiences>
  </validate-jwt>
  <base/>
</inbound>

APIM akan mengambil JWKS dan memvalidasi setiap permintaan.


Selanjutnya

Penafian:
Dokumen ini telah diterjemahkan menggunakan layanan terjemahan AI Co-op Translator. Meskipun kami berusaha untuk akurasi, harap diketahui bahwa terjemahan otomatis mungkin mengandung kesalahan atau ketidakakuratan. Dokumen asli dalam bahasa aslinya harus dianggap sebagai sumber yang sah. Untuk informasi penting, disarankan menggunakan terjemahan manusia profesional. Kami tidak bertanggung jawab atas kesalahpahaman atau salah tafsir yang timbul dari penggunaan terjemahan ini.