Skip to content

Latest commit

 

History

History
130 lines (92 loc) · 3.88 KB

File metadata and controls

130 lines (92 loc) · 3.88 KB

MCP OAuth2 Demo

Bu proje, hem:

  • Spring Authorization Server (JWT erişim tokenlarını client_credentials akışı ile veren), hem de
  • kendi /hello uç noktasını koruyan bir Resource Server olan

minimal bir Spring Boot uygulamasıdır.

Spring blog yazısında (2 Nis 2025) gösterilen yapılandırmayı yansıtır.


Hızlı başlangıç (yerel)

# 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

OAuth2 Yapılandırmasını Test Etme

OAuth2 güvenlik yapılandırmasını aşağıdaki adımlarla test edebilirsiniz:

1. Sunucunun çalıştığını ve güvenli olduğunu doğrulayın

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

2. İstemci kimlik bilgileri ile erişim tokenı alın

# 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

Not: Basic Authentication başlığı (bWNwLWNsaWVudDpzZWNyZXQ=) is the Base64 encoding of mcp-client:secret).

3. Token ile korunan uç noktaya erişin

# 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

"Hello from MCP OAuth2 Demo!" mesajı ile başarılı bir yanıt, OAuth2 yapılandırmasının doğru çalıştığını gösterir.


Container oluşturma

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

Azure Container Apps üzerine dağıtım

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, sizin issuer adresiniz olur (https://<fqdn>).
Azure provides a trusted TLS certificate automatically for *.azurecontainerapps.io).


Azure API Management ile entegrasyon

API'nize aşağıdaki inbound policy'i ekleyin:

<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, JWKS'i alacak ve her isteği doğrulayacaktır.


Sonraki adımlar

Feragatname:
Bu belge, AI çeviri hizmeti Co-op Translator kullanılarak çevrilmiştir. Doğruluk için çaba sarf etsek de, otomatik çevirilerin hatalar veya yanlışlıklar içerebileceğini lütfen unutmayın. Orijinal belge, kendi ana dilinde yetkili kaynak olarak kabul edilmelidir. Kritik bilgiler için profesyonel insan çevirisi önerilir. Bu çevirinin kullanımı sonucu ortaya çıkabilecek herhangi bir yanlış anlama veya yanlış yorumdan sorumlu değiliz.