Аутентификация SolrCore с использованием Spring Data Solr
У меня есть Служба (API), которая подключается к Solr для извлечения данных и отправки в качестве ответа. До сих пор у нас не было никакого типа слоя безопасности в Solr, поэтому мой API работал нормально. Но все изменилось, и все ядра были добавлены с чистотой слой для аутентификации. Я столкнулся с проблемой при подключении к Core в моем API. Ниже приведен мой класс Java для подключения:
@Configuration
@EnableConfigurationProperties
public class SolrConfigs {
private static final Logger LOG = LoggerFactory.getLogger(SolrConfigs.class);
@Value("${solr.core.FirstCore}")
private String coreFirstCore;
@Value("${solr.core.SecondCore}")
private String coreSecondCore;
@Value("${solr.core.ThirdCore}")
private String coreThirdCore;
@Bean
@ConfigurationProperties(prefix = "solr.client")
public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) {
LOG.info("Connecting to solr : {}", solrBaseUrl);
final HttpSolrClient client = new HttpSolrClient(solrBaseUrl);
return client;
}
@Bean
public MulticoreSolrClientFactory multicoreSolrClientFactory(final SolrClient solrClient) {
final MulticoreSolrClientFactory factory = new MulticoreSolrClientFactory(solrClient, Arrays.asList(coreFirstCore, coreSecondCore, coreThirdCore));
return factory;
}
@Bean(name = "solrFirstCoreTemplate")
public SolrTemplate solrFirstCoreTemplate(MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreFirstCore);
return template;
}
@Bean(name = "solrSecondCoreTemplate")
public SolrTemplate solrSecondCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreSecondCore);
return template;
}
@Bean(name = "solrThirdCoreTemplate")
public SolrTemplate solrThirdCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) {
final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory);
template.setSolrCore(coreThirdCore);
return template;
}
}
Есть ли способ передать учетные данные каждому ядру? Я пытался что-то вроде этого
@Bean
@ConfigurationProperties(prefix = "solr.client")
public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) {
LOG.info("Connecting to solr : {}", solrBaseUrl);
final HttpSolrClient client = new HttpSolrClient(solrBaseUrl);
return client;
}
@Bean(name = "solrFirstCoreTemplate")
public SolrTemplate solrFirstCoreTemplate(SolrClient solrClient) {
Credentials credentials = new UsernamePasswordCredentials(username, password);
final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreFirstCore, credentials , "BASIC"));
template.setSolrCore(coreFirstCore);
return template;
}
@Bean(name = "solrSecondCoreTemplate")
public SolrTemplate solrSecondCoreTemplate(SolrClient solrClient) {
Credentials credentials = new UsernamePasswordCredentials(username, password);
final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreSecondCore, credentials , "BASIC"));
template.setSolrCore(coreSecondCore);
return template;
}
Это не сработало.
2 ответа
Вы можете использовать код ниже для аутентификации...
@Bean
public SolrClientFactory solrClientFactory (окончательный SolrClient solrClient) {
Credentials credentials = new UsernamePasswordCredentials ("тест", "тест");
вернуть новый HttpSolrClientFactory(solrClient,"collection", учетные данные, "BASIC");
}
поскольку утвержденный ответ мне не помог, я поделюсь с вами своим решением.
@Value("${spring.data.solr.host}") String solrURL;
@Value("${spring.data.solr.user}") String solrUSER;
@Value("${spring.data.solr.pass}") String solrPASS;
@Bean
public SolrClient solrClient(){
if(solrUSER.isEmpty() || solrUSER.equals("none") || solrPASS.isEmpty() || solrPASS.equals("none"))
return new HttpSolrClient.Builder(solrURL).build();
HttpSolrClient solrClient = new HttpSolrClient.Builder(solrURL).build();
Credentials credentials = new UsernamePasswordCredentials(solrUSER, solrPASS);
return new HttpSolrClientFactory(solrClient, credentials, "BASIC").getSolrClient();
}
и добавьте эту зависимость, если у вас есть какие-либо ошибки, связанные с base64
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
в моем случае я сначала проверяю, использует ли сервер Solr аутентификацию или нет.
хост Solr - http://localhost:8983/solr/, и я использую весенние данные Solr