Swagger REST API 문서 설정

jb kim·2022년 3월 13일
0

REST API 블로그 앱

목록 보기
57/65

SecurityConfig 설정

			.antMatchers("/api/auth/**").permitAll()
			.antMatchers("/v3/api-docs/**").permitAll()
			.antMatchers("/swagger-ui/**").permitAll()
			.antMatchers("/swagger-resources/**").permitAll()
			.antMatchers("/swagger-ui.html/**").permitAll()
			.antMatchers("/webjars/**").permitAll()

http://localhost:8080/swagger-ui/

문서 설정 (config 패키지)

@Configuration
public class SwaggerConfig {
	
	private ApiInfo apiInfo() {
		return new ApiInfo(
				"스프링 부트 Blog REST APIs",
				"스프링 부트 Blog REST API Document",
				"1",
				"Terms of service",
				new Contact("홍길동", "myblog.app", "drv98@naver.com"),
				"License of API",
				"API license URL",
				Collections.emptyList()
				);
	}
	
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any())
				.build();
	}
}

SecurityConfig 에 v3=> v2로 설정

.antMatchers("/v2/api-docs/**").permitAll()

SwaggerConfig

package com.blog.app.config;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration
public class SwaggerConfig {
	
    public static final String AUTHORIZATION_HEADER = "Authorization";

    private ApiKey apiKey(){
        return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
    }

	private ApiInfo apiInfo() {
		return new ApiInfo(
				"스프링 부트 Blog REST APIs",
				"스프링 부트 Blog REST API Document",
				"1",
				"Terms of service",
				new Contact("홍길동", "myblog.app", "drv98@naver.com"),
				"License of API",
				"API license URL",
				Collections.emptyList()
				);
	}
	
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
                .securityContexts(Arrays.asList(securityContext()))
                .securitySchemes(Arrays.asList(apiKey()))				
				.select()
				.apis(RequestHandlerSelectors.any())
				.paths(PathSelectors.any())
				.build();
	}
	
	private SecurityContext securityContext() {
		return SecurityContext.builder().securityReferences(defaultAuth()).build();
	}
	
    private List<SecurityReference> defaultAuth(){
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
    }
}

profile
픽서

0개의 댓글