Skip to content

Commit

Permalink
[Feat/#85] Swagger JWT Auth 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnsugyeong committed Feb 5, 2024
1 parent 5146596 commit fe42da7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.friends.easybud.auth.dto.IdTokenRequest;
import com.friends.easybud.auth.dto.RefreshTokenRequest;
import com.friends.easybud.auth.service.AuthService;
import com.friends.easybud.global.annotation.AuthUser;
import com.friends.easybud.global.annotation.AuthMember;
import com.friends.easybud.global.response.ResponseDto;
import com.friends.easybud.jwt.JwtDto;
import com.friends.easybud.jwt.JwtProvider;
Expand Down Expand Up @@ -51,7 +51,7 @@ public ResponseDto<Boolean> logout(@RequestBody RefreshTokenRequest request) {

@Operation(summary = "회원 탈퇴", description = "회원 탈퇴를 진행합니다.")
@PostMapping("/withdrawal")
public ResponseDto<Boolean> withdrawal(@AuthUser Member member) {
public ResponseDto<Boolean> withdrawal(@AuthMember Member member) {
return ResponseDto.onSuccess(memberCommandService.withdrawal(member));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.friends.easybud.card.dto.CardResponse.CardListDto;
import com.friends.easybud.card.service.CardCommandService;
import com.friends.easybud.card.service.CardQueryService;
import com.friends.easybud.global.annotation.AuthUser;
import com.friends.easybud.global.annotation.AuthMember;
import com.friends.easybud.global.response.ResponseDto;
import com.friends.easybud.member.domain.Member;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -33,32 +33,32 @@ public class CardController {

@Operation(summary = "카드 생성", description = "새로운 카드를 생성합니다.")
@PostMapping
public ResponseDto<Long> createCard(@AuthUser Member member, @RequestBody CardCreateDto request) {
public ResponseDto<Long> createCard(@AuthMember Member member, @RequestBody CardCreateDto request) {
return ResponseDto.onSuccess(cardCommandService.createCard(member, request));
}

@Operation(summary = "카드 삭제", description = "기존의 카드를 삭제합니다.")
@DeleteMapping("/{cardId}")
public ResponseDto<Boolean> deleteCard(@AuthUser Member member, @PathVariable Long cardId) {
public ResponseDto<Boolean> deleteCard(@AuthMember Member member, @PathVariable Long cardId) {
return ResponseDto.onSuccess(cardCommandService.deleteCard(member, cardId));
}

@Operation(summary = "카드 수정", description = "기존의 카드를 수정합니다.")
@PutMapping("/{cardId}")
public ResponseDto<Long> updateCard(@AuthUser Member member, @PathVariable Long cardId,
public ResponseDto<Long> updateCard(@AuthMember Member member, @PathVariable Long cardId,
@RequestBody CardUpdateDto request) {
return ResponseDto.onSuccess(cardCommandService.updateCard(member, cardId, request));
}

@Operation(summary = "카드 조회", description = "특정 카드를 조회합니다.")
@GetMapping("/{cardId}")
public ResponseDto<CardDto> getCard(@AuthUser Member member, @PathVariable Long cardId) {
public ResponseDto<CardDto> getCard(@AuthMember Member member, @PathVariable Long cardId) {
return ResponseDto.onSuccess(CardConverter.toCardDto(cardQueryService.getCard(member, cardId)));
}

@Operation(summary = "카드 목록 조회", description = "특정 회원의 카드 목록을 조회합니다.")
@GetMapping
public ResponseDto<CardListDto> getCards(@AuthUser Member member) {
public ResponseDto<CardListDto> getCards(@AuthMember Member member) {
return ResponseDto.onSuccess(CardConverter.toCardListDto(cardQueryService.getCards(member)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.friends.easybud.category.dto.CategoryRequest.TertiaryCategoryCreateDto;
import com.friends.easybud.category.service.CategoryCommandService;
import com.friends.easybud.category.service.CategoryQueryService;
import com.friends.easybud.global.annotation.AuthUser;
import com.friends.easybud.global.annotation.AuthMember;
import com.friends.easybud.global.response.ResponseDto;
import com.friends.easybud.member.domain.Member;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -32,21 +32,22 @@ public class CategoryController {

@Operation(summary = "계정 소분류 생성", description = "새로운 소분류를 생성합니다.")
@PostMapping("/tertiary")
public ResponseDto<Long> createTertiaryCategory(@AuthUser Member member,
public ResponseDto<Long> createTertiaryCategory(@AuthMember Member member,
@RequestBody TertiaryCategoryCreateDto request) {
return ResponseDto.onSuccess(categoryCommandService.createTertiaryCategory(member, request));
}

@Operation(summary = "계정 소분류 삭제", description = "기존의 소분류를 삭제합니다.")
@Parameter(name = "tertiaryCategoryId", description = "삭제할 소분류의 ID")
@DeleteMapping("/tertiary/{tertiaryCategoryId}")
public ResponseDto<Boolean> deleteTertiaryCategory(@AuthUser Member member, @PathVariable Long tertiaryCategoryId) {
public ResponseDto<Boolean> deleteTertiaryCategory(@AuthMember Member member,
@PathVariable Long tertiaryCategoryId) {
return ResponseDto.onSuccess(categoryCommandService.deleteTertiaryCategory(member, tertiaryCategoryId));
}

@Operation(summary = "계정 카테고리 목록 조회", description = "로그인 된 회원의 계정 카테고리 목록을 조회합니다.")
@GetMapping
public ResponseDto<AccountCategoryListDto> getAccountCategories(@AuthUser Member member) {
public ResponseDto<AccountCategoryListDto> getAccountCategories(@AuthMember Member member) {
return ResponseDto.onSuccess(
CategoryConverter.toAccountCategoryListDto(categoryQueryService.getTertiaryCategories(member)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.friends.easybud.financial.dto.FinancialResponse.IncomeStatementSummaryDto;
import com.friends.easybud.financial.dto.FinancialResponse.ProfitLossListDto;
import com.friends.easybud.financial.service.FinancialService;
import com.friends.easybud.global.annotation.AuthUser;
import com.friends.easybud.global.annotation.AuthMember;
import com.friends.easybud.global.response.ResponseDto;
import com.friends.easybud.member.domain.Member;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -34,21 +34,21 @@ public class FinancialController {

@Operation(summary = "가용자금 조회", description = "사용자의 가용자금을 조회합니다.")
@GetMapping("/available-funds")
public ResponseDto<AvailableFundsDto> getAvailableFunds(@AuthUser Member member) {
public ResponseDto<AvailableFundsDto> getAvailableFunds(@AuthMember Member member) {
return ResponseDto.onSuccess(financialService.getAvailableFunds(member));
}

@Operation(summary = "재무 상태 조회", description = "사용자의 재무 상태를 조회합니다.")
@GetMapping("/financial-statement")
public ResponseDto<FinancialStatementDto> getFinancialStatement(@AuthUser Member member) {
public ResponseDto<FinancialStatementDto> getFinancialStatement(@AuthMember Member member) {
return ResponseDto.onSuccess(financialService.getFinancialStatement(member));
}

@Operation(summary = "손익현황 조회", description = "사용자의 손익현황을 조회합니다.")
@Parameter(name = "startDate", example = "2024-02-01")
@Parameter(name = "endDate", example = "2024-02-02")
@GetMapping("/income-statement")
public ResponseDto<IncomeStatementDto> getIncomeStatement(@AuthUser Member member,
public ResponseDto<IncomeStatementDto> getIncomeStatement(@AuthMember Member member,
@RequestParam LocalDate startDate,
@RequestParam LocalDate endDate) {
LocalDateTime startOfDay = startDate.atStartOfDay();
Expand All @@ -60,7 +60,7 @@ public ResponseDto<IncomeStatementDto> getIncomeStatement(@AuthUser Member membe
@Parameter(name = "year", example = "2024")
@Parameter(name = "month", example = "2")
@GetMapping("/income-statement/summary/monthly")
public ResponseDto<IncomeStatementSummaryDto> getMonthlyIncomeStatementSummary(@AuthUser Member member,
public ResponseDto<IncomeStatementSummaryDto> getMonthlyIncomeStatementSummary(@AuthMember Member member,
@RequestParam int year,
@RequestParam int month) {
LocalDateTime startOfMonth = LocalDateTime.of(year, month, 1, 0, 0, 0);
Expand All @@ -75,7 +75,7 @@ public ResponseDto<IncomeStatementSummaryDto> getMonthlyIncomeStatementSummary(@
@Parameter(name = "year", example = "2024")
@Parameter(name = "month", example = "2")
@GetMapping("/income-statement/summary/daily")
public ResponseDto<ProfitLossListDto> getDailyIncomeStatementSummary(@AuthUser Member member,
public ResponseDto<ProfitLossListDto> getDailyIncomeStatementSummary(@AuthMember Member member,
@RequestParam int year,
@RequestParam int month) {
return ResponseDto.onSuccess(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.friends.easybud.global.annotation;

import io.swagger.v3.oas.annotations.Parameter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthUser {
@Parameter(hidden = true)
public @interface AuthMember {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AuthenticationArgumentResolver implements HandlerMethodArgumentReso

@Override
public boolean supportsParameter(MethodParameter parameter) {
final boolean isRegUserAnnotation = parameter.getParameterAnnotation(AuthUser.class) != null;
final boolean isRegUserAnnotation = parameter.getParameterAnnotation(AuthMember.class) != null;
final boolean isMember = parameter.getParameterType().equals(Member.class);
return isRegUserAnnotation && isMember;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -16,11 +18,23 @@ public OpenAPI openAPI() {
.version("v0.0.1")
.description("Easybud의 API 명세서입니다.");

String jwtSchemeName = "JWT";
// API 요청헤더에 인증정보 포함
SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName);
// SecuritySchemes 등록
Components components = new Components()
.addSecuritySchemes(jwtSchemeName, new SecurityScheme()
.name(jwtSchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT"));

Server server = new Server().url("/");

return new OpenAPI()
.components(new Components())
.components(components)
.info(info)
.addSecurityItem(securityRequirement)
.addServersItem(server);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.friends.easybud.transaction.controller;

import com.friends.easybud.global.annotation.AuthUser;
import com.friends.easybud.global.annotation.AuthMember;
import com.friends.easybud.global.response.ResponseDto;
import com.friends.easybud.member.domain.Member;
import com.friends.easybud.transaction.converter.TransactionConverter;
Expand Down Expand Up @@ -35,29 +35,29 @@ public class TransactionController {

@Operation(summary = "거래 생성", description = "새로운 거래를 생성합니다.")
@PostMapping
public ResponseDto<Long> createTransaction(@AuthUser Member member,
public ResponseDto<Long> createTransaction(@AuthMember Member member,
@RequestBody TransactionCreateDto request) {
return ResponseDto.onSuccess(transactionCommandService.createTransaction(member, request));
}

@Operation(summary = "거래 삭제", description = "기존의 거래를 삭제합니다.")
@DeleteMapping("/{transactionId}")
public ResponseDto<Boolean> deleteTransaction(@AuthUser Member member,
public ResponseDto<Boolean> deleteTransaction(@AuthMember Member member,
@PathVariable Long transactionId) {
return ResponseDto.onSuccess(transactionCommandService.deleteTransaction(member, transactionId));
}

@Operation(summary = "거래 조회", description = "특정 거래를 조회합니다.")
@GetMapping("/{transactionId}")
public ResponseDto<TransactionDto> getTransaction(@AuthUser Member member,
public ResponseDto<TransactionDto> getTransaction(@AuthMember Member member,
@PathVariable Long transactionId) {
return ResponseDto.onSuccess(
TransactionConverter.toTransactionDto(transactionQueryService.getTransaction(member, transactionId)));
}

@Operation(summary = "특정 날짜의 거래 조회", description = "주어진 날짜에 해당하는 모든 거래 목록을 조회합니다.")
@GetMapping("/date/{date}")
public ResponseDto<TransactionListDto> getTransactionsByDate(@AuthUser Member member,
public ResponseDto<TransactionListDto> getTransactionsByDate(@AuthMember Member member,
@PathVariable @DateTimeFormat(iso = ISO.DATE) LocalDate date) {
LocalDateTime startOfDay = date.atStartOfDay();
LocalDateTime endOfDay = date.atTime(23, 59, 59);
Expand All @@ -68,15 +68,15 @@ public ResponseDto<TransactionListDto> getTransactionsByDate(@AuthUser Member me

@Operation(summary = "최근 3개의 거래 조회", description = "가장 최근에 이루어진 3개의 거래를 조회합니다.")
@GetMapping("/recent")
public ResponseDto<TransactionListDto> getRecentTransactions(@AuthUser Member member) {
public ResponseDto<TransactionListDto> getRecentTransactions(@AuthMember Member member) {
return ResponseDto.onSuccess(
TransactionConverter.toTransactionListDto(transactionQueryService.getRecentTransactions(member)));
}

@Operation(summary = "특정 연도와 달의 거래 조회", description = "주어진 연도와 달에 해당하는 모든 거래 목록을 조회합니다.")
@GetMapping("/year/{year}/month/{month}")
public ResponseDto<TransactionListDto> getTransactionsByYearAndMonth(
@AuthUser Member member,
@AuthMember Member member,
@PathVariable int year,
@PathVariable int month) {
LocalDate startOfMonth = LocalDate.of(year, month, 1);
Expand Down

0 comments on commit fe42da7

Please sign in to comment.