第一部分:Solr配置账户密码

创建配置文件

/data/solr-8.3.0/server/etc
创建realm.properties

1
2
3
4
5
6
7
8
#  
# 这个文件定义用户名,密码和角色
#
# 格式如下
# <username>: <password>[,<rolename> ...]
#
#userName: password,role
admin: passwd1219,admin

安全处理程序设置

/data/solr-8.3.0/server/contexts
修改solr-jetty-context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath"><Property name="hostContext" default="/solr"/></Set>
<Set name="war"><Property name="jetty.base"/>/solr-webapp/webapp</Set>
<Set name="defaultsDescriptor"><Property name="jetty.base"/>/etc/webdefault.xml</Set>
<Set name="extractWAR">false</Set>
<!-- 安全处理程序设置 -->
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">realm</Set> <!-- 一个名字-->
              <!-- 引入刚刚新建的文件 -->
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
</New>
</Set>
</Get>
</Configure>

添加验证拦截
/data/solr-8.3.0/server/solr-webapp/webapp/WEB-INF
编辑web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<security-constraint>    
  <web-resource-collection>
  <web-resource-name>Solr</web-resource-name> <!--描述-->
  <url-pattern>/</url-pattern> <!-- 验证的网页的位置-->
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name> <!-- 验证的角色,别写成用户名,如有多个角色可以写多个role-name 标签-->
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method> <!-- 关键-->
<realm-name>realm</realm-name> <!-- solr-jetty-context.xml中name标签内容-->
</login-config>

第二部分:测试与验证

再次访问已经要输入用户名密码才可以操作了

alt text

如果Java集成solr地址如下:

http://admin:passwd1219@localhost:8983/solr/new_core