Home ๐Ÿ” Swagger ์‚ฌ์šฉํ•˜๊ธฐ
Post
Cancel

๐Ÿ” Swagger ์‚ฌ์šฉํ•˜๊ธฐ



  • Swagger
  • Rest Docs



Swagger ์ ‘์† URL

http://localhost:8080/swagger-ui/index.html



SwaggerConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.context.annotation.Configuration;

@OpenAPIDefinition(
		info = @Info(
				title = "Side-Project API",
				description = "์‚ฌ์ด๋“œํ”„๋กœ์ ํŠธ API ๋ช…์„ธ์„œ",
				version = "v1",
				contact = @Contact(
						name = "beginners",
						email = "sample@email.co.kr"
				)
		)
)

@Configuration
public class SwaggerConfig {

//GroupedOpenApi ์„ค์ •์„ ํ•˜๋ฉด ๊ทธ๋ฃน ์„ค์ •๋œ api ๋“ค๋งŒ ๋ฌธ์„œ์— ๋…ธ์ถœ
//    @Bean
//    public GroupedOpenApi sampleGroupOpenApi() {
//        String[] paths = {"/member/**"};
//
//        return GroupedOpenApi.builder().group("์ƒ˜ํ”Œ ๋ฉค๋ฒ„ API").pathsToMatch(paths)
//            .build();
//    }

}



SecurityConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@EnableWebSecurity
@Configuration
public class SecurityConfig {

  ...

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
		httpSecurity
				// token์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— csrf๋ฅผ disableํ•ฉ๋‹ˆ๋‹ค.
				.csrf().disable()

				.addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)

				.exceptionHandling()
				.authenticationEntryPoint(jwtAuthenticationEntryPoint)
				.accessDeniedHandler(jwtAccessDeniedHandler)

				// enable h2-console
				.and()
				.headers()
				.frameOptions()
				.sameOrigin()

				// ์„ธ์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— STATELESS๋กœ ์„ค์ •
				.and()
				.sessionManagement()
				.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

				.and()
				.authorizeHttpRequests()
				.requestMatchers(PathRequest.toH2Console()).permitAll()
				.requestMatchers( "/swagger-ui/**", "/v3/api-docs/**").permitAll() // Swagger ์ถ”๊ฐ€
				.anyRequest().authenticated()

				.and()
				.apply(new JwtSecurityConfig(tokenProvider));

		return httpSecurity.build();
	}
}



MemberController

1
2
3
4
5
6
7
8
9
@Tag(name = "๋ฉค๋ฒ„ ์ปจํŠธ๋กค๋Ÿฌ")  // Swagger
@RequiredArgsConstructor
@RestController
public class MemberController {
	
  ...
  
}



LoginReqDto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LoginReqDto {

  @Schema(description = "์œ ์ €์ด๋ฉ”์ผ", example = "test123@gmail.com") // Swagger
  @NotNull
  private String email;

  @Schema(description = "ํŒจ์Šค์›Œ๋“œ", example = "test123!") // Swagger
  @NotNull
  private String password;
}



แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-04-24 แ„‹แ…ฉแ„’แ…ฎ 10 07 07



This post is licensed under CC BY 4.0 by the author.

๐Ÿ” yaml ํŒŒ์ผ logging ์„ค์ •

๐Ÿ” AWS S3 ์„ค์ •ํ•˜๊ธฐ