package kr.re.etri.autoflow.service; 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 javax.net.ssl.*; import java.security.cert.X509Certificate; import java.util.Map; @Service public class ExternalAuthService { @Value("${external.auth.signin-url}") private String signinUrl; private final RestTemplate restTemplate; 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) {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); this.restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory()); } public String getBearerToken(String id, String password) { Map body = Map.of( "id", id, "password", password ); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity> request = new HttpEntity<>(body, headers); ResponseEntity response = restTemplate.exchange( signinUrl, HttpMethod.POST, request, Map.class ); if (response.getStatusCode() == HttpStatus.OK) { Map respBody = response.getBody(); if (respBody != null && respBody.get("data") instanceof Map dataMap) { return (String) dataMap.get("token"); } } throw new RuntimeException("Failed to get Bearer token"); } }