//Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
구글링 하면 springfox 머시기 넣으라고 할텐데, 현재 우리의 스프링부트 버전(3.0 이상)과 호환이 안 좋음
@Configuration
public class SwaggerConfig {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("UMC")
.pathsToMatch("/**")
.build();
}
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info()
.title("게시판 API")
.description("UMC-게시판")
.version("1.0.0"));
}
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
// CSRF 설정 Disable
http.csrf().disable()
.addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
// exception handling 할 때 우리가 만든 클래스를 추가
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.accessDeniedHandler(jwtAccessDeniedHandler)
.and()
.headers()
.frameOptions()
.disable()
// 시큐리티는 기본적으로 세션을 사용
// 여기서는 세션을 사용하지 않기 때문에 세션 설정을 Stateless 로 설정
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
// 로그인, 회원가입 API 는 토큰이 없는 상태에서 요청이 들어오기 때문에 permitAll 설정
.and()
.authorizeHttpRequests(authorize -> authorize
.requestMatchers(new MvcRequestMatcher(introspector,"/auth/**"),
new AntPathRequestMatcher("/h2-console/**"),
new AntPathRequestMatcher("/swagger-ui/**"),
new AntPathRequestMatcher("/swagger-resources/**"),
new AntPathRequestMatcher("/v3/api-docs/**"),
new AntPathRequestMatcher("/webjars/**")).permitAll()
.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults()) // 나머지 API 는 전부 인증 필요
// JwtFilter 를 addFilterBefore 로 등록했던 JwtSecurityConfig 클래스를 적용
.apply(new JwtSecurityConfig(tokenProvider));
return http.build();
}
사용자 입장에선 /swagger-ui 로만 요청을 보내지만 스웨거 내부적으로 서버에 또 여러 요청을 보내기 때문에 저렇게 해줌
브라우저에 “도메인(우리의 경우 localhost:8080)/swagger-ui” 치면 됨