【问题标题】:Swagger-ui + Spring Security => localhost:8080/swagger-ui.html <= Not Found 404Swagger-ui + Spring Security => localhost:8080/swagger-ui.html <= Not Found 404
【发布时间】:2025-12-04 00:40:01
【问题描述】:

我一直在为此进行一些投资,但我无处可去,这是我目前的情况:

1.我正在使用 Spring Boot + Spring Security,这是我的配置:

。 . .

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

private final PasswordEncoder passwordEncoder;
@Autowired
private UserRepository userRepository;

@Autowired
public WebSecurityConfig(PasswordEncoder passwordEncoder) {
    
    this.passwordEncoder = passwordEncoder;
};

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs",
                               "/configuration/ui",
                               "/swagger-resources/**",
                               "/configuration/security",
                               "/swagger-ui.html",
                               "/webjars/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .csrf().disable()
    .authorizeRequests(authorize -> {
        authorize
                .antMatchers("/h2-console/**").permitAll() //do not use in production!
                .antMatchers("/", "/webjars/**", "/login/**", "/resources/**", "/v2/api-docs",
                        "/configuration/ui",
                        "/swagger-resources/**",
                        "/configuration/security",
                        "/swagger-ui.html",
                        "/webjars/**").permitAll();
    } )
    .authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .httpBasic()
    .and().csrf().ignoringAntMatchers("/h2-console/**", "/api/**");
    http.headers().frameOptions().sameOrigin();
};

application.properties:

server.ssl.key-store: classpath:springboot.p12
server.ssl.key-store-password:password
server.ssl.key-store-type: pkcs12
server.ssl.key-alias: springboot
server.ssl.key-password: password

Swagger 配置:

@Configuration
@EnableSwagger2
public class SwaggerConfig { //} extends WebMvcConfigurationSupport {
@Bean
public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("enide.logicon.backend.controllers"))
            .paths(PathSelectors.any())
            .build()
            .apiInfo(metaData());
}

。 . .


  • 自己生成的 SSL 证书正在运行,生成以填充 “editor.swagger.io” https 请求需要 & 使 Basic Auth 更安​​全。

。 . .

  • 为什么没有找到 SWAGGER-UI.HTML 页面?这里有我的依赖项:

。 . .

 <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>

。 . .

**

  • 还允许所有路径,无需登录 antMatchers("/**").permitAll() 这允许“editor.swagger.io” 从 API 但不是 /swagger-ui.html 检索数据,有什么想法吗?

**

【问题讨论】:

    标签: spring spring-boot spring-security swagger


    【解决方案1】:

    嗯,不知道为什么我的问题是-1,答案就在那里。

    对我有用的是改变这个依赖项:

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
     </dependency>
    

    【讨论】:

      最近更新 更多