solr账号密码配置,例如:
solr默认情况下是没有配置登录账号密码的,在这种情况下存在安全隐患。
例如:可以 从cmd 命令执行curl http:// IP :8983/solr/admin/cores (或者浏览器访问)可以获取core信息,在solr版本低于8.2.0情况下,如果启用了DataImportHandler模块,因为它支持使用web请求来指定配置信息"DIH配置" ,攻击者可根据获取的core信息中name信息构造HTTP请求指定dataConfig参数的值,后端处理的过程中,可导致命令执行。(CVE-2019-0193)
一、solr配置
1.在solr-7.7.2\server\etc下创建role..properties,并配置账号密码,可以设置多个。
#用户名: 密码,角色
user: passw,admin
2.修改solr-7.7.2\server\contexts\solr-jetty-context.xml
<?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">role—name</Set> <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/role.properties</Set> </New> </Set> </Get> <!-- 添加以上代码 --> </Configure>
3.修改solr-7.7.2\server\solr-webapp\webapp\WEB-INF\web.xml
找到以下代码,但切忌修改,否则可能导致solr运行报错。
<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-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>role—name</realm-name> </login-config>
4.修改core(可选,非必要)
将 ’solr-7.7.2\server\solr\旧项目名‘ 改为 ’solr-7.7.2\server\solr\新项目名‘。打开solr-7.7.2\server\solr\新项目名\core.properties,修改name字段。
5.重启solr,打开http:// IP :8983/solr,若提示输入账号密码则配置成功。
二、java调用solr账号密码验证配置
在没有配置账号密码时
String serverUrl = PropertiesUtils.GetString("SOLR_URL");//从properties文件中获取solrUrl地址 HttpSolrClient client = new HttpSolrClient(serverUrl); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "001"); //...... client.add(doc); client.commit(); client.close();
在配置后
String serverUrl = PropertiesUtils.GetString("SOLR_URL");//若修改了core,则需要修改properties中的SOLR_URL //新增 String solruser = PropertiesUtils.GetString("SOLR_USERNAME"); String solrpasw = PropertiesUtils.GetString("SOLR_PASSWORD"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(solruser, solrpasw); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); HttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(provider) .build(); //修改 HttpSolrClient client = new HttpSolrClient(serverUrl, httpClient); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "001"); //...... client.add(doc); client.commit(); client.close();
三、命令行调用solr账号密码验证
在没有配置账号密码时,向solr提交json文件
curl "http://127.0.0.1:8983/solr/yourcore/update?commit=true" --data-binary @/yourpath/yourfile.json -H "Content-type:application/json"
在配置后,将以下username替换为你的账号名
curl -u username "http://127.0.0.1:8983/solr/yourcore/update?commit=true" --data-binary @/yourpath/yourfile.json -H "Content-type:application/json"
输入后将提示输入密码。
或者采取一下方案,但密码将在bash历史记录中显示:
curl -u username:password "http://127.0.0.1:8983/solr/yourcore/update?commit=true" --data-binary @/yourpath/yourfile.json -H "Content-type:application/json"