Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 4.15 KB

File metadata and controls

128 lines (91 loc) · 4.15 KB

MCP OAuth2 Demo

Dự án này là một ứng dụng Spring Boot tối giản hoạt động đồng thời như:

  • một Spring Authorization Server (cấp token truy cập JWT qua client_credentials flow), và
  • một Resource Server (bảo vệ chính endpoint /hello của nó).

Nó phản ánh cấu hình được trình bày trong bài blog Spring (2 Apr 2025).


Bắt đầu nhanh (local)

# 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

Kiểm tra cấu hình OAuth2

Bạn có thể kiểm tra cấu hình bảo mật OAuth2 bằng các bước sau:

1. Xác nhận server đang chạy và đã được bảo vệ

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

2. Lấy token truy cập bằng 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

Lưu ý: Header Basic Authentication (bWNwLWNsaWVudDpzZWNyZXQ=) is the Base64 encoding of mcp-client:secret.

3. Truy cập endpoint được bảo vệ bằng 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

Phản hồi thành công với "Hello from MCP OAuth2 Demo!" xác nhận cấu hình OAuth2 đang hoạt động đúng.


Xây dựng container

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

Triển khai lên 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 sẽ trở thành issuer của bạn (https://<fqdn>).
Azure provides a trusted TLS certificate automatically for *.azurecontainerapps.io.


Kết nối với Azure API Management

Thêm chính sách inbound này vào API của bạn:

<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 sẽ lấy JWKS và xác thực mọi yêu cầu.


Tiếp theo là gì

Tuyên bố từ chối trách nhiệm:
Tài liệu này đã được dịch bằng dịch vụ dịch thuật AI Co-op Translator. Mặc dù chúng tôi cố gắng đảm bảo độ chính xác, xin lưu ý rằng các bản dịch tự động có thể chứa lỗi hoặc không chính xác. Tài liệu gốc bằng ngôn ngữ gốc nên được xem là nguồn tham khảo chính xác nhất. Đối với thông tin quan trọng, nên sử dụng dịch vụ dịch thuật chuyên nghiệp do con người thực hiện. Chúng tôi không chịu trách nhiệm về bất kỳ sự hiểu lầm hoặc diễn giải sai nào phát sinh từ việc sử dụng bản dịch này.