2016-10-29 5 views
0

Ich habe Solr 6.2.1 als SolrCloud konfiguriert. Später habe ich Basic Authentication konfiguriert. Ich werde Frühlings Daten solr 2.0.4.RELEASE mit Solrj 6.2 und das ist mein Code konfigurieren:So konfigurieren Sie Spring-Daten SolrCloud-Verbindung mit Basic-Authentifizierung

@Configuration 
@EnableSolrRepositories(basePackages = { "ir.saeed.server.solr" }, multicoreSupport = true) 

public class SearchContext { 

    @Value("${solr.host}") 

    private String host; 


    @Value("${solr.port}") 

    private Integer port; 


    @Value("${solr.username}") 

    private String username; 


    @Value("${solr.password}") 

    private String password; 


    @Value("${zkHost}") 

    private String zkHost; 


    @Value("${solr.coreName}") 

    private String collectionName; 


    @Bean 

    public SolrTemplate solrTemplate() { 

     return new SolrTemplate(solrClientFactory()); 

    } 


    @Bean 

    public BasicCredentialsProvider credentialsProvider() { 

     BasicCredentialsProvider provider = new BasicCredentialsProvider(); 

     provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); 

     return provider; 

    } 


    @Bean 

    public SolrClientFactory solrClientFactory() { 

     return new HttpSolrClientFactory(solrClient(), "", credentialsProvider().getCredentials(AuthScope.ANY), "BASIC"); 

    } 


    @Bean 

    public SolrClient solrClient() { 

     return new CloudSolrClient.Builder().withZkHost(zkHost).build(); 

    } 

} 

Aber wenn ich meine Web-Anwendung ausführen, diese Ausnahme occures:

10:51:48,110 org.springframework.data.solr.UncategorizedSolrException: nested exception is java.lang.NullPointerException 


10:51:48,111 at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:172) 


10:51:48,111 at org.springframework.data.solr.core.SolrTemplate.executeSolrQuery(SolrTemplate.java:509) 


10:51:48,111 at org.springframework.data.solr.core.SolrTemplate.query(SolrTemplate.java:504) 


10:51:48,111 at org.springframework.data.solr.core.SolrTemplate.doQueryForPage(SolrTemplate.java:338) 


10:51:48,111 at org.springframework.data.solr.core.SolrTemplate.queryForPage(SolrTemplate.java:350) 

Wie kann Ich löse das Problem? Ich denke, meine Konfiguration ist falsch

+0

Ich habe meine Konfiguration geändert jetzt verwende ich solrj-5.5 und ZooKeeper 3.4.6 und ich habe diese Zeile geändert: neuen CloudSolrClient.Builder zurückkehren() withZkHost (zkHost)..bauen(); an: Rückgabe neuer CloudSolrClient (zkURL); aber mein Problem nicht lösen jetzt diese Ausnahme occures: 11: 08: 43.827 org.springframework.data.solr.UncategorizedSolrException: Fehler vom Server 11: 08: 43.827 11: 08: 43.827 11: 08: 43.827 Fehler 401 eine Authentifizierung erforderlich 11: 08: 43.827 –

Antwort

0

Es gibt mehrere Gründe, warum Ihr Code nicht funktioniert, aber sie sind hauptsächlich auf Feder-Daten-Solr fehlenden Funktionen gelogen.

Zuerst spring-data-solr Version 2.0.4 hat keine Unterstützung für Solr 5 (Cloud) -Features. Das ist also der Grund, warum Sie die NullPointerException in dem Verfahren erhalten org.springframework.data.solr.server.support.SolrClientUtils#cloneLBHttpSolrClient

Ich habe versucht, ob es von Ihnen ausgesetzt das Szenario zu sehen, arbeitet mit dem neuesten SNAPSHOT (2.1.0-SNAPSHOT) feder Data- solr und nach einigen Modifikationen auf der SolrContext Federkonfiguration Klasse:

@Configuration 
@EnableSolrRepositories(basePackages = {"com.acme.solr"}) 
// notice that the multicoresupport is left false 
// see org.springframework.data.solr.repository.config.SolrRepositoryConfigExtension#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource) for details 
public class SolrContext { 

    @Bean 
    public Credentials credentials(@Value("${solr.username}") String username, @Value("${solr.password}") String 
      password) { 
     return new UsernamePasswordCredentials(username, password); 
    } 

    @Bean 
    public BasicCredentialsProvider credentialsProvider(Credentials credentials) { 
     BasicCredentialsProvider provider = new BasicCredentialsProvider(); 
     provider.setCredentials(AuthScope.ANY, credentials); 

     return provider; 
    } 

    @Bean 
    public SolrClientFactory solrClientFactory(SolrClient solrClient, Credentials credentials) { 
     return new HttpSolrClientFactory(solrClient, "", credentials, "BASIC"); 

    } 

    // create a solrtemplate bean, so that it is used in 
    // org.springframework.data.solr.repository.support.SolrRepositoryFactoryBean#doCreateRepositoryFactory method 
    // for using org.springframework.data.solr.repository.support.SolrRepositoryFactory#SolrRepositoryFactory(org.springframework.data.solr.core.SolrOperations) constructor 
    @Bean 
    public SolrTemplate solrTemplate(SolrClientFactory solrClientFactory){ 
     return new SolrTemplate(solrClientFactory); 
    } 

    @Bean 
    public CloudSolrClient solrClient(@Value("${zkHost}") String zkHost) { 
     CloudSolrClient solrClient = new CloudSolrClient.Builder().withZkHost(zkHost).build(); 
     solrClient.setDefaultCollection("gettingstarted"); 
     return solrClient; 
    } 
} 

I erhielt noch ein 401 Authentifizierungsproblem, wenn eine Anfrage durchführt solr (wenn die Basisauthentifizierungs wurden auf solr aktiviert).

In einer solrj Anwendung Vanille, das ist, wie Sie eine authentifizierte Anforderung machen würden:

CloudSolrClient solr = new CloudSolrClient.Builder() 
      .withZkHost("localhost:9983") 
      .build(); 

    SolrQuery query = new SolrQuery(); 
    query.setQuery("*:*"); 
    SolrRequest<QueryResponse> req = new QueryRequest(query); 
    req.setBasicAuthCredentials("solr", "SolrRocks"); 
    QueryResponse rsp = req.process(solr, "gettingstarted"); 

    System.out.println("numFound: " + rsp.getResults().getNumFound()); 

Bei der Suche, ob die Methode SolrRequest#setBasicAuthCredentials(String, String) im Code verwendet wird, Feder-data-solr Ich habe nicht bemerkt, wo auch immer diese Methode verwendet wird. Es ist also sehr wahrscheinlich, dass dieses Feature noch nicht einmal auf dem SNAPSHOT-Build des Feder-Daten-Solr implementiert ist.

Ich habe ein feature request auf Spring-Data-Solr-Projekt erstellt, um Unterstützung für diese Funktionalität hinzuzufügen.

0

Ich fand eine Abhilfe für diejenigen, die das gleiche Problem hat Erweitert Ihre eigene HttpSolrClientFactory. das Problem, das von LBHttpSolrClient httpClient verursacht wird, das nicht ordnungsgemäß eingerichtet wird. korrekte Einstellung sollte der folgenden Block wenn sein (solrClient Instanceof LBHttpSolrClient) {...}

AuthHttpSolrClientFactory.java

@SuppressWarnings ("deprecation") public class AuthHttpSolrClientFactory erstreckt HttpSolrClientFactory {

public AuthHttpSolrClientFactory(SolrClient solrClient, String core, Credentials credentials, String authPolicy) { 
    super(solrClient, core, credentials, authPolicy); 
    Assert.notNull(solrClient, "solrClient must not be null"); 
    if (authPolicy != null) { 
     Assert.hasText(authPolicy); 
    } 
    appendBasicAuthentication(credentials, authPolicy, this.getSolrClient()); 
} 

private void appendBasicAuthentication(Credentials credentials, String authPolicy, SolrClient solrClient) { 
    if(credentials != null) { 
     if (solrClient instanceof HttpSolrClient) { 
      HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient; 
      if (assertHttpClientInstance(httpSolrClient.getHttpClient())) { 
       AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrClient.getHttpClient(); 
       httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials); 
       httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy)); 
      } 
     } 
     else if (solrClient instanceof LBHttpSolrClient) { 
      LBHttpSolrClient lbhttpSolrClient = (LBHttpSolrClient) solrClient; 
      if (assertHttpClientInstance(lbhttpSolrClient.getHttpClient())) { 
       AbstractHttpClient httpClient = (AbstractHttpClient) lbhttpSolrClient.getHttpClient(); 
       httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials); 
       httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy)); 
      } 
     } 
    } 
} 

private boolean assertHttpClientInstance(HttpClient httpClient) { 
    Assert.isInstanceOf(AbstractHttpClient.class, httpClient, 
      "HttpClient has to be derivate of AbstractHttpClient in order to allow authentication."); 
    return true; 
} 

}

Bohne s-solr.xml

<solr:solr-client id="solrClient" url="${solr.host}" /> 
<bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials"> 
    <constructor-arg type="java.lang.String" value="${solr.credentials}"/> 
</bean> 
<bean id="solrClientFactory" class="com.example.solr.AuthHttpSolrClientFactory" scope="singleton"> 
    <constructor-arg ref="solrClient" /> 
    <constructor-arg name="core"> 
     <null /> 
    </constructor-arg> 
    <constructor-arg ref="credentials" /> 
    <constructor-arg type="java.lang.String" value="BASIC"/> 
</bean> 
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate" scope="singleton"> 
    <constructor-arg ref="solrClientFactory" /> 
    <constructor-arg name="requestMethod"> 
     <value type="org.springframework.data.solr.core.RequestMethod">POST</value> 
    </constructor-arg> 
</bean> 
+0

für Frühjahr-data-Daten 3.0, entfernen Sie einfach der *** Kern *** aus dem obigen Beispiel wird es funktionieren. – Fei

Verwandte Themen