Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Swagger 적용 #12

Merged
merged 1 commit into from
Feb 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import io.swagger.v3.oas.annotations.Hidden;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.AccessLevel;
Expand All @@ -28,6 +29,7 @@ public PageCondition(Integer page, Integer size) {
this.size = Boolean.TRUE.equals(isValidSize(size)) ? size : DEFAULT_SIZE;
}

@Hidden
public Pageable getPageable() {
return PageRequest.of(this.page - 1, this.size);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.pgms.apimember.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Operation()
@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "sec-401/01", description = "로그인이 필요합니다.", content = @Content),
@ApiResponse(responseCode = "sec-401/02", description = "액세스 토큰이 만료되었으니 다시 로그인 해주세요.", content = @Content),
@ApiResponse(responseCode = "sec-401/03", description = "리프레시 토큰이 만료되었으니 다시 로그인 해주세요.", content = @Content),
@ApiResponse(responseCode = "sec-401/04", description = "현재 토큰은 로그아웃 상태로 재로그인이 필요합니다.", content = @Content),
@ApiResponse(responseCode = "sec-403/01", description = "해당 토큰에 접근 권한이 없습니다.", content = @Content)
})
public @interface SwaggerResponseAuth {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pgms.apimember.annotation.SwaggerResponseAuth;
import com.pgms.apimember.dto.request.LoginRequest;
import com.pgms.apimember.dto.response.AuthResponse;
import com.pgms.apimember.service.AuthService;
import com.pgms.coredomain.response.ApiResponse;
import com.pgms.coresecurity.jwt.JwtTokenProvider;
import com.pgms.coresecurity.resolver.CurrentAccount;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Tag(name = "회원 인증", description = "회원 인증 관련 API 입니다.")
@SwaggerResponseAuth
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/auth")
Expand All @@ -28,11 +33,13 @@ public class AuthController {
private final AuthService authService;
private final JwtTokenProvider jwtTokenProvider;

@Operation(summary = "로그인")
@PostMapping("/login")
public ResponseEntity<ApiResponse<AuthResponse>> login(@Valid @RequestBody LoginRequest request) {
return ResponseEntity.ok(ApiResponse.of(authService.login(request)));
}

@Operation(summary = "로그아웃")
@PostMapping("/logout")
public ResponseEntity<ApiResponse<Void>> logout(
@RequestHeader("Authorization") String bearerToken,
Expand All @@ -42,11 +49,13 @@ public ResponseEntity<ApiResponse<Void>> logout(
return ResponseEntity.ok(ApiResponse.of(SUCCESS));
}

@Operation(summary = "게스트 로그인")
@PostMapping("/guest")
public ResponseEntity<ApiResponse<AuthResponse>> guestLogin() {
return ResponseEntity.ok(ApiResponse.of(authService.guestLogin()));
}

@Operation(summary = "토큰 재발급")
@PostMapping("/reissue")
public ResponseEntity<ApiResponse<AuthResponse>> reIssueAccessToken(
@CookieValue("refreshToken") String refreshToken) {
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ subprojects {
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.pgms.coredomain.config;

import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

@OpenAPIDefinition(
info = @Info(title = "티키타자 백엔드 API 명세서",
description = "티키타자 백엔드 API 명세서 입니다",
version = "0.1"),
servers = {
@Server(url = "/", description = "티키타자 서버 URL")}
)
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI openAPI() {
SecurityScheme securityScheme = new SecurityScheme()
.type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")
.in(SecurityScheme.In.HEADER).name("Authorization");
SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth");

return new OpenAPI()
.components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
.security(Arrays.asList(securityRequirement));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private RequestMatcher[] requestHasRoleUser() {
private RequestMatcher[] requestPermitAll() {
List<RequestMatcher> requestMatchers = List.of(
antMatcher("/"),
antMatcher("/swagger-ui/**"),
antMatcher("/v3/api-docs/**"),
antMatcher("/ws/**"),
antMatcher("/from/**"),
antMatcher("/to/**"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.swagger.v3.oas.annotations.Parameter;

@Parameter(hidden = true)
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface CurrentAccount {
Expand Down
3 changes: 2 additions & 1 deletion http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ Authorization: Bearer eyJhbGciOiJIUzM4NCJ9.eyJpZCI6MSwic3ViIjoidGVzdEBuYXZlci5jb
{
"title": "빙봉의 게임",
"maxPlayer": 2,
"roundCount": 5
"roundCount": 5,
"gameType": "SENTENCE"
}

### 조회
Expand Down
Loading