1.导入配置添加Swagger3依赖与knife4j依赖
<!--添加Swagger3依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--knife4j(接口文档)-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2.Swagger3配置文件
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @author :jerry
* @date :Created in 2022/5/28 10:30
* @description:
* @version: V1.1
* *Swagger3API文档的配置
* knife4j文档地址(端口号根据自己项目配置): http://localhost:8081/doc.html#
* swagger文档地址(端口号根据自己项目配置):http://localhost:8081/swagger-ui/index.html#/
*/
@Configuration
@EnableOpenApi
@EnableKnife4j
public class Swagger3Config {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
// .groupName("webApi")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yl.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SwaggerUI接口文档")
.description("接口文档Swagger-Bootstrap版")
.termsOfServiceUrl("http://localhost:8081/swagger-ui/index.html#/")
.contact(new Contact("jerry","http://localhost:8081/doc.html#", "13258239832@163.com"))
.version("1.0")
.license("jerry")
.build();
}
}
3.修改SecurityConfig配置文件(放行静态资源)
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
private static final String[] URL_WHITELIST = {
"/favicon.ico",
"/webjars/**",
"/img.icons/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/swagger-ui/**",
"/doc.html",
};
protected void configure(HttpSecurity http) throws Exception {
*
*
// 配置拦截规则
.and()
.authorizeRequests()
.antMatchers(URL_WHITELIST).permitAll()
.anyRequest().authenticated()
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
}
}
4.yml配置(非必需配置)
#swagger文档开启/关闭
springfox:
documentation:
auto-startup: true
#knife4j
knife4j:
production: false # 开启/屏蔽文档资源
文档访问路径:
knife4j文档地址(端口号根据自己项目配置): http://localhost:8081/doc.html# swagger文档地址(端口号根据自己项目配置):http://localhost:8081/swagger-ui/index.html#/