OAuth2安全认证服务

一个基于MCP规范的OAuth2安全认证服务,支持客户端凭证和授权码流程,适用于需要安全认证的MCP服务器。

  • by akshay12390
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 akshay12390/mcp-weather-starter-webmvc-oauth2-server

使用说明

MCP与OAuth

此示例演示了如何使用OAuth2保护MCP服务器,如下所示 这 MCP规范.

入门

使用以下方式运行项目:

./mvnw spring-boot:run

客户端凭据

通过调用获得令牌 /oauth2/token 端点:

curl -XPOST "http://localhost:8080/oauth2/token" \
  --data grant_type=client_credentials \
  --user "mcp-client:secret"
# And copy-paste the access token
# Or use JQ:
curl -XPOST "http://localhost:8080/oauth2/token" \
  --data grant_type=client_credentials \
  --user "mcp-client:secret" | jq -r ".access_token"

使用浏览器流进行身份验证代码(PKCE)

通过首先调用authorize端点获取身份验证码

python3 pkce_generator.py

然后通过调用 /oauth2/token 端点

python3 token_request.py

使用MCP检查器

存储上一步生成的令牌,然后启动MCP检查器:

npx @modelcontextprotocol/inspector@0.6.0

在MCP检查器中,粘贴您的令牌。点击连接,瞧!

请注意,令牌的有效期为15分钟

实施注意事项

依赖项

在Spring中,MCP服务器的OAuth2支持意味着添加:

  1. Spring Security (安全基础设施)
  2. Spring授权服务器 (发行代币)
  3. Spring安全:OAuth2资源服务器 ( 使用令牌进行身份验证)

请注意,Spring Auth Server不支持反应式堆栈,因此仅在Servlet中发出令牌。