You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autoflow-server-mgmt/src/main/java/kr/re/etri/autoflow/service/ExternalAuthService.java

66 lines
2.2 KiB

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<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) {
return (String) dataMap.get("token");
}
}
throw new RuntimeException("Failed to get Bearer token");
}
}