solr8.4配置用户登录验证
一、增加用户名称密码配置
进入目录:solr-8.4/server/etc目录下,增加user.properties文件。这个文件一会要配置到solr-jetty-context.xml文件中。
user.properties文件增加如下内容:
# 用户名:密码,权限 admin: admin123.,admin user: user123.,admin
二、配置solr-jetty-context.xml文件
在这个solr-8.4\server\contexts目录下只有一个文件,就是它了。打开文件,增加如下内容:
<!-- 添加配置权限认证:在文件configure中添加获取用户文件的配置,内容如下: -->
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<!-- name与web.xml中realm-name一致即可 -->
<Set name="name">user-name</Set>
<!-- user.properties与上面新建的文件保持同名即可 -->
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/user.properties</Set>
</New>
</Set>
</Get>
添加后的整个文件内容如下:
<?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>
<!-- 添加配置权限认证:在文件configure中添加获取用户文件的配置,内容如下: -->
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<!-- name与web.xml中realm-name一致即可 -->
<Set name="name">user-name</Set>
<!-- user.properties与上面新建的文件保持同名即可 -->
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/user.properties</Set>
</New>
</Set>
</Get>
</Configure>
三、配置web.xml文件
在这个solr-8.4\server\solr-webapp\webapps\WEB-INF\目录下的web.xml文件。
找到security-constraint配置。内容如下:
<!-- Get rid of error message --> <security-constraint> <web-resource-collection> <web-resource-name>Disable TRACE</web-resource-name> <url-pattern>/</url-pattern> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint/> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>Enable everything but TRACE</web-resource-name> <url-pattern>/</url-pattern> <http-method-omission>TRACE</http-method-omission> </web-resource-collection> </security-constraint>
在这些内容后面新增加一些内容,原有内容不能删除,否则会导致登录的配置无效。增加的内容如下:
<!--重新配置 security-resource-collection (删除之前的security-constraint,会导致登录的配置无效)-->
<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>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>user-name</realm-name>
</login-config>
到此就配置完成了。接下来启动solr,即可生效。启动命令如下solr-8.4/bin/:
./solr start -force
四、项目调用修改配置
登录权限增加完之后,代码调用的url要做相应改变。增加用户名密码
# http://user:pass@localhost:8088/solr/ http://admin:admin123.@localhost:8088/solr/