Swagger文档在SpringBoot框架下的配置,Swagger配置登录验证

  1. Swagger文档作为后端接口调用重要的工具之一,下面是文档调用的详细代码
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
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;

/**
 * Swagger 接口文档配置类
 *
 * @author
 * @date 2023/6/17
 */

@EnableSwaggerBootstrapUI
@EnableSwagger2
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {

    /**
     * 配置扫描的api控制包路径
     * 这里配置的是你后端框架controller类对应的路径
     */
    private static final String BASE_PACKAGE = "xxxxxxxxxxxxxxx.controller";

    /**
     * 服务器路径
     * 代码部署服务的路径,选添,可以不写
     */
    private static final String SERVICE_URL = "xxxxxxxxx";

    /**
     * 配置swagger文档
     *
     * @return docket 对象
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()

                // 设置扫描基础包
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))

                // 扫描使用Api注解的控制器
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()

                //添加登录认证
                //这里下面两行代码已经相关下面包可以注释掉,这里是配置的Swagger文档登录验证,需要在application.yml文件中进行相关的配置,配置信息见后面详细情况
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    @Bean
    public RequestMappingInfoHandlerMapping requestMapping() {
        return new RequestMappingHandlerMapping();
    }

    /**
     * 配置 api 文档信息
     *
     * @return 文档信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()

                // 页面标题(自由发挥)
                .title("xxxxx")

                // API描述
                .description("自由发挥")

                // 创建路径
                .termsOfServiceUrl(SERVICE_URL)
                .version("1.0.0")
                .build();
    }

    /**
     * 解决 swagger 静态资源 404问题
     *
     * @param registry 资源路径
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 解决swagger-bootstrap 无法访问
        registry.addResourceHandler("doc.html").
                addResourceLocations("classpath:/META-INF/resources/");
    }

    private List<ApiKey> securitySchemes() {
        //设置请求头信息
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        //设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();

        // 所有请求路径
        result.add(getContextByPath("/.*"));
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

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

}

  1. application.yml进行swagger配置信息
## 开启 Swagger的 Basic认证功能,默认是false
swagger:
  # 是否关闭 swagger接口文档访问
  #  production: true
  basic:
    # 开启简单用户验证
    enable: true
    # 用户名(自由发挥)
    username: xx
    # 用户密码(自由发挥)
    password: xx
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
技术选型 #### 后端技术 | 技术 | 说明 | 官网 | | -------------------- | ------------------- | ------------------------------------------------------------ | | SpringBoot | 容器+MVC框架 | [https://spring.io/projects/spring-boot](https://spring.io/projects/spring-boot) | | Shiro | 认证和授权框架 | [http://shiro.apache.org/](http://shiro.apache.org/) | | MyBatis-Plus | ORM框架 | [https://mp.baomidou.com/l](https://mp.baomidou.com/l) | | PageHelper | MyBatis物理分页插件 | [http://git.oschina.net/free/Mybatis_PageHelper](http://git.oschina.net/free/Mybatis_PageHelper) | | Swagger-UI | 文档生产工具 | [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui) | | Hibernator-Validator | 验证框架 | [http://hibernate.org/validator/](http://hibernate.org/validator/) | | RabbitMq | 消息队列 | [https://www.rabbitmq.com/](https://www.rabbitmq.com/) | | Redis | 分布式缓存 | [https://redis.io/](https://redis.io/) | | Docker | 应用容器引擎 | [https://www.docker.com/](https://www.docker.com/) | | Druid | 数据库连接池 | [https://github.com/alibaba/druid](https://github.com/alibaba/druid) | | JWT | JWT登录支持 | [https://github.com/jwtk/jjwt](https://github.com/jwtk/jjwt) | | Lombok | 简化对象封装工具 | [https://github.com/rzwitserloot/lombok](https://github.com/rzwitserloot/lombok) | #### 前端技术 | 技术 | 说明 | 官网 | | ---------- | --------------------- | ------------------------------ ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
【项目说明】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载食用体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源介绍】 基于SpringBoot+MyBatis实现的外卖点餐系统源码+sql数据库+说明文档.zip 该项目是一个外卖点餐系统,基于 Spring Boot + MyBatis 实现,使用了 Vue.js 和 Element UI 前端框架。它包括用户端和商家端两个模块,用户可以在用户端浏览商家菜单,选择商品下单,而商家可以在商家端管理订单、菜单等。 ## 主要功能: ### 用户端 注册、登录 查看商家列表、菜单 购物车管理、订单管理 ### 商家端 注册、登录 菜品管理、订单管理、配送管理 技术栈: ## 后端 Spring Boot:构建后端基础框架 MyBatis:ORM 框架,实现与数据库的交互 MySQL:数据库 Swagger:API 文档生成工具 ## 前端 Vue.js:构建前端单页面应用 Element UI:基于 Vue.js 的前端 UI 框架 Axios:前端与后端的交互工具
SuperBoot框架是基于SpringCloud、SpringBoot敏捷开发框架框架开发初衷是为了方便快速开发项目,无需关心基础代码的编写,可以更专注于业务本身。框架实现基于JWT Token授权验证,实现单点登录SSO,服务鉴权,实现Redis数据缓存,在保证数据一致性的前提下提高接口响应速度。无缝集成MongoDB数据库,提供对非结构型数据存储,解决关系型数据库瓶颈问题。集成swagger框架,实现自动API测试及调试功能,解决程序员最反感的编写技术文档的问题。数据源基于Druid,提供更高性能及SQL监控。框架提供统一异常处理,统一响应结果,增加对JPA、Mongo的AOP拦截,由Snowflake ID自动生成赋值主键,数据实体无需开发均可由Idea自动生成。增删改查默认基于方法名称即可实现,无需写具体SQL。查询支持JPA、QueryDSL、Mybatis等方式。支持数据库读写分离,Feign增加服务直接的安全调用。v01版本是基础学习,主分支基于目前线上功能拆出来的功能模块,实现开箱即用。 ## 项目结构 ``` lua super-boot ├── client-config -- 项目配置文件信息,业务模块通过配置中心读取自动配置服务 ├── project_info -- 项目相关信息包含数据字典、SQL语句、工具等 ├── super-boot-utils -- 项目公用工具模块 ├── super-boot-base -- 项目公用常量模块 ├── super-boot-global -- 项目公用全局模块 ├── super-boot-registry -- 注册中心 ├── super-boot-config -- 配置中心 ├── super-boot-gateway -- 网关中心 ├── super-boot-user -- 用户中心 ``` ##

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无心 码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值