SpringBoot/Security下集成swagger遇到Unable to infer base url错误解决

2022-03-29 693点热度 0人点赞 0条评论

问题描述

如题,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;
    }

}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论