Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/main/java/com/fisa/pg/controller/MerchantController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,25 @@ public class MerchantController {
* 만료일이 지정되지 않은 경우 기본값으로 1년 후로 설정됩니다.
* </p>
*
* @param requestDto API 키 생성 요청 정보 (이름, 만료일)
* @param dto API 키 생성 요청 정보 (이름, 만료일)
* @param merchant 현재 인증된 가맹점 정보
* @return 생성된 API 키 정보와 함께 201 Created 응답
* @throws jakarta.validation.ConstraintViolationException 요청 데이터 유효성 검증 실패 시
*/
@PostMapping("/api-keys")
public ResponseEntity<CreateApiKeyResponseDto> createApiKey(
@Valid @RequestBody CreateApiKeyRequestDto requestDto,
public ResponseEntity<BaseResponse<CreateApiKeyResponseDto>> createApiKey(
@Valid @RequestBody CreateApiKeyRequestDto dto,
@AuthenticationPrincipal @NotNull Merchant merchant
) {

log.info("API 키 생성 요청: 가맹점 ID={}, 키 이름={}", merchant.getId(), requestDto.getName());
log.info("API 키 생성 요청: 가맹점 ID={}, 키 이름={}", merchant.getId(), dto.getName());

// 방어적 코딩: expiresAt이 null인 경우 기본값 설정 (필드에 @NotBlank 있지만 안전장치로 유지)
LocalDateTime expiresAt = requestDto.getExpiresAt() != null
? requestDto.getExpiresAt()
: LocalDateTime.now().plusYears(1);

CreateApiKeyResponseDto responseDto = apiKeyService.issueApiKey(
requestDto.getName(),
merchant,
expiresAt);
CreateApiKeyResponseDto data = apiKeyService.issueApiKey(dto, merchant);

log.info("API 키 생성 완료: 가맹점 ID={}", merchant.getId());
return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
return ResponseEntity.
status(HttpStatus.CREATED)
.body(BaseResponse.onCreate("가맹점 API 키 생성 성공", data));
}

/**
Expand All @@ -85,19 +79,19 @@ public ResponseEntity<CreateApiKeyResponseDto> createApiKey(
* @return 가맹점의 API 키 목록 페이지와 함께 200 OK 응답
*/
@GetMapping("/api-keys")
public ResponseEntity<Page<ApiKeyResponseDto>> getApiKeys(
public ResponseEntity<BaseResponse<Page<ApiKeyResponseDto>>> getApiKeys(
@AuthenticationPrincipal @NotNull Merchant merchant,
Pageable pageable
) {

log.info("API 키 목록 조회 요청: 가맹점 ID={}, 페이지={}, 크기={}",
merchant.getId(), pageable.getPageNumber(), pageable.getPageSize());

Page<ApiKeyResponseDto> responsePage = apiKeyService.getMerchantApiKeys(merchant, pageable);
Page<ApiKeyResponseDto> data = apiKeyService.getMerchantApiKeys(merchant, pageable);

log.info("API 키 목록 조회 완료: 가맹점 ID={}, 총 키 개수={}, 페이지 개수={}",
merchant.getId(), responsePage.getTotalElements(), responsePage.getTotalPages());
return ResponseEntity.ok(responsePage);
merchant.getId(), data.getTotalElements(), data.getTotalPages());
return ResponseEntity.ok(BaseResponse.onSuccess("가맹점 API 키 목록 조회 성공", data));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class CreateApiKeyRequestDto {
@NotBlank(message = "이름은 필수입니다.")
private String name;


@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private LocalDateTime expiresAt;

}
12 changes: 5 additions & 7 deletions src/main/java/com/fisa/pg/service/ApiKeyService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fisa.pg.service;

import com.fisa.pg.dto.request.CreateApiKeyRequestDto;
import com.fisa.pg.dto.response.ApiKeyResponseDto;
import com.fisa.pg.dto.response.CreateApiKeyResponseDto;
import com.fisa.pg.entity.auth.ApiKey;
Expand Down Expand Up @@ -44,31 +45,28 @@ public Page<ApiKeyResponseDto> getApiKeyList(Pageable pageable) {
/**
* API 키 발급 메서드 (가맹점용)
*
* @param keyName API 키 이름
* @param requestDto API 키 발급 요청 DTO
* @param merchant API 키를 발급받는 가맹점
* @param expiresAt 만료일 (지정하지 않으면 기본값으로 1년 후 설정)
* @return 생성된 API 키 정보
*/
@PostAuthorize("hasRole('MERCHANT')")
@Transactional
public CreateApiKeyResponseDto issueApiKey(String keyName, Merchant merchant, LocalDateTime expiresAt) {
public CreateApiKeyResponseDto issueApiKey(CreateApiKeyRequestDto requestDto, Merchant merchant) {
String accessKey = StringUtil.generateRandomString(20);
String secretKey = StringUtil.generateRandomString(40);

ApiKey apiKey = ApiKey.builder()
.merchant(merchant)
.name(merchant.getName())
.name(requestDto.getName())
.accessKey(accessKey)
.secretKey(secretKey)
.issuedAt(LocalDateTime.now())
.expiresAt(expiresAt)
.expiresAt(requestDto.getExpiresAt())
.isActive(true)
.build();

apiKeyRepository.save(apiKey);

log.info("API 키 발급 완료: 가맹점={}, 키이름={}", merchant.getName(), keyName);

return CreateApiKeyResponseDto.from(apiKey);
}

Expand Down