|
|
|
|
@ -1,45 +1,62 @@
|
|
|
|
|
package kr.re.etri.autoflow.service;
|
|
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ExternalAuthService {
|
|
|
|
|
|
|
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
|
|
|
|
@Value("${external.auth.signin-url}")
|
|
|
|
|
private String signinUrl;
|
|
|
|
|
|
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
|
|
@Value("${external.auth.edge-search-url}")
|
|
|
|
|
private String edgeSearchUrl;
|
|
|
|
|
|
|
|
|
|
public ExternalAuthService() throws Exception {
|
|
|
|
|
// SSLContext 생성 (모든 인증서 허용)
|
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
|
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
this.restTemplate = createUnsafeRestTemplate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** SSL 무시용 RestTemplate 생성 */
|
|
|
|
|
private RestTemplate createUnsafeRestTemplate() {
|
|
|
|
|
try {
|
|
|
|
|
TrustManager[] trustAllCerts = new TrustManager[]{
|
|
|
|
|
new X509TrustManager() {
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
|
|
|
|
|
}}, new java.security.SecureRandom());
|
|
|
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) { }
|
|
|
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) { }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
|
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
|
|
|
|
|
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
|
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
|
|
|
|
|
|
|
|
|
|
this.restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory());
|
|
|
|
|
}
|
|
|
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
|
|
return new RestTemplate(requestFactory);
|
|
|
|
|
|
|
|
|
|
public String getBearerToken(String id, String password) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("Failed to create unsafe RestTemplate", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, String> body = Map.of(
|
|
|
|
|
"id", id,
|
|
|
|
|
"password", password
|
|
|
|
|
);
|
|
|
|
|
/** Bearer 토큰 발급 */
|
|
|
|
|
public Map<String, Object> getUserInfo(String id, String password) {
|
|
|
|
|
// 요청 본문
|
|
|
|
|
Map<String, String> body = Map.of("id", id, "password", password);
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
@ -47,19 +64,42 @@ public class ExternalAuthService {
|
|
|
|
|
HttpEntity<Map<String, String>> request = new HttpEntity<>(body, headers);
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Map> response = restTemplate.exchange(
|
|
|
|
|
signinUrl,
|
|
|
|
|
HttpMethod.POST,
|
|
|
|
|
request,
|
|
|
|
|
Map.class
|
|
|
|
|
);
|
|
|
|
|
signinUrl, HttpMethod.POST, request, Map.class);
|
|
|
|
|
|
|
|
|
|
if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
|
|
Map<String, Object> respBody = response.getBody();
|
|
|
|
|
if (respBody != null && respBody.get("data") instanceof Map dataMap) {
|
|
|
|
|
return (String) dataMap.get("token");
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
result.put("id", dataMap.get("id"));
|
|
|
|
|
result.put("name", dataMap.get("name"));
|
|
|
|
|
result.put("token", dataMap.get("token"));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new RuntimeException("Failed to get user info from external server");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Edge 패키지 검색 API 호출
|
|
|
|
|
*/
|
|
|
|
|
public Map<String, Object> getEdgePackageList(String id, String token) {
|
|
|
|
|
// URL 구성
|
|
|
|
|
String url = String.format("%s?sw_group=-1&sw_type=-1&searchType=&searchText=&pageNum=1&pageSize=10&auth_id=%s&user_id=",
|
|
|
|
|
edgeSearchUrl, id);
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setBearerAuth(token);
|
|
|
|
|
headers.setAccept(java.util.List.of(MediaType.APPLICATION_JSON));
|
|
|
|
|
|
|
|
|
|
HttpEntity<Void> request = new HttpEntity<>(headers);
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, request, Map.class);
|
|
|
|
|
|
|
|
|
|
if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
|
|
return response.getBody();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new RuntimeException("Failed to get Bearer token");
|
|
|
|
|
throw new RuntimeException("Failed to fetch edge package list");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|