欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

Swagger(十):Authorize

Authorize 功能是后端配置类似JWT等权限配置而设置的,可以全局配置token等参数

80_1.png

当后台通过代码设置后,UI会自动识别,生成Authorize菜单功能,供开发者填相关auth参数等

后台代码示例(SwaggerConfiguration.java):

@Bean(value = "groupRestApi")
@Order(value = 1)
public Docket groupRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(groupApiInfo())
        .groupName("分组接口")
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.swagger.bootstrap.ui.demo.group"))
        .paths(PathSelectors.any())
        .build().securityContexts(Lists.newArrayList(securityContext(),securityContext1())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(),apiKey1()));
}

private ApiKey apiKey() {
        return new ApiKey("BearerToken", "Authorization", "header");
}
private ApiKey apiKey1() {
    return new ApiKey("BearerToken1", "Authorization-x", "header");
}

private SecurityContext securityContext() {
    return SecurityContext.builder()
        .securityReferences(defaultAuth())
        .forPaths(PathSelectors.regex("/.*"))
        .build();
}
private SecurityContext securityContext1() {
    return SecurityContext.builder()
        .securityReferences(defaultAuth1())
        .forPaths(PathSelectors.regex("/.*"))
        .build();
}

来源:http://39sd.cn/66ADB

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 博客站点 » Swagger(十):Authorize

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏