栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot中Swagger2集成全局Token

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot中Swagger2集成全局Token

1. 添加依赖
		
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
2. 基础配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket getDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(swaggerDemoApiInfo())
                .select()
                //可以通过apis()方法设置哪个包中内容被扫描
                .apis(RequestHandlerSelectors.basePackage("com.lumashequ.cloudstorage.controller"))
                .paths(PathSelectors.any())
                .build());
    }

    private ApiInfo swaggerDemoApiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("撸码社区的swagger", "http://www.xxx.com", "xxxqq.com"))
                //文档标题
                .title("我的后台管理系统")
                //文档描述
                .description("管理项目")
                //文档版本
                .version("1.0.0")
                .build();
    }
}
3. 配置全局Token后的配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket getDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(swaggerDemoApiInfo())
                .select()
                //可以通过apis()方法设置哪个包中内容被扫描
                .apis(RequestHandlerSelectors.basePackage("com.lumashequ.cloudstorage.controller"))
                .paths(PathSelectors.any())
                .build()
                //  安全上下文
                .securityContexts(Arrays.asList(securityContexts()))
                .securitySchemes(unifiedAuth());
    }

    private ApiInfo swaggerDemoApiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("撸码社区的swagger", "http://www.xxx.com", "xxxqq.com"))
                //文档标题
                .title("我的后台管理系统")
                //文档描述
                .description("管理项目")
                //文档版本
                .version("1.0.0")
                .build();
    }
    
    // 添加配置全局token
    private static List unifiedAuth() {
        List arrayList = new ArrayList<>();
        arrayList.add(new ApiKey("token", "token", "header"));
        return arrayList;
    }

    private SecurityContext securityContexts() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.any())
                .build();
    }

    private List defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "全局token");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("token", authorizationScopes));
    }
}
4. 效果展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号