|
| 1 | +# HTTP Client Authentication Usage Guide |
| 2 | + |
| 3 | +本文档演示了 fit-framework HTTP 客户端代理系统中各种身份认证方式的使用方法。 |
| 4 | + |
| 5 | +## 1. 概述 |
| 6 | + |
| 7 | +`@RequestAuth` 注解提供了统一的身份认证解决方案,支持多种认证类型和应用级别: |
| 8 | + |
| 9 | +### 认证类型 (AuthType) |
| 10 | +- **BEARER**: Bearer Token 认证 |
| 11 | +- **BASIC**: HTTP Basic 认证 |
| 12 | +- **API_KEY**: API Key 认证(支持 Header、Query、Cookie) |
| 13 | +- **CUSTOM**: 自定义认证(通过 Provider) |
| 14 | + |
| 15 | +### 应用级别 |
| 16 | +- **接口级别**: 应用于整个接口的所有方法 |
| 17 | +- **方法级别**: 应用于特定方法(会覆盖接口级别) |
| 18 | +- **参数级别**: 通过方法参数动态设置(最高优先级) |
| 19 | + |
| 20 | +## 2. 静态认证配置 |
| 21 | + |
| 22 | +### 2.1 Bearer Token 认证 |
| 23 | + |
| 24 | +```java |
| 25 | +// 接口级别静态配置 |
| 26 | +@RequestAuth(type = AuthType.BEARER, value = "your-static-token") |
| 27 | +public interface YourClient { |
| 28 | + |
| 29 | + // 方法级别覆盖 |
| 30 | + @RequestAuth(type = AuthType.BEARER, value = "method-specific-token") |
| 31 | + String someMethod(); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### 2.2 Basic 认证 |
| 36 | + |
| 37 | +```java |
| 38 | +@RequestAuth(type = AuthType.BASIC, username = "admin", password = "secret") |
| 39 | +String basicAuthMethod(); |
| 40 | +``` |
| 41 | + |
| 42 | +### 2.3 API Key 认证 |
| 43 | + |
| 44 | +```java |
| 45 | +// Header 中的 API Key |
| 46 | +@RequestAuth(type = AuthType.API_KEY, name = "X-API-Key", value = "your-api-key") |
| 47 | +String headerApiKeyMethod(); |
| 48 | + |
| 49 | +// Query 参数中的 API Key |
| 50 | +@RequestAuth(type = AuthType.API_KEY, name = "api_key", value = "your-key", location = Source.QUERY) |
| 51 | +String queryApiKeyMethod(); |
| 52 | +``` |
| 53 | + |
| 54 | +## 3. 动态认证配置 |
| 55 | + |
| 56 | +### 3.1 参数驱动的认证 |
| 57 | + |
| 58 | +```java |
| 59 | +// 动态 Bearer Token |
| 60 | +String dynamicBearer(@RequestAuth(type = AuthType.BEARER) String token); |
| 61 | + |
| 62 | +// 动态 API Key |
| 63 | +String dynamicApiKey(@RequestAuth(type = AuthType.API_KEY, name = "X-Dynamic-Key") String apiKey); |
| 64 | +``` |
| 65 | + |
| 66 | +### 3.2 Provider 模式 |
| 67 | + |
| 68 | +#### 创建 Provider |
| 69 | + |
| 70 | +```java |
| 71 | +@Component |
| 72 | +public class DynamicTokenProvider implements AuthProvider { |
| 73 | + @Override |
| 74 | + public Authorization provide() { |
| 75 | + // 从 TokenManager、缓存或其他来源获取 token |
| 76 | + String token = TokenManager.getCurrentToken(); |
| 77 | + return Authorization.createBearer(token); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +#### 使用 Provider |
| 83 | + |
| 84 | +```java |
| 85 | +@RequestAuth(type = AuthType.BEARER, provider = DynamicTokenProvider.class) |
| 86 | +String providerBasedMethod(); |
| 87 | +``` |
| 88 | + |
| 89 | +## 4. 组合认证 |
| 90 | + |
| 91 | +可以在不同级别同时应用多种认证: |
| 92 | + |
| 93 | +```java |
| 94 | +@HttpProxy |
| 95 | +@RequestAddress(protocol = "http", host = "localhost", port = "8080") |
| 96 | +// 接口级别:默认 API Key |
| 97 | +@RequestAuth(type = AuthType.API_KEY, name = "X-Service-Key", value = "service-key") |
| 98 | +public interface CombinedAuthClient { |
| 99 | + |
| 100 | + // 方法级别:添加 Bearer Token(会与接口级别的 API Key 共存) |
| 101 | + @RequestAuth(type = AuthType.BEARER, provider = TokenProvider.class) |
| 102 | + String combinedAuth( |
| 103 | + // 参数级别:用户上下文 API Key |
| 104 | + @RequestAuth(type = AuthType.API_KEY, name = "X-User-Context") String userToken |
| 105 | + ); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## 5. 完整示例 |
| 110 | + |
| 111 | +### TestAuthClient 接口 |
| 112 | + |
| 113 | +```java |
| 114 | +@HttpProxy |
| 115 | +@RequestAddress(protocol = "http", host = "localhost", port = "8080") |
| 116 | +@RequestMapping(path = "/http-server/auth") |
| 117 | +@RequestAuth(type = AuthType.API_KEY, name = "X-Service-Key", value = "service-default-key") |
| 118 | +public interface TestAuthClient { |
| 119 | + |
| 120 | + // 1. 静态 Bearer Token |
| 121 | + @GetMapping(path = "/bearer-static") |
| 122 | + @RequestAuth(type = AuthType.BEARER, value = "static-bearer-token-12345") |
| 123 | + String testBearerStatic(); |
| 124 | + |
| 125 | + // 2. 动态 Bearer Token |
| 126 | + @GetMapping(path = "/bearer-dynamic") |
| 127 | + String testBearerDynamic(@RequestAuth(type = AuthType.BEARER) String token); |
| 128 | + |
| 129 | + // 3. Basic 认证 |
| 130 | + @GetMapping(path = "/basic-static") |
| 131 | + @RequestAuth(type = AuthType.BASIC, username = "admin", password = "secret123") |
| 132 | + String testBasicStatic(); |
| 133 | + |
| 134 | + // 4. Header API Key |
| 135 | + @GetMapping(path = "/apikey-header-static") |
| 136 | + @RequestAuth(type = AuthType.API_KEY, name = "X-API-Key", value = "static-api-key-67890") |
| 137 | + String testApiKeyHeaderStatic(); |
| 138 | + |
| 139 | + // 5. Query API Key |
| 140 | + @GetMapping(path = "/apikey-query-static") |
| 141 | + @RequestAuth(type = AuthType.API_KEY, name = "api_key", value = "query-api-key-111", location = Source.QUERY) |
| 142 | + String testApiKeyQueryStatic(); |
| 143 | + |
| 144 | + // 6. 动态 API Key |
| 145 | + @GetMapping(path = "/apikey-dynamic") |
| 146 | + String testApiKeyDynamic(@RequestAuth(type = AuthType.API_KEY, name = "X-Dynamic-Key") String apiKey); |
| 147 | + |
| 148 | + // 7. Provider 模式 |
| 149 | + @GetMapping(path = "/dynamic-provider") |
| 150 | + @RequestAuth(type = AuthType.BEARER, provider = DynamicTokenProvider.class) |
| 151 | + String testDynamicProvider(); |
| 152 | + |
| 153 | + // 8. 自定义认证 |
| 154 | + @GetMapping(path = "/custom-provider") |
| 155 | + @RequestAuth(type = AuthType.CUSTOM, provider = CustomSignatureProvider.class) |
| 156 | + String testCustomProvider(); |
| 157 | + |
| 158 | + // 9. 组合认证 |
| 159 | + @GetMapping(path = "/combined-auth") |
| 160 | + @RequestAuth(type = AuthType.BEARER, provider = DynamicTokenProvider.class) |
| 161 | + String testCombinedAuth(@RequestAuth(type = AuthType.API_KEY, name = "X-User-Context") String userToken); |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +## 6. 注意事项 |
| 166 | + |
| 167 | +1. **优先级**: 参数级别 > 方法级别 > 接口级别 |
| 168 | +2. **Provider**: 需要标记为 `@Component` 并在容器中可用 |
| 169 | +3. **组合认证**: 不同级别的认证会叠加,相同级别的认证会覆盖 |
| 170 | +4. **安全性**: 避免在代码中硬编码敏感信息,优先使用 Provider 模式 |
| 171 | + |
| 172 | +## 7. 快速启动和测试 |
| 173 | + |
| 174 | +### 启动应用 |
| 175 | + |
| 176 | +本示例基于 FIT 框架,启动方式如下: |
| 177 | + |
| 178 | +```bash |
| 179 | +# 1. 编译整个项目(在 fit-framework 根目录) |
| 180 | +mvn clean install |
| 181 | + |
| 182 | +# 2. 启动服务器端 |
| 183 | +# 方式一:在 IDEA 中运行 plugin-http-server 模块的 main 方法 |
| 184 | +# 方式二:命令行运行 JAR 文件(编译后在 target 目录) |
| 185 | +java -jar plugin-http-server/target/plugin-http-server-*.jar |
| 186 | +``` |
| 187 | + |
| 188 | +### 验证启动成功 |
| 189 | + |
| 190 | +查看日志中是否包含以下信息: |
| 191 | + |
| 192 | +``` |
| 193 | +[INFO] [main] [modelengine.fitframework.runtime.aggregated.AggregatedFitRuntime] FIT application started. |
| 194 | +[INFO] [netty-http-server-thread-0] [modelengine.fit.http.server.netty.NettyHttpClassicServer] Start netty http server successfully. [httpPort=8080] |
| 195 | +``` |
| 196 | + |
| 197 | +### 快速测试 |
| 198 | + |
| 199 | +```bash |
| 200 | +# 测试基本连接 |
| 201 | +curl http://localhost:8080/http-server/auth/bearer-static \ |
| 202 | + -H "Authorization: Bearer static-bearer-token-12345" \ |
| 203 | + -H "X-Service-Key: service-default-key" |
| 204 | + |
| 205 | +# 期望响应:Bearer Static Auth: Bearer static-bearer-token-12345 |
| 206 | +``` |
| 207 | + |
| 208 | +## 8. 下一步 |
| 209 | + |
| 210 | +- 查看 [CURL_TEST_EXAMPLES.md](./CURL_TEST_EXAMPLES.md) 了解如何测试这些认证场景 |
| 211 | +- 查看 [run_tests.sh](./run_tests.sh) 了解如何批量执行测试 |
0 commit comments