개발/spring&springboot

Springboot+swagger 연동

이수민 2023. 10. 5. 13:54

의존성 설정

implementation "org.springframework.security:spring-security-test"
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

 

package com.cloudmaestro.global.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SpringConfig {

    private static final String[] WHITE_LIST = {
            "/**",
            "/api/v1/"
    };

    @Bean
    protected SecurityFilterChain config(HttpSecurity http) throws Exception {
        http
            .csrf(csrf -> csrf.disable())
            .headers(headers -> headers.frameOptions(frameOptions -> frameOptions.disable()))
            .authorizeHttpRequests(authorize -> authorize
            .requestMatchers(WHITE_LIST).permitAll()
            .requestMatchers("/v3/api-docs/**").permitAll()
            .requestMatchers("/swagger-ui/**").permitAll());
        return http.build();
    }

}

 

http://localhost:8080/swagger-ui/index.html 링크로 들어가면 된다.

안된다면 버전 문제일테니

localhost:8080/swagger-ui.html 로 들어가면된다.

스웨거 버전 2까지는 localhost:8080/swagger-ui.html 의 링크이며,

스웨거 버전 3부터는 http://localhost:8080/swagger-ui/index.html의 링크라고 한다.

 

 

참고한 tistory: https://ssoxong.github.io/spring/spring_8/