저 같은 경우 Elasitc Search를 사용할 때 직접 API 문서 보고 자바 코드로 작성합니다.
아래 링크는 Elasitc Search 공식 API Basic Authentication 입니다.
Basic authentication | Java REST Client [7.14] | Elastic
Configuring basic authentication can be done by providing an HttpClientConfigCallback while building the RestClient through its builder. The interface has one method that receives an instance of org.apache.http.impl.nio.client.HttpAsyncClientBuilder as an
www.elastic.co
제 서버 환경은 아래와 같습니다.
- CentOS7
- JDK1.8
- ElasticSearch 7.13.4
우선 설치되어있는 서버나 로컬에서 Elastic Search에 보안설정을 해주어야 합니다.
보안설정은 아래를 참고해주세요.
[ElasticSearch]Elasticsearch 보안 적용 방법
ElasticSearch는 url을 이용해서 쉽게 데이터 전송이 가능하기 때문에 ElasticSearch를 이용하여 서비스를 할 때 반드시 보안설정을 해주어야 합니다. 만약 해주지 않는다면 임의의 사용자가 마음대로 CR
kingofbackend.tistory.com
보안설정을 완료했다치고 자바에서 코드로 구현해보겠습니다.
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(ElasticSearchService.esUserId, ElasticSearchService.esPassword));
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost(serverName, 9200))
.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider);
}
}));
문서에 나와있는게 다 입니다. esUserId, esPassword, serverName만 본인 환경에 맞게 설정하면 됩니다. 위 글에서 설정한 es 계정을 입력해주시면 됩니다.
es_user_id=elastic
es_password=1234
serverName=192.168.0.1
크게 어려운 부분은 없네요 ㅎㅎ
이상 Elasitc Search + Java 사용자 인증 구현하기 였습니다.
'...' 카테고리의 다른 글
[JS] 자바스크립트 쿠키 생성, 저장, 삭제 구현하기 (0) | 2021.08.20 |
---|---|
[Elastic Search+Java] Java에서 Elastic Search 시작하기 (0) | 2021.08.19 |
[ElasticSearch] ElasticSearch 외부 접속하기 (0) | 2021.08.18 |
[ElasticSearch]Elasticsearch 보안 적용 방법 (0) | 2021.08.18 |
[Linux] CentOS 7에 OpenJDK 1.8 설치하기 (0) | 2021.08.18 |