安全配置 #
安全概述 #
Algolia安全主要包括:
- API密钥管理
- 访问控制
- 数据安全
- 网络安全
API密钥管理 #
密钥类型 #
| 密钥类型 | 权限 | 使用场景 |
|---|---|---|
| Admin API Key | 完全权限 | 服务端管理 |
| Search-Only Key | 只读搜索 | 前端搜索 |
| Write Key | 写入权限 | 数据同步 |
| Secured API Key | 受限权限 | 用户特定访问 |
密钥使用原则 #
javascript
// ❌ 错误:前端使用Admin Key
const client = algoliasearch('APP_ID', 'ADMIN_KEY');
// ✅ 正确:前端使用Search-Only Key
const client = algoliasearch('APP_ID', 'SEARCH_ONLY_KEY');
生成安全密钥 #
javascript
// 服务端生成受限密钥
const securedKey = client.generateSecuredApiKey('SEARCH_ONLY_KEY', {
filters: 'user_id:12345', // 数据过滤
validUntil: Math.floor(Date.now() / 1000) + 3600, // 有效期
restrictIndices: ['products'], // 限制索引
restrictSources: ['192.168.1.1'] // 限制IP
});
访问控制 #
基于用户的数据隔离 #
javascript
// 生成用户专属密钥
function generateUserKey(userId) {
return client.generateSecuredApiKey('SEARCH_ONLY_KEY', {
filters: `user_id:${userId}`,
userToken: userId
});
}
// 用户只能搜索自己的数据
基于角色的访问控制 #
javascript
function generateRoleKey(role) {
const filters = {
admin: '',
manager: 'department:sales',
user: 'status:published AND access:public'
};
return client.generateSecuredApiKey('SEARCH_ONLY_KEY', {
filters: filters[role] || 'access:public'
});
}
时间限制 #
javascript
// 1小时有效的密钥
const tempKey = client.generateSecuredApiKey('SEARCH_ONLY_KEY', {
validUntil: Math.floor(Date.now() / 1000) + 3600
});
IP限制 #
javascript
// 限制特定IP访问
const ipRestrictedKey = client.generateSecuredApiKey('SEARCH_ONLY_KEY', {
restrictSources: ['192.168.1.0/24']
});
密钥权限配置 #
创建自定义密钥 #
javascript
await client.addApiKey({
acl: ['search', 'browse'], // 权限列表
indices: ['products'], // 可访问索引
validity: 3600, // 有效期(秒)
maxQueriesPerIPPerHour: 1000, // 每IP每小时请求限制
maxHitsPerQuery: 1000 // 每次查询最大结果数
});
ACL权限列表 #
| 权限 | 说明 |
|---|---|
| search | 搜索权限 |
| browse | 浏览权限 |
| addObject | 添加记录 |
| deleteObject | 删除记录 |
| deleteIndex | 删除索引 |
| settings | 修改设置 |
| editSettings | 编辑设置 |
| listIndexes | 列出索引 |
数据安全 #
隐藏敏感字段 #
javascript
await index.setSettings({
unretrievableAttributes: [
'internal_id',
'cost_price',
'supplier_info',
'user_email'
]
});
// 这些字段不会被返回
敏感数据不索引 #
javascript
// ❌ 不要索引敏感数据
{
"name": "Product",
"user_email": "user@example.com", // 敏感
"credit_card": "1234-5678-9012" // 敏感
}
// ✅ 只索引必要数据
{
"name": "Product",
"user_id": "user-123" // 使用ID替代
}
网络安全 #
HTTPS强制 #
javascript
// Algolia默认使用HTTPS
// 确保所有请求都通过HTTPS
CORS配置 #
javascript
// 前端配置
const client = algoliasearch('APP_ID', 'SEARCH_KEY', {
requestOptions: {
headers: {
'X-Algolia-Application-Id': 'APP_ID',
'X-Algolia-API-Key': 'SEARCH_KEY'
}
}
});
安全最佳实践 #
1. 密钥轮换 #
javascript
// 定期轮换密钥
async function rotateKeys() {
// 1. 创建新密钥
const newKey = await client.addApiKey({
acl: ['search'],
validity: 30 * 24 * 60 * 60 // 30天
});
// 2. 更新应用配置
updateAppConfig(newKey);
// 3. 删除旧密钥
await client.deleteApiKey(oldKeyId);
}
2. 监控密钥使用 #
javascript
// 监控异常使用
async function monitorKeyUsage() {
const logs = await client.getLogs({
length: 100
});
// 检测异常请求
const suspicious = logs.logs.filter(log =>
log.answer_code >= 400 ||
log.query_params.includes('unexpected')
);
if (suspicious.length > 0) {
sendAlert('检测到异常API使用');
}
}
3. 限制请求频率 #
javascript
await client.addApiKey({
acl: ['search'],
maxQueriesPerIPPerHour: 1000, // 每IP每小时1000次
maxHitsPerQuery: 100 // 每次最多100条结果
});
4. 使用环境变量 #
javascript
// ❌ 不要硬编码密钥
const client = algoliasearch('APP_ID', 'ADMIN_KEY');
// ✅ 使用环境变量
const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
);
安全检查清单 #
- [ ] Admin Key仅用于服务端
- [ ] 前端使用Search-Only Key
- [ ] 敏感数据设置unretrievableAttributes
- [ ] 使用Secured API Key限制访问
- [ ] 设置密钥有效期
- [ ] 实现请求频率限制
- [ ] 定期轮换密钥
- [ ] 监控异常API使用
总结 #
安全配置要点:
| 要点 | 说明 |
|---|---|
| 密钥管理 | Admin Key服务端,Search Key前端 |
| 访问控制 | Secured API Key限制访问 |
| 数据安全 | unretrievableAttributes隐藏敏感字段 |
| 网络安全 | HTTPS、CORS配置 |
| 最佳实践 | 密钥轮换、监控、限频 |
接下来,让我们学习 错误处理。
最后更新:2026-03-28