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

댓글 작성 기능 구현 #151

Merged
merged 16 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion backend/src/main/java/com/votogether/config/OpenAPIConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.votogether.config;

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.security.SecurityScheme.In;
import io.swagger.v3.oas.models.security.SecurityScheme.Type;
import io.swagger.v3.oas.models.servers.Server;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -37,9 +42,22 @@ public OpenAPI openAPI() {
.version("v1.0.0")
.description("보투게더 API");

final SecurityScheme securityScheme = new SecurityScheme()
.type(Type.HTTP)
.in(In.HEADER)
.name("Authorization")
.scheme("bearer")
.bearerFormat("JWT")
.description("Bearer JWT");

final SecurityRequirement securityRequirement = new SecurityRequirement()
.addList("bearerAuth");
Comment on lines +45 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q
신기한 기능이네요!
swagger에 토큰을 적용해볼 수 있는 코드라고 생각하면 될까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞습니다 :) 해당 기능을 사용하면 Swagger 전역적으로 토큰을 적용할 수 있습니다. 현재는 Bearer 토큰을 설정해두었는데, 설정을 수정하면 Basic 토큰도 사용이 가능합니다!


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q
이러한 설정을 따로 안해주면 어떤 문제가 생기는건가요??

Copy link
Collaborator Author

@woo-chang woo-chang Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 궁금해서 직접 실험해 보았습니다!

스크린샷 2023-07-28 오전 11 17 32

위의 사진은 SecurityRequirement를 적용하기 전이고, 아래의 사진은 적용한 후의 모습입니다.

스크린샷 2023-07-28 오전 11 18 06

각 API 마다 Security를 설정할 수 있게 변한 것 같아요 :) 전역적으로 설정할 수 있는 버튼이 최상단에 있어서 있어도, 없어도 문제는 없을 것 같아요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 직접 실험까지! 감사합니다

return new OpenAPI()
.info(info)
.servers(List.of(devServer, prodServer));
.servers(List.of(devServer, prodServer))
.components(new Components().addSecuritySchemes("bearerAuth", securityScheme))
.security(List.of(securityRequirement));
}

}
2 changes: 2 additions & 0 deletions backend/src/main/java/com/votogether/global/jwt/Auth.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.votogether.global.jwt;

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

@Hidden
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q
이건 어떤 역할을 하는 어노테이션인가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 어노테이션은 스웨거에서 사용되는 어노테이션으로 @Hidden이 붙은 어노테이션은 Swagger에서 보이지 않게 됩니다!

스크린샷 2023-07-27 오후 9 52 04

붙이지 않고 사용하면 일반적인 요청에도 해당 멤버의 값들이 입력받아야 하는 값으로 인식해 Request에 그대로 노출되게 됩니다. 저희 프로젝트에서 Auth가 붙은 매개변수는 ArgumentResolver가 처리해서 넘겨주는 값이기에 요청 시 보여주지 않아도 된다고 판단하였습니다. 따라서 보이지 않도록 설정하였습니다 :)

어노테이션을 붙이면 아래와 같이 보이게 됩니다.

스크린샷 2023-07-27 오후 9 55 03

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Auth {
Expand Down