Swagger在配置类中写法
启动项目直接访问 ip:端口号/路径
比如:
Swagge在线接口文档访问路径: ip:端口号/swagger-ui/index.html
Knife4j在线接口文档访问路径: ip:端口号/doc.html
springboot版本太高的话可能会导致不兼容(2.6以上)
依赖
<!--swagger2 依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<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>
<!--如果SpringBoot版本大于2.3则需要引入下面依赖-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
配置类
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;
@Configuration //配置类
//@EnableSwagger2 //swagger注解
@EnableOpenApi //开启swagger注解
@EnableKnife4j
public class SwaggerConfig {
@Bean
public Docket createDocket(){
//添加摘要信息
ApiInfoBuilder apiInfoBuilder=new ApiInfoBuilder();
ApiInfo apiInfo = apiInfoBuilder
//设置标题
.title("在线接口文档")
//描述
.description("API在线接口文档管理")
//版本
.version("1.0.0")
//作者信息
.contact(new Contact("admin", null, null))
.build();
return new Docket(DocumentationType.OAS_30)
//是否启用Swagger
.enable(true)
//用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
.apiInfo(apiInfo)
//设置哪些接口暴露给Swagger展示
.select()
// 扫描所有
.apis(RequestHandlerSelectors.any())
//扫描指定包中的swagger注解
//.apis(RequestHandlerSelectors.basePackage("com.csdn.controller"))
//扫描所有有注解的api,用这种方式更灵活
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
.paths(PathSelectors.any()).build();
}
}
Spring Security 安全框架需要放行路径
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//关闭csrf
.csrf().disable()
//不通过Session获取SecurityContext
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// 对于注册和登录接口 允许匿名访问
.antMatchers("/login","/register").anonymous()
//放行swagger3、Knife4j
// .antMatchers("/swagger-ui.html","/swagger-ui/index.html","/swagger-resources/**","/swagger-ui/**","/webjars/**","/v3/**","/api/**").permitAll()
.antMatchers("/swagger-ui/**", "/swagger-resources/**", "/v3/**",
"/doc.html","/webjars/**","/favicon.ico", "/img.icons/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated();
//把token校验过滤器添加到过滤器链中
http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
//配置异常处理器
http.exceptionHandling()
//配置登陆失败处理器
.authenticationEntryPoint(authenticationEntryPoint);
//允许跨域
http.cors();
}