|
|
|
|
package kr.re.etri.autoflow.controllers;
|
|
|
|
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
|
|
|
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
|
import kr.re.etri.autoflow.payload.request.EdgeSWVO;
|
|
|
|
|
import kr.re.etri.autoflow.service.ExternalAuthService;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/external-auth")
|
|
|
|
|
@Tag(name = "ExternalAuthController", description = "외부 백엔드 로그인 및 Bearer 토큰 조회 API")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class ExternalAuthController {
|
|
|
|
|
|
|
|
|
|
private final ExternalAuthService externalAuthService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "외부 로그인 후 사용자 정보 조회", description = "외부 Spring 백엔드에 로그인 요청 후 id, name, token을 반환합니다.")
|
|
|
|
|
@PostMapping("/signin")
|
|
|
|
|
public ResponseEntity<ApiResponse> signin(@RequestBody SigninRequest request) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> userInfo = externalAuthService.getUserInfo(request.id(), request.password());
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(userInfo));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.failure(e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "Edge 패키지 목록 조회", description = "로그인 후 받은 id와 token을 사용해 외부 Edge 패키지 검색 API를 호출합니다.")
|
|
|
|
|
@GetMapping("/edge-search")
|
|
|
|
|
public ResponseEntity<ApiResponse> edgeSearch(
|
|
|
|
|
@RequestParam String id,
|
|
|
|
|
@RequestParam String token
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> edgeResult = externalAuthService.getEdgePackageList(id, token);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(edgeResult));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.failure(e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private final String TARGET_URL = "https://cuuva.com:24443/api/datamanager/edge-pkg/add";
|
|
|
|
|
|
|
|
|
|
@PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
|
|
|
@Operation(
|
|
|
|
|
summary = "외부 Edge 패키지 등록",
|
|
|
|
|
description = "VO 정보와 파일을 업로드하여 외부 Edge 패키지를 등록합니다.",
|
|
|
|
|
security = {@SecurityRequirement(name = "bearerAuth")}
|
|
|
|
|
)
|
|
|
|
|
public ResponseEntity<?> addEdgePackage(
|
|
|
|
|
@RequestPart("sw_id") String sw_id,
|
|
|
|
|
@RequestPart("sw_name") String sw_name,
|
|
|
|
|
@RequestPart("sw_version") String sw_version,
|
|
|
|
|
@RequestPart("creation_datetime") String creation_datetime,
|
|
|
|
|
@RequestPart("auth_id") String auth_id,
|
|
|
|
|
@RequestPart(value = "files", required = false) MultipartFile file, // ✅ 수정: file → files
|
|
|
|
|
@RequestHeader("Authorization") String bearerToken
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// VO 생성
|
|
|
|
|
EdgeSWVO edgeSWVO = new EdgeSWVO();
|
|
|
|
|
edgeSWVO.setSw_id(sw_id);
|
|
|
|
|
edgeSWVO.setSw_name(sw_name);
|
|
|
|
|
edgeSWVO.setSw_version(sw_version);
|
|
|
|
|
edgeSWVO.setCreation_datetime(creation_datetime);
|
|
|
|
|
edgeSWVO.setAuth_id(auth_id);
|
|
|
|
|
edgeSWVO.setFile(file); // ✅ 반드시 추가해야 WebClient에서 파일 전송 가능
|
|
|
|
|
|
|
|
|
|
// 토큰 정제
|
|
|
|
|
String token = bearerToken.startsWith("Bearer ") ? bearerToken.substring(7) : bearerToken;
|
|
|
|
|
|
|
|
|
|
// 서비스 호출
|
|
|
|
|
Map<String, Object> result = externalAuthService.addEdgePackage(token, edgeSWVO);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(result);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.badRequest().body(Map.of(
|
|
|
|
|
"success", false,
|
|
|
|
|
"message", e.getMessage()
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DTO: 요청
|
|
|
|
|
public static record SigninRequest(String id, String password) { }
|
|
|
|
|
|
|
|
|
|
// DTO: 응답
|
|
|
|
|
public static class ApiResponse {
|
|
|
|
|
private boolean success;
|
|
|
|
|
private Object data;
|
|
|
|
|
private String errorMessage;
|
|
|
|
|
|
|
|
|
|
public ApiResponse(boolean success, Object data, String errorMessage) {
|
|
|
|
|
this.success = success;
|
|
|
|
|
this.data = data;
|
|
|
|
|
this.errorMessage = errorMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApiResponse success(Object data) {
|
|
|
|
|
return new ApiResponse(true, data, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApiResponse failure(String errorMessage) {
|
|
|
|
|
return new ApiResponse(false, null, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isSuccess() { return success; }
|
|
|
|
|
public Object getData() { return data; }
|
|
|
|
|
public String getErrorMessage() { return errorMessage; }
|
|
|
|
|
}
|
|
|
|
|
}
|