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.
59 lines
2.1 KiB
59 lines
2.1 KiB
|
8 months ago
|
package kr.re.etri.autoflow.controllers;
|
||
|
|
|
||
|
|
import io.swagger.v3.oas.annotations.Operation;
|
||
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
|
|
import kr.re.etri.autoflow.service.ExternalAuthService;
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/external-auth")
|
||
|
|
@Tag(name = "ExternalAuthController", description = "외부 백엔드 로그인 및 Bearer 토큰 조회 API")
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class ExternalAuthController {
|
||
|
|
|
||
|
|
private final ExternalAuthService externalAuthService;
|
||
|
|
|
||
|
|
@Operation(summary = "외부 로그인 후 Bearer 토큰 조회", description = "외부 Spring 백엔드에 로그인 요청 후 Bearer 토큰을 반환합니다.")
|
||
|
|
@PostMapping("/signin")
|
||
|
|
public ResponseEntity<ApiResponse> signin(
|
||
|
|
@RequestBody SigninRequest request
|
||
|
|
) {
|
||
|
|
try {
|
||
|
|
String token = externalAuthService.getBearerToken(request.id(), request.password());
|
||
|
|
return ResponseEntity.ok(ApiResponse.success(token));
|
||
|
|
} catch (Exception e) {
|
||
|
|
return ResponseEntity.ok(ApiResponse.failure(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; }
|
||
|
|
}
|
||
|
|
}
|