【人工智能时代】-火爆 AI 编程圈的 MCP 到底是个什么东西?

先从专业角度讲,MCP 就是 Anthropic (Claude) 主导发布的一个开放的、通用的、有共识的协议标准。

Model Context Protocol (MCP)

  • MCP 是一个标准协议,就像给 AI 大模型装了一个 “万能接口”,让 AI 模型能够与不同的数据源和工具进行无缝交互。它就像 USB-C 接口一样,提供了一种标准化的方法,将 AI 模型连接到各种数据源和工具。
  • MCP 旨在替换碎片化的 Agent 代码集成,从而使 AI 系统更可靠,更有效。通过建立通用标准,服务商可以基于协议来推出它们自己服务的 AI 能力,从而支持开发者更快的构建更强大的 AI 应用。开发者也不需要重复造轮子,通过开源项目可以建立强大的 AI Agent 生态。
  • MCP 可以在不同的应用 /
### 实现 Spring AI Starter MCP Server WebFlux 的鉴权功能 为了在基于 `spring-ai-starter-mcp-server-webflux` 开发的 MCP 服务中实现鉴权功能,可以采用以下方法: #### 鉴权机制概述 WebFlux 是一种响应式编程框架,支持异步和非阻塞操作。Spring Security 提供了强大的工具来保护 WebFlux 应用程序的安全性。通过集成 Spring Security 和自定义过滤器,可以在 MCP 服务器中实现身份验证和授权。 以下是具体实现方式: --- #### 1. 添加依赖项 确保项目中的 `pom.xml` 或 `build.gradle` 文件包含必要的依赖项。对于 Maven 用户,添加以下内容: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-resource-server</artifactId> </dependency> ``` 如果使用 Gradle,则添加如下内容: ```gradle implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.security:spring-security-oauth2-resource-server' ``` 这些依赖项允许配置 OAuth2 资源服务器或其他类型的认证方案[^1]。 --- #### 2. 配置安全设置 创建一个类用于配置安全性策略。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; import org.springframework.security.web.server.SecurityWebFilterChain; @Configuration public class SecurityConfig { @Bean public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { return http .authorizeExchange(exchanges -> exchanges .pathMatchers(HttpMethod.GET, "/public/**").permitAll() // 公共路径无需认证 .anyExchange().authenticated()) // 所有其他请求都需要认证 .oauth2ResourceServer(oauth2 -> oauth2 .jwt(jwt -> jwt.decoder(jwtDecoder()).jwtAuthenticationConverter(authenticationConverter()))) .csrf(csrf -> csrf.disable()) .build(); } private ReactiveJwtDecoder jwtDecoder() { // 自定义 JWT 解码逻辑 return NimbusReactiveJwtDecoder.withJwkSetUri("https://your-jwks-endpoint/.well-known/jwks.json") .build(); } private JwtAuthenticationConverter authenticationConverter() { JwtAuthenticationConverter converter = new JwtAuthenticationConverter(); converter.setAuthoritiesExtractor(new CustomAuthorityExtractor()); return converter; } } ``` 在此配置中,设置了 `/public/**` 路径为公共访问路径,而其余 API 请求则需要经过 OAuth2 认证[^1]。 --- #### 3. 创建自定义权限提取器 可以通过扩展 `JwtGrantedAuthoritiesConverter` 来解析令牌并提取角色或权限信息。例如: ```java import org.springframework.security.oauth2.jwt.Jwt; import java.util.List; import java.util.stream.Collectors; public class CustomAuthorityExtractor implements Converter<Jwt, Collection<SimpleGrantedAuthority>> { @Override public Collection<SimpleGrantedAuthority> convert(Jwt jwt) { List<String> roles = (List<String>) jwt.getClaim("roles"); return roles.stream() .map(role -> "ROLE_" + role.toUpperCase()) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); } } ``` 此代码片段会从 JWT 中读取 `roles` 声明,并将其转换为 Spring 安全性的标准角色格式(如 `ROLE_ADMIN`)[^1]。 --- #### 4. 测试与调试 完成以上步骤后,启动应用程序并通过 Postman 或其他工具测试受保护端点的行为。发送带有有效 Bearer Token 的 HTTP 请求时,应能成功调用目标资源;否则返回未授权错误状态码 `401 Unauthorized` 或 `403 Forbidden`。 --- #### 注意事项 - 如果计划部署到生产环境,请务必启用 HTTPS 并妥善管理敏感数据(如密钥存储位置)。 - 对于复杂的业务场景,可能还需要引入额外的功能模块,比如速率限制、日志记录以及异常处理等。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoli8748_软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值