1. solr-webapp中的web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>solr</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>Test Realm</realm-name>
</login-config>
2. solr-jetty-context.xml
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService>
<Set name="name">Test Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
</New>
</Set>
</Get>
注意此处realm.properties的位置是相对于jetty.home
3. realm.properties
username: password,role
注意password需要配置成MD5
例如:username=admin, password=12345
admin: MD5:827ccb0eea8a706c4c34a16891f84e7b, admin
生成上述MD5: java -cp jetty-util-9.2.13.v20150730.jar org.eclipse.jetty.util.security.Password admin 12345
修改或者添加密码后需要重启服务
4. Solrj访问时进行相关权限设置。参见Solr5部分
http://stackoverflow.com/questions/2014700/preemptive-basic-authentication-with-apache-httpclient-4/11868040#11868040
5. 自动更新进行权限设置,参考http://www.cnblogs.com/donganwangshi/p/4276015.html
在sendHttpPost(StringcoreName,Stringparams)中添加
String headerKey = "Authorization";
String encoding = username+":"+password;
String headerValue = "Basic " + new BASE64Encoder().encode(encoding.getBytes());
conn.setRequestProperty(headerKey, headerValue);
其中username和password是从配置文件dataimport.properties中读取的