swagger接口需要权限验证解决方案

简介: 当我们在使用swagger的情况下,经常会遇到需要授权或者请求带有token才可以访问接口,这里我们就是解决授权问题。

背景

当我们在使用swagger的情况下,经常会遇到需要授权或者请求带有token才可以访问接口,这里我们就是解决授权问题。

废话不多说,我们直接给出解决方案,具体代码如下:

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.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
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;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置类,该类里面的应该是固定的,主要用来设置文档的主题信息,比如文档的大标题,副标题,公司名
 *  * 等
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
   
   
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */
    @Bean
    public Docket createRestApi(){
   
   
        //版本类型是swagger2
        return new Docket(DocumentationType.SWAGGER_2)
                //通过调用自定义方法apiInfo,获得文档的主要信息
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aaa.www.module.controller"))//扫描该包下面的API注解
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }
    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
   
   
        return new ApiInfoBuilder()
                .title("后台管理系统api") //接口管理文档首页显示
                .description("api信息")//API的描述
                .termsOfServiceUrl("www.aaa.com")//网站url等
                .version("1.0")
                .build();
    }
    private List<ApiKey> securitySchemes() {
   
   
        List<ApiKey> apiKeyList= new ArrayList();
        apiKeyList.add(new ApiKey("token", "token", "header"));
        return apiKeyList;
    }

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

    List<SecurityReference> defaultAuth() {
   
   
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences=new ArrayList<>();
        securityReferences.add(new SecurityReference("token", authorizationScopes));
        return securityReferences;
    }
}

注意截图中圈出,要和自己请求头中的字段名一致

image.png

配置完成后,在右上角会出现 Authorize按钮,如下图

image.png

目录
相关文章
|
6月前
|
移动开发 Java API
微服务技术系列教程(26) - SpringCloud- 接口管理Swagger
微服务技术系列教程(26) - SpringCloud- 接口管理Swagger
80 0
|
7月前
|
安全 Java API
解决 Swagger API 未授权访问漏洞:完善分析与解决方案
Swagger 是一个用于设计、构建、文档化和使用 RESTful 风格的 Web 服务的开源软件框架。它通过提供一个交互式文档页面,让开发者可以更方便地查看和测试 API 接口。然而,在一些情况下,未经授权的访问可能会导致安全漏洞。本文将介绍如何解决 Swagger API 未授权访问漏洞问题。
|
7月前
|
JSON Java 数据格式
【异常处理】关于访问swagger-ui报错java.lang.NumberFormatException: For input string: ““的解决方案总结
【异常处理】关于访问swagger-ui报错java.lang.NumberFormatException: For input string: ““的解决方案总结
154 0
|
16小时前
|
开发框架 JSON .NET
初学者不会写接口怎么办?微软Visual Studio 2022无脑式API接口创建——Swagger一键导入APIKit快速测试
初学者不会写接口怎么办?微软Visual Studio 2022无脑式API接口创建——Swagger一键导入APIKit快速测试
95 0
|
17小时前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
|
7月前
使用Swagger 让某些接口不显示在文档
使用Swagger 让某些接口不显示在文档
36 0
|
8月前
|
搜索推荐 Java 测试技术
Swagger与knife4j接口测试工具
Swagger与knife4j接口测试工具
110 0
|
9月前
|
Java 程序员 API
SpringBoot项目使用Swagger2接口工具
使用RESTful服务通常是涉及到多个终端的团队,比如Android、iOS、WEB等。为了让大家沟通顺畅,通常我们需要编写一份详细的RESTful业务接口文档
76 0
|
11月前
|
XML JSON Java
【Spring Boot】Swagger接口分组及细分排序问题详解
【Spring Boot】Swagger接口分组及细分排序问题详解
500 0
【Spring Boot】Swagger接口分组及细分排序问题详解