게시글 작성 API에 Swagger를 적용하였다.
swagger 는 spring boot로 api 서버를 만들때 소통에있어 도움을 줄수 있는 api 명세서 제공하는 OAS이다.
처음 swagger 의 2.x.x 버전을 dependency 에 추가 했지만 Spring boot 3.0.0 이상부터는 springdoc-openapi-ui 라이브러리를 사용해야된다.
로그인 api에 spring security 를 적용하였고 그부분을 주석처리후 2.2.0 버전의 종속성으로 주입하였다.
springdoc-openapi-ui 라이브러리를 사용하면 SwaggerConfig 설정 파일을 추가 하지 않아도 작동한다.
localhost:swagger-ui/index.html
dependencies {
implementation 'org.json:json:20190722'
// Security
//implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
}
라이브러리 주입후에도 서버에서 HTTP ERROR 403 에러가 발생하여 접근 자체가 불가하였다.
때문에 Spring security 설정파일인 WebSecurityConfig 안에서 api 인가쪽 부분을 살펴보았다.
해당 url localhost:swagger-ui/index.html 을 인가해주어야 하기 때문에 Spring v3 에 해당 부분을 추가 접근허가해 주었다.
.requestMatchers("/v3/api-docs/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
.requestMatchers("/swagger-ui/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// CSRF 설정
http.csrf((csrf) -> csrf.disable());
// 기본 설정인 Session 방식은 사용하지 않고 JWT 방식을 사용하기 위한 설정
http.sessionManagement((sessionManagement) ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
);
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() // resources 접근 허용 설정
.requestMatchers("/api/auth/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
.requestMatchers("/v3/api-docs/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
.requestMatchers("/swagger-ui/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
// 조회 API는 비로그인 유저도 접근 가능.
.requestMatchers(HttpMethod.GET, "/api/post/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/posts").permitAll()
.anyRequest().authenticated() // 그 외 모든 요청 인증처리
);
...

https://stackoverflow.com/questions/74614369/how-to-run-swagger-3-on-spring-boot-3
https://devfunny.tistory.com/692