solr 配置用户名密码以及代码账号密码连接

1. 安装

centos solr8集群搭建

2. 配置用户、密码、权限

1. 新建账号密码文件

1. 打开路径`/disk/solr/solr-8.8.1/server/etc`,此处是我的安装路径。

新建账号密码文件

2. 创建`realm.properties`文件 , 内容如下:格式为-用户名:密码,权限
xxx:MD5:xxxx,admin

2. 添加权限验证

进入/disk/solr/solr-8.8.1/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">verify?name</Set>
        <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
      </New>
    </Set>
  </Get>
</Configure>

3. 修改web.xml

进入/disk/solr/solr-8.8.1/server/solr-webapp/webapp/WEB-INF,修改web.xml文件。

 <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>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>solr admin</realm-name>   
  </login-config>
  
  
  #添加在文件最后 </web-app> 之前增加即可

配置完成,重启服务,访问后提示出入账号密码!

3 代码添加账号密码

1. 配置文件

spring.data.solr.host=http://访问地址
spring.data.solr.username=账号
spring.data.solr.password=密码

2. 编写solr认证信息拦截器

/**
 * @author :yepk
 * @version :1.0
 * @apiNote :solr登录拦截器
 * @date :2021-06-08-15:02
 */

public class SolrAuthInterceptor implements HttpRequestInterceptor {
    @Override
    public void process(final HttpRequest request, final HttpContext context) {
        AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
        if (authState.getAuthScheme() == null) {
            CredentialsProvider provider =
                    (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
            HttpHost httpHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            AuthScope scope = new AuthScope(httpHost.getHostName(), httpHost.getPort());
            Credentials credentials = provider.getCredentials(scope);
            authState.update(new BasicScheme(), credentials);
        }
    }
}

3. 编写HttpSolrClient登录bean

@Component
public class SolrConfig {


    @Value("${spring.data.solr.username}")
    private String username;
    @Value("${spring.data.solr.password}")
    private String password;
    @Value("${spring.data.solr.host}")
    private String uri;

    /***
     * @apiNote 配置solr账号密码
     * @author yepk
     * @date 2021-6-8 15:05
     * 拦截器 {@link SolrAuthInterceptor}
     * @return {@link HttpSolrClient }
     */
    @Bean
    public HttpSolrClient solrClient() {
        CredentialsProvider provider = new BasicCredentialsProvider();
        final URI uri = URI.create(this.uri);
        provider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
                new UsernamePasswordCredentials(username, password));
        HttpClientBuilder builder = HttpClientBuilder.create();
        // 指定拦截器,用于设置认证信息
        builder.addInterceptorFirst(new SolrAuthInterceptor());
        builder.setDefaultCredentialsProvider(provider);
        CloseableHttpClient httpClient = builder.build();
        return new HttpSolrClient.Builder(this.uri).withHttpClient(httpClient).build();
    }
}
# solr   配置  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×