问题描述
如题,SpringBoot/SpringCloud/SprintSecurity下集成swagger打开 http://loclahost:port/swagger-ui.html弹框提示如下错误:
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway.
问题解决
SpringSecurity需要增加swagger-ui相关url规则放行
pom.xml中增加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
配置文件修改:
@Configuration
@EnableSwagger2WebFlux
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration extends WebSecurityConfigurerAdapter {
private static final String[] AUTH_LIST = {
// -- swagger ui
"**/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
};
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers(AUTH_LIST).authenticated()
.and()
.httpBasic().authenticationEntryPoint(swaggerAuthenticationEntryPoint())
.and()
.csrf().disable();
}
@Bean
public BasicAuthenticationEntryPoint swaggerAuthenticationEntryPoint() {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName("Swagger Realm");
return entryPoint;
}
}
文章评论