Solr部署:基础认证插件配置与使用指南

Solr部署:基础认证插件配置与使用指南

Apache Solr的基础认证插件(BasicAuthPlugin)提供了基于用户名和密码的简单身份认证机制。本文将详细介绍如何配置和使用基础认证插件,确保Solr服务的安全访问。

基础认证插件概述

基础认证插件只提供用户认证功能。如需控制用户权限,还需要配置授权插件,如基于规则的授权插件。基础认证使用HTTP Basic Authentication标准,支持用户名密码验证。

主要特性

  • 简单易用:基于标准HTTP Basic Authentication
  • 灵活管理:支持动态添加、修改和删除用户
  • API支持:提供RESTful API进行用户管理
  • 集群支持:在SolrCloud环境中统一管理用户
  • 多方案兼容:可与其他认证方案组合使用

启用基础认证

创建security.json文件

要使用基础认证,必须首先创建security.json文件。该文件的详细信息和位置在认证和授权插件配置部分有详细描述。

对于云模式运行,可以使用bin/solr auth命令行工具为新安装启用安全功能:

1
bin/solr auth --help

基本配置示例

以下是一个完整的security.json示例,展示了认证和授权块如何协同工作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"authentication": {
"blockUnknown": true,
"class": "solr.BasicAuthPlugin",
"credentials": {
"solr": "IV0EHq1OnNrj6gvRCwvFwTrZ1+z1oBbnQdiVC3otuq0= Ndd7LKvVBAaZIF0QAVi1ekCfAJXr1GGfLtRUXhgrF8c="
},
"realm": "My Solr users",
"forwardCredentials": false
},
"authorization": {
"class": "solr.RuleBasedAuthorizationPlugin",
"permissions": [
{
"name": "security-edit",
"role": "admin"
}
],
"user-role": {
"solr": "admin"
}
}
}

配置参数详解

认证块参数

参数 说明 默认值
class 指定认证插件类 solr.BasicAuthPlugin
blockUnknown 是否阻止未认证请求 true
credentials 用户凭据(编码格式)
realm 认证域名称 solr
forwardCredentials 是否转发凭据 false

参数详细说明

  1. blockUnknown:设置为true意味着不允许未认证的请求通过
  2. credentials:用户凭据采用base64(sha256(sha256(salt+password))) base64(salt)格式
  3. realm:覆盖realm属性以在登录提示中显示自定义文本
  4. forwardCredentials:设置为false表示让Solr的PKI认证处理分布式请求,而不转发Basic Auth头

部署配置文件

单节点部署

security.json文件放置在$SOLR_HOME目录中。

SolrCloud集群部署

必须将security.json上传到ZooKeeper。示例命令:

1
bin/solr auth enable -type basicAuth -prompt true -z localhost:2181

重要安全注意事项

警告:如果将blockUnknown设置为false,则任何未受权限显式保护的请求都将对匿名用户可访问!因此,您应该为要保护的每个预定义权限定义角色绑定。可以为希望允许匿名用户访问的请求分配特殊的role: null绑定。要保护除role:null之外的所有端点,可以为all权限添加角色绑定并将其放在security.json的最后位置。

安全最佳实践

  1. 使用SSL:凭据默认以明文发送,建议启用SSL通信
  2. 权限控制:对security.json写入权限的用户能够修改所有权限,应谨慎授权
  3. 网络安全:即使启用了基础认证,也不应不必要地将Solr暴露给外部世界

多认证方案组合

MultiAuthPlugin概述

当使用其他认证方案(如JWT认证插件)时,您可能仍希望为少量”服务账户”导向的客户端应用程序使用基础认证。Solr提供MultiAuthPlugin来支持多种认证方案。

使用场景

  • OIDC用户认证:将Solr与OIDC提供商集成处理用户账户
  • 基础认证服务账户:为Prometheus指标导出器等服务使用基础认证
  • Kubernetes环境:为存活性和就绪性端点使用基础认证,避免Kubernetes使用OIDC

MultiAuthPlugin配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"authentication": {
"class": "solr.MultiAuthPlugin",
"schemes": [
{
"scheme": "bearer",
"blockUnknown": true,
"class": "solr.JWTAuthPlugin",
"wellKnownUrl": "https://OIDC_PROVIDER_URL/.well-known/openid-configuration",
"clientId": "solr",
"redirectUris": "http://localhost:8983/solr/",
"rolesClaim": "groups"
},
{
"scheme": "basic",
"blockUnknown": true,
"class": "solr.BasicAuthPlugin",
"credentials": {
"k8s-oper": "PASSWORD_SALT_AND_HASH"
},
"forwardCredentials": false
}
]
}
}

MultiAuthPlugin工作机制

  • MultiAuthPlugin使用Authorization头的scheme来确定哪个插件应该处理每个请求
  • 对于来自Solr管理界面的未认证AJAX请求,MultiAuthPlugin将请求转发给schemes列表中的第一个插件
  • 在上述示例中,用户需要向OIDC提供商认证才能登录管理界面

编辑基础认证配置

认证API概述

认证API允许修改用户ID和密码。该API提供特定命令的端点来设置用户详细信息或删除用户。

API入口点

  • V1 APIhttp://localhost:8983/solr/admin/authentication
  • V2 APIhttp://localhost:8983/api/cluster/security/authentication

此端点不特定于集合,因此为整个Solr集群创建用户。如果需要将用户限制到特定集合,可以通过授权规则完成。

用户管理操作

添加用户或编辑密码

set-user命令允许您添加用户和更改密码:

V1 API示例:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/solr/admin/authentication \
-H 'Content-type:application/json' \
-d '{"set-user": {"tom":"TomIsCool", "harry":"HarrysSecret"}}'

V2 API示例:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/api/cluster/security/authentication \
-H 'Content-type:application/json' \
-d '{"set-user": {"tom":"TomIsCool", "harry":"HarrysSecret"}}'

删除用户

delete-user命令允许删除用户,不需要发送用户密码:

V1 API示例:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/solr/admin/authentication \
-H 'Content-type:application/json' \
-d '{"delete-user": ["tom", "harry"]}'

V2 API示例:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/api/cluster/security/authentication \
-H 'Content-type:application/json' \
-d '{"delete-user": ["tom", "harry"]}'

设置属性

为认证插件设置属性。基础认证插件当前支持的属性包括blockUnknownrealmforwardCredentials

设置blockUnknown属性:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/solr/admin/authentication \
-H 'Content-type:application/json' \
-d '{"set-property": {"blockUnknown":false}}'

设置认证域:

1
2
3
curl --user solr:SolrRocks http://localhost:8983/solr/admin/authentication \
-H 'Content-type:application/json' \
-d '{"set-property": {"realm":"My Solr users"}}'

认证域默认为solr,显示在WWW-Authenticate HTTP头和管理界面登录页面中。

使用MultiAuthPlugin编辑配置

使用MultiAuthPlugin时,需要用标识scheme的单键对象包装命令数据:

设置用户命令:

1
2
3
4
5
{
"set-user": {
"basic": {"tom":"TomIsCool", "harry":"HarrysSecret"}
}
}

设置属性命令:

1
2
3
4
5
{
"set-property": {
"basic": {"realm":"My Solr users"}
}
}

SolrJ客户端集成

集成方案概述

在SolrJ中使用基础认证保护的Solr服务器有两种主要方法:

  1. 每请求认证:在每个单独请求上设置权限
  2. 客户端级认证:配置底层HTTP客户端为所有请求添加凭据

每请求基础认证凭据

最简单的方法是在每个请求上使用setBasicAuthCredentials方法:

1
2
3
4
5
6
7
8
9
// 通用请求示例
SolrRequest req; // 创建新请求对象
req.setBasicAuthCredentials(userName, password);
solrClient.request(req);

// 查询请求示例
QueryRequest req = new QueryRequest(new SolrQuery("*:*"));
req.setBasicAuthCredentials(userName, password);
QueryResponse rsp = req.process(solrClient);

方法限制

  • 需要确保在所有需要的地方都提供凭据,可能不够便利
  • 不适用于许多不接受SolrRequest对象的SolrClient方法

客户端级凭据配置

Http2SolrClient配置

Http2SolrClient支持在构建时在客户端级别设置凭据:

1
2
3
4
Http2SolrClient client = new Http2SolrClient.Builder(solrUrl)
.withBasicAuthCredentials(userName, password)
.build();
QueryResponse rsp = req.process(client);

CloudHttp2SolrClient配置

CloudHttp2SolrClient支持接收Http2SolrClient.Builder实例来创建其内部客户端:

1
2
3
4
5
6
7
8
Http2SolrClient.Builder http2ClientBuilder = Http2SolrClient.Builder()
.withBasicAuthCredentials(userName, password);

CloudHttp2SolrClient client = new CloudHttp2SolrClient.Builder(zkHostList, chroot)
.withInternalClientBuilder(http2ClientBuilder)
.build();

QueryResponse rsp = req.process(client);

全局(JVM)基础认证凭据

PreemptiveBasicAuthClientBuilderFactory

用户可以使用SolrJ的PreemptiveBasicAuthClientBuilderFactory自动为所有请求添加基础认证凭据。

启用此功能需要设置系统属性:

1
-Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory

凭据提供方式

方式1:直接通过系统属性提供

1
-Dbasicauth=username:password

此选项简单直接,但可能在命令行中暴露凭据。

方式2:通过属性文件提供

1
-Dsolr.httpclient.config=/path/to/credentials.properties

属性文件内容:

1
2
httpBasicAuthUser=my_username
httpBasicAuthPassword=secretPassword

Solr控制脚本集成

配置控制脚本认证

启用基础认证后,所有对Solr控制脚本(bin/solr)的请求都必须包含用户凭据。

配置方法

solr.in.shsolr.in.cmd文件中添加以下行:

1
2
SOLR_AUTH_TYPE="basic"
SOLR_AUTHENTICATION_OPTS="-Dbasicauth=solr:SolrRocks"

使用配置文件

也可以使用配置文件路径:

1
2
SOLR_AUTH_TYPE="basic"
SOLR_AUTHENTICATION_OPTS="-Dsolr.httpclient.config=/path/to/solr-9.x.x/server/solr/basicAuth.conf"

配置文件内容:

1
2
httpBasicAuthUser=solr
httpBasicAuthPassword=SolrRocks

常用命令示例

配置认证后,常用控制脚本命令示例:

1
2
3
4
5
6
7
8
9
10
11
# 创建集合
bin/solr create -c mycollection

# 删除集合
bin/solr delete -c mycollection

# 上传配置集
bin/solr zk upconfig -n myconfig -d /path/to/config

# 查看集群状态
bin/solr status

生产环境配置最佳实践

1. 密码安全管理

强密码策略

1
2
3
4
5
6
7
8
9
10
{
"authentication": {
"class": "solr.BasicAuthPlugin",
"credentials": {
"admin": "STRONG_ENCODED_PASSWORD_WITH_SALT",
"readonly": "ANOTHER_STRONG_ENCODED_PASSWORD_WITH_SALT"
},
"blockUnknown": true
}
}

密码轮换

1
2
3
4
# 定期更新密码
curl --user admin:current_password http://localhost:8983/solr/admin/authentication \
-H 'Content-type:application/json' \
-d '{"set-user": {"admin":"new_strong_password"}}'

2. 用户角色管理

角色分层设计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"authentication": {
"class": "solr.BasicAuthPlugin",
"credentials": {
"admin": "ADMIN_PASSWORD_HASH",
"developer": "DEV_PASSWORD_HASH",
"readonly": "READONLY_PASSWORD_HASH",
"service": "SERVICE_PASSWORD_HASH"
}
},
"authorization": {
"class": "solr.RuleBasedAuthorizationPlugin",
"user-role": {
"admin": ["admin", "developer", "readonly"],
"developer": ["developer", "readonly"],
"readonly": ["readonly"],
"service": ["service"]
},
"permissions": [
{"name": "security-edit", "role": "admin"},
{"name": "collection-admin-edit", "role": ["admin", "developer"]},
{"name": "read", "role": ["admin", "developer", "readonly", "service"]},
{"name": "update", "role": ["admin", "developer"]}
]
}
}

3. SSL/TLS集成

强制HTTPS配置

1
2
3
4
5
6
7
8
# solr.in.sh
SOLR_SSL_ENABLED=true
SOLR_SSL_KEY_STORE=etc/solr-ssl.keystore.p12
SOLR_SSL_KEY_STORE_PASSWORD=secret
SOLR_SSL_TRUST_STORE=etc/solr-ssl.keystore.p12
SOLR_SSL_TRUST_STORE_PASSWORD=secret
SOLR_AUTH_TYPE="basic"
SOLR_AUTHENTICATION_OPTS="-Dbasicauth=admin:SecurePassword"

客户端SSL配置

1
2
3
4
5
6
7
// SolrJ客户端SSL配置
System.setProperty("javax.net.ssl.trustStore", "/path/to/truststore.p12");
System.setProperty("javax.net.ssl.trustStorePassword", "password");

Http2SolrClient client = new Http2SolrClient.Builder("https://localhost:8983/solr")
.withBasicAuthCredentials("admin", "SecurePassword")
.build();

4. 监控和审计

认证失败监控

1
2
# 监控认证失败日志
tail -f /var/solr/logs/solr.log | grep "Authentication failed"

用户活动审计

1
2
3
4
5
6
7
8
9
10
11
12
{
"authentication": {
"class": "solr.BasicAuthPlugin",
"blockUnknown": true
},
"authorization": {
"class": "solr.RuleBasedAuthorizationPlugin"
},
"auditlogging": {
"class": "solr.SolrLogAuditLoggerPlugin"
}
}

5. 高可用配置

多节点认证同步

1
2
# 在SolrCloud集群中,security.json自动同步
bin/solr auth enable -type basicAuth -credentials admin:AdminPassword -z zk1:2181,zk2:2181,zk3:2181

故障转移处理

1
2
3
4
5
6
7
// 客户端故障转移配置
List<String> zkHosts = Arrays.asList("zk1:2181", "zk2:2181", "zk3:2181");
CloudHttp2SolrClient client = new CloudHttp2SolrClient.Builder(zkHosts, Optional.empty())
.withInternalClientBuilder(
Http2SolrClient.Builder().withBasicAuthCredentials("admin", "password")
)
.build();

故障排除指南

常见问题和解决方案

1. 认证失败问题

问题症状:HTTP 401 Unauthorized错误

解决步骤

1
2
3
4
5
6
# 检查用户凭据
curl -u admin:password http://localhost:8983/solr/admin/info/system

# 验证security.json配置
bin/solr auth disable
bin/solr auth enable -type basicAuth -prompt true

2. 配置文件问题

问题症状:Solr启动失败或认证不工作

解决步骤

1
2
3
4
5
# 验证security.json语法
python -m json.tool security.json

# 检查ZooKeeper中的配置
bin/solr zk ls /security.json -z localhost:2181

3. 客户端连接问题

问题症状:SolrJ客户端无法连接

解决步骤

1
2
3
4
5
// 启用详细日志
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");

4. 性能问题

问题症状:认证后响应变慢

优化建议

1
2
3
4
5
6
# 调整JVM参数
SOLR_OPTS="$SOLR_OPTS -Dsolr.jetty.request.header.size=65536"

# 启用连接池
-Dsolr.httpclient.retries=1
-Dsolr.httpclient.connTimeout=10000

总结

基础认证插件为Apache Solr提供了简单而有效的身份认证机制。通过本指南,您应该能够:

  1. 正确配置基础认证插件和security.json文件
  2. 灵活管理用户和权限,包括动态添加、修改和删除用户
  3. 集成客户端,包括SolrJ和控制脚本的认证配置
  4. 组合使用多种认证方案满足复杂的安全需求
  5. 遵循最佳实践确保生产环境的安全性

记住,基础认证只是Solr安全体系的一部分。在生产环境中,还应该结合SSL加密、权限控制、网络安全和监控审计等措施,构建完整的安全防护体系。

© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero