본문 바로가기

Trouble Shooting

[swagger] Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway.

💣 error log

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:


 

🐣 원인

spring boot security와 함께 swagger를 사용 중이라면 security 때문에 접근이 안되는 것이다.

 

🙃 해결 방법

  1. 기존 사용하던 SecurityConfig.java> configure(HttpSecurity http)에 아래 코드 추가
.antMatchers("/swagger-resources/**").permitAll()

 

2. 기존 사용하던 SecurityConfig.java> configure(WebSecurity http)에 아래 코드 추가

이건 위의 에러랑은 관련없긴한데, 넣지 않으면 로그인하라고 로그인 페이지로 돌려버리기 때문에 혹시 도움이 될까 해서 첨부합니다.

web.ignoring().antMatchers("/v2/api-docs",  "/configuration/ui",
										       "/swagger-resources", "/configuration/security",
												   "/swagger-ui.html", "/webjars/**","/swagger/**");

 

📜 SecurityConfig.java 전체 코드

참고하라고 첨부합니다.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.renocomms.task.filter.JwtAuthenticationFilter;
import com.renocomms.task.service.UserService;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	
	@Autowired
	UserService userService;

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/static/css/**, /static/js/**, *.ico");
		
		// swagger
    web.ignoring().antMatchers(
		    		 "/v2/api-docs",  "/configuration/ui",
             "/swagger-resources", "/configuration/security",
             "/swagger-ui.html", "/webjars/**","/swagger/**");
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		http.authorizeRequests()
			.antMatchers("/user/admin/**").access("hasAuthority('ADMIN')")
			.antMatchers("/user/info").access("hasAuthority('USER')")
			.antMatchers("/", "/user/signup", "/user/denied", "/user/logout/result").permitAll()
			.antMatchers("/swagger-resources/**").permitAll() // swagger
			.anyRequest().authenticated()
			.and()
			.formLogin().loginPage("/user/loginPage")
			.loginProcessingUrl("/login")
			.defaultSuccessUrl("/user/login/result")
			.permitAll()
			.and()
			.logout().logoutRequestMatcher(new AntPathRequestMatcher("/user/logout"))
			.logoutSuccessUrl("/user/logout/result")
			.invalidateHttpSession(true)
			.and()
			.exceptionHandling().accessDeniedPage("/user/denied")
			;
		
	}
	
	@Bean
	public JwtAuthenticationFilter jwtFilter() {
		return new JwtAuthenticationFilter();
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userService).passwordEncoder(userService.passwordEncoder());
	}
	
}

 

🥕 문제 해결 참조 출처

https://github.com/springfox/springfox/issues/2191