solr_添加用户名密码 r囧r小猫 2022-05-24 03:21 653阅读 0赞 页面直接访问页面:[http://192.168.75.101:8983/solr/][http_192.168.75.101_8983_solr] 就可以在页面中操作各数据,安全性不好,因此希望给solr添加一个密码。 1. 修改web.xml solr-5.5.3/server/solr-webapp/webapp/WEB-INF/web.xml中添加如下内容 <security-constraint> <web-resource-collection> <web-resource-name>Solr Lockdown</web-resource-name> <url-pattern>/</url-pattern> </web-resource-collection> <auth-constraint> <role-name>solr\_admin</role-name> <role-name>admin</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>Solr Admin</realm-name> </login-config> 2. 修改jetty.xml solr-5.5.3/server/etc/jetty.xml中,</Configure>前添加如下内容 <Call name="addBean"> <Arg> <New class="org.eclipse.jetty.security.HashLoginService"> <Set name="name">Solr Admin</Set> <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set> <Set name="refreshInterval">0</Set> </New> </Arg> </Call> 3. 创建realm.properties 最后在solr-5.5.3/server/etc/目录下vim realm.properties,该文件用来存放用户名密码。 例如用户名:admin,密码:12345 格式:【用户名:密码,角色】 ![20180519153145458][] ![20180519153214491][] /\*\*\*\*\*\*\*将密码改为密文\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ 输入命令 java -cp /opt/solr-5.5.3/server/lib/jetty-util-9.2.13.v20150730.jar org.eclipse.jetty.util.security.Password admin 12345把明文密码生成三种格式密文如下: ![20180519153242895][] 比如密码用MD5格式就是这样 admin:MD5:827ccb0eea8a706c4c34a16891f84e7b,admin /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ 4. 重启solr 这些都完成后,重启服务,进入Solr主界面 ![20180519153305641][] 发现需要输入用户名和密码了。 输入admin/12345后成功登录。 5. 代码使用 使用代码时报错: ![20180519153331559][] 需要添加包:commons-codec-1.9.jar 再测试 成功。 需要注意的是:前面的例子中solr没有密码,因此HttpSolrClient 中使用的是第一块代码: @Bean **public** HttpSolrClient httpSolrClient() \{ **if** (Strings.*isNullOrEmpty*(username)) \{ **return** **new** HttpSolrClient(uri); \} **final** URI scopeUri = URI.*create*(uri); **final** BasicCredentialsProvider credentialsProvider = **new** BasicCredentialsProvider(); credentialsProvider.setCredentials(**new** AuthScope(scopeUri.getHost(), scopeUri.getPort()), **new** UsernamePasswordCredentials(username, password)); solrHttpClientBuilder.setMaxConnTotal(128).setMaxConnPerRoute(32).setDefaultRequestConfig(RequestConfig.*copy*(RequestConfig.***DEFAULT***).setRedirectsEnabled(**true**).build()).setDefaultCredentialsProvider(credentialsProvider).addInterceptorFirst(**new** PreemptiveAuthInterceptor()); HttpSolrClient client = **new** HttpSolrClient(uri, solrHttpClientBuilder.build()); **return** client; \} 因此在使用完后不能close,否则后面就不能再用了。 但是有密码后使用的solrHttpClientBuilder,使用完HttpSolrClient 后可以使用其close方法,后面还能继续使用。 [http_192.168.75.101_8983_solr]: http://192.168.75.101:8983/solr/ [20180519153145458]: /images/20220524/5fb65bcf40194e43949173a4e51584e7.png [20180519153214491]: /images/20220524/3d265bb875104d63b53a8ec75001047c.png [20180519153242895]: /images/20220524/342b218b4b7446e6a6a689495ebb7748.png [20180519153305641]: /images/20220524/c4e12626d3214968970159445a076a11.png [20180519153331559]: /images/20220524/140b962015b44beb90713624859c5dd8.png
还没有评论,来说两句吧...