呢個項目係一個最簡Spring Boot應用程式,同時擔當:
- Spring授權伺服器(透過
client_credentials流程發行JWT存取令牌),同時 - 資源伺服器(保護佢自己嘅
/hello端點)。
佢反映咗喺Spring blog post (2025年4月2日)入面示範嘅設定。
# 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安全設定:
# This should return 401 Unauthorized, confirming OAuth2 security is active
curl -v http://localhost:8081/# 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注意:Basic Authentication標頭係 (bWNwLWNsaWVudDpzZWNyZXQ=) is the Base64 encoding of mcp-client:secret。
# 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-demoaz containerapp up -n mcp-oauth2 \
-g demo-rg -l westeurope \
--image <your-registry>/mcp-oauth2-demo:latest \
--ingress external --target-port 8081ingress嘅FQDN會變成你嘅issuer (https://<fqdn>).
Azure provides a trusted TLS certificate automatically for *.azurecontainerapps.io。
將呢個入站策略加入你嘅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同驗證每個請求。
免責聲明:
本文件由 AI 翻譯服務 Co-op Translator 進行翻譯。雖然我們致力於確保準確性,但請注意自動翻譯可能存在錯誤或不準確之處。原始文件的母語版本應被視為權威來源。對於重要資訊,建議使用專業人工翻譯。我們不對因使用此翻譯而引致的任何誤解或誤釋承擔責任。