springboot集成swagger-bootstrap-ui实现Authorize 功能

用Swagger测试接口的时候请求头需要携带token参数,swagger-bootstrap-ui就为我们提供了这样的功能。

swagger-bootstrap-ui 相对swagger使用更好点。

1、引入jar



   io.springfox
   springfox-swagger2
   2.9.2
   
       
           io.swagger
           swagger-annotations
       
       
           io.swagger
           swagger-models
       
   



 
      io.swagger
      swagger-annotations
      1.5.21
 
      

     io.swagger
     swagger-models
     1.5.21

    


   io.springfox
   springfox-swagger-ui
   2.9.2



   com.github.xiaoymin
   swagger-bootstrap-ui
   1.9.6

2、编写SwaggerConfig 配置类

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2的接口配置
 *
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig{
    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                // 是否启用Swagger ,true 开启, false 关闭
                .enable(true) 
               // .pathMapping("/test-api")
                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo())
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                /* 设置安全模式,swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * 安全模式,这里指定token通过Authorization头请求头传递
     */
    private List securitySchemes()
    {
        List apiKeyList = new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List securityContexts()
    {
        List securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List defaultAuth()
    {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("标题:xxxxxx接口API")
                // 描述
                .description("描述:xxxxAPI文档")
                // 作者信息
                .contact(new Contact(TEST, null, null))
                // 版本
                .version("版本号:2.2.0" )
                .build();
    }
}

3、在springboot启动类添加 两个注解 @EnableSwagger2  和 @EnableSwaggerBootstrapUI

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class RuoYiApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiApplication.class, args);
   
    }
}

4、在controller编写接口

RestController
@RequestMapping("/content/search")
@Api(value = "es测试接口", tags = {"es测试相关的接口"})
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "添加ES测试接口",notes = "添加ES测试接口",httpMethod = "GET")
    @GetMapping(value = "/add")
    public AjaxResult add( ){
        ItemBean itemBean = new ItemBean();
        itemBean.setId((long) 1233456789);
        itemBean.setTitle("Ik分词");
        itemBean.setCategory("文件");
        itemBean.setBrand("smart");
        itemBean.setPrice(1.1);
        itemBean.setImage("www.baidu.com");
        itemBean.setContent("正在学习 elasticsearch");
        itemService.insertData(itemBean);
        return  AjaxResult.success();
    }

 

效果图:
springboot集成swagger-bootstrap-ui实现Authorize 功能_第1张图片

你可能感兴趣的:(swagger)