Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 4.18 KB

File metadata and controls

128 lines (91 loc) · 4.18 KB

הדגמת MCP OAuth2

הפרויקט הזה הוא אפליקציית Spring Boot מינימלית שפועלת בתור גם:

  • שרת הרשאות Spring (המנפיק אסימוני גישה JWT דרך הזרם client_credentials), וגם
  • שרת משאבים (המגן על נקודת הקצה שלו /hello).

הוא משקף את ההגדרה שמוצגת ב-פוסט הבלוג של Spring (2 באפריל 2025).


התחלה מהירה (מקומית)

# 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

ניתן לבדוק את תצורת האבטחה של OAuth2 באמצעות השלבים הבאים:

1. וודא שהשרת פועל ומאובטח

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

2. קבל אסימון גישה באמצעות אישורי לקוח

# 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

[!NOTE] כותרת האימות הבסיסית (bWNwLWNsaWVudDpzZWNyZXQ=) is the Base64 encoding of mcp-client:secret.

3. גש לנקודת הקצה המוגנת באמצעות האסימון

# 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!" מאשרת שהתצורה של OAuth2 פועלת כראוי.


בניית מכולה

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

פריסה ל-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 הופך להמנפיק שלך (https://<fqdn>).
Azure provides a trusted TLS certificate automatically for *.azurecontainerapps.io.


חיבור ל-Azure API Management

הוסף מדיניות נכנסת זו ל-API שלך:

<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 ויוודא כל בקשה.


מה הלאה

כתב ויתור:
מסמך זה תורגם באמצעות שירות תרגום מבוסס בינה מלאכותית Co-op Translator. למרות שאנו שואפים לדיוק, יש לקחת בחשבון כי תרגומים אוטומטיים עלולים להכיל שגיאות או אי-דיוקים. המסמך המקורי בשפתו המקורית הוא המקור הסמכותי. למידע קריטי מומלץ להשתמש בתרגום מקצועי אנושי. אנו לא נושאים באחריות על כל אי-הבנה או פרשנות שגויה הנובעת משימוש בתרגום זה.