|
|
|
|
package kr.re.etri.autoflow.service;
|
|
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
@Value("${external.auth.edge-search-url}")
|
|
|
|
|
private String edgeSearchUrl;
|
|
|
|
|
|
|
|
|
|
@Value("${external.edge.add-url:https://cuuva.com:24443/api/datamanager/edge-pkg/add}")
|
|
|
|
|
private String edgeAddUrl;
|
|
|
|
|
|
|
|
|
|
@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]; }
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
|
|
return new RestTemplate(requestFactory);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("Failed to create unsafe RestTemplate", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
|
|
HttpEntity<Map<String, String>> request = new HttpEntity<>(body, headers);
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Map> response = restTemplate.exchange(
|
|
|
|
|
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) {
|
|
|
|
|
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 fetch edge package list");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Object uploadEdgePackage(String bearerToken, String edgePkgInfoJson, MultipartFile file) throws IOException {
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
|
|
headers.set("Authorization", bearerToken.startsWith("Bearer ") ? bearerToken : "Bearer " + bearerToken);
|
|
|
|
|
|
|
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
|
|
body.add("edgePkgInfoVO", edgePkgInfoJson);
|
|
|
|
|
|
|
|
|
|
if (file != null && !file.isEmpty()) {
|
|
|
|
|
body.add("file", new org.springframework.core.io.ByteArrayResource(file.getBytes()) {
|
|
|
|
|
@Override
|
|
|
|
|
public String getFilename() {
|
|
|
|
|
return file.getOriginalFilename();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
|
|
|
|
|
|
|
|
|
|
ResponseEntity<Object> response = restTemplate.exchange(
|
|
|
|
|
edgeAddUrl,
|
|
|
|
|
HttpMethod.POST,
|
|
|
|
|
request,
|
|
|
|
|
Object.class
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response.getBody();
|
|
|
|
|
}
|
|
|
|
|
}
|