我已经尝试在我的spring启动项目中使用
实现swagger2pom.xml中的依赖项
<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>
在spring security忽略列表中添加资源
@Override
public void configure(WebSecurity web) throws Exception
{
web.ignoring().antMatchers("/v2/api-docs/**");
web.ignoring().antMatchers("/swagger.json");
web.ignoring().antMatchers("/swagger-ui.html");
web.ignoring().antMatchers("/swagger-resources/**");
web.ignoring().antMatchers("/webjars/**");
}
还尝试在重写的配置方法中添加此
.antMatchers("/swagger-ui.html").permitAll()
配置类:我已经尝试了我的Swagger配置类
implements WebMvcConfigurer
extends WebMvcConfigurationSupport
如其他答案所示
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Value("${spring.application.name}")
private String TITLE;
private static final String DESCRIPTION = "API Documentation";
/**
* Providing app information to swagger
* @return ApiInfo
*/
private ApiInfo apiInfo()
{
return new ApiInfoBuilder().title(TITLE)
.description(DESCRIPTION)
.build();
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
和这些属性文件
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
spring.main.allow-bean-definition-overriding=true
Access to localhost was denied
You don't have authorisation to view this page.
HTTP ERROR 403
有人可以告诉这个swagger配置错误吗?
您至少缺少一个无法访问的资源(configuration/ui)
,下面的操作可以解决这个问题。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**");
}
实际上,当我在pom中切换到以前版本的springfox时,这个问题已经解决了
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
注意:看起来springfox v3不稳定,当使用v3时,由于一些错误,应用程序无法启动,后来通过在线解决方案解决了这个问题-添加以下pathmaker属性
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
我在spring-boot v3中遇到了非常类似的问题。
在这种情况下,您需要使用:springdoc-openapi-starter-webmvc-ui,就像在文档介绍
中写的那样和使用安全配置,像这样:
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)
@RequiredArgsConstructor
public class SecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.authorizeHttpRequests(a -> a
.requestMatchers("/v3/**", "/swagger-ui/**").permitAll()
.anyRequest().authenticated()
).build();
}
}
Swagger UI: http://localhost:8080/swagger-ui/index.html
JSON: http://localhost: 8080/v3/api文档
yaml: http://localhost: 8080/v3/api-docs.yaml
如果您在localhost上运行应用程序:8080:)