swagger2-身份认证Authenticatio(二)ApiKey

https://blog.csdn.net/lanwp5302/article/details/101628732

这里使用spring boot 2.1.8 ,使用swagger-ui

一、引入maven依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

二、配置类

@Configuration
@EnableSwagger2
//@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .pathMapping("/")
                .select()
                // 指定当前包路径,这里就添加了两个包,注意方法变成了basePackage,中间加上成员变量splitor
                .apis(RequestHandlerSelectors.basePackage("com.example.lwp.swagger2.swagger2authdemo.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                ;
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger")
                .description("SpringBoot整合Swagger,详细信息......")
                .version("9.0")
                .contact(new Contact("张三", "blog.csdn.net", "zhangsan@gmail.com"))
                .contact(new Contact("李四", "blog.csdn.net", "zhangsan@gmail.com"))
//                        .license("The Apache License")
//                        .licenseUrl("http://www.baidu.com")
                .build();
    }

    /**
     * SecurityScheme 子类 BasicAuth OAuth ApiKey
     * @return
     */
    private List<SecurityScheme> securitySchemes(){
        List<SecurityScheme> list = new ArrayList<>();
        // basicAuth SwaggerBootstrapUI支持的不好,使用swagger原生UI
        list.add(new BasicAuth("basicAuth"));
        // name 为参数名  keyname是页面传值显示的 keyname, name在swagger鉴权中使用
        list.add(new ApiKey("access_token", "access_token", "header"));
        list.add(new ApiKey("query_token鉴权值-参数名称", "query_token", "query"));
        return list;
    }
}

三、controller

使用注解 @ApiOperation(value = “hello入口”, authorizations = {@Authorization(value=“access_token”)})
名称对应 access_token

...
@RestController
@RequestMapping("/auth")
@Api(tags = {"auth模块"}, description = "需要鉴权的模块(描述)") //
public class AuthController {

    @GetMapping("/hello")
    @ApiOperation(value = "hello入口", authorizations = {@Authorization(value="access_token")})
    public String hello(String username){
        return "hello " + username;
    }

    @GetMapping("/hello1")
    @ApiOperation(value = "auth hello1接口", authorizations = {@Authorization(value="basicAuth")})
    public String hello1(String username){
        return "hello1 " + username;
    }
    ...

四、swagger-ui

在这里插入图片描述
测试方法,锁图标显示关闭,ApiKey已经生效
在这里插入图片描述

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值