• SpringCloud下关于SWAGGER2的部署,包含JWT+GATEWAY鉴权


    BackGround-背景

    Recently, I am coding a Spring Cloud project, that I need to test something with Swagger.The problem is that I need to route and filter the request urls of Swagger in case of being block by JWT Auth & Gateway Custom Filter.

    最近,我在做一个微服务项目,需要用到Swagger。问题是我需要路由过滤请求,防止他被JWT和网关自定义过滤器给阻挡。

    Project Structure 项目结构

    There are 2 consumers modules and 1 gateway module in this project.

    这有2个消费者 和1个生产者。

    img

    Process-过程

    Add depencies of swagger2 & ui to commons module.Here I use version-2.9.2

    1. Add Maven dependencies of swagger2 & ui to commons module.Here I use version-2.9.2.

    在commons模块添加swagger2和ui的Maven依赖,这里我使用的版本是-2.9.2.

            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger2artifactId>
                <version>2.9.2version>
            dependency>
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger-uiartifactId>
                <version>2.9.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    img


    1. Create SwaggerConfig.java to admin & app-server modules.At the same time,don’t forget to add urls pattern of Swagger including its urls static urls to the interceptors to make it free to be visited without token as we make token interceptor for authorization.

    在admin和app-server模块下添加SwaggerConfig.java配置类。与此同时,由于我们做了token认证的拦截器,别忘记把swagger的访问的路径,包括他的静态文件路径到拦截器里排除掉,使他们能够被正常访问。

    img

    package com.tanhua.admin.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Description: SwaggerCongig配置
     * @Author: Spike Wong
     * @Date: 2022/9/8
     */
    @Slf4j
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
       
    
        private boolean SWAGGER_IS_ENABLE = true;
    
        /**
         * 创建文档接口信息
         *
         * @return
         */
        public ApiInfo createApiInfo() {
       
            return new ApiInfoBuilder()
                    .title("鸡你太美-交友Admin后端文档")
                    .description("陌生人交友")
                    .contact(new Contact("ikun探花交友论坛", "tanhua.com", "tanhua@tanhua.com"))
                    .version("1.0.0").build();
        }
    
        /**
         * ioc 容器 存储bean
         *
         * @return
         */
        @Bean
        public Docket createApi() {
       
            //加token的
            ParameterBuilder tokenPar = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<>();
            tokenPar.name("token").description("token")
                    .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
            pars.add(tokenPar.
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    Semantic Kernel入门系列:利用Handlebars创建Prompts functions
    史上最短的“牛熊转换”:BTC价格昨夜起飞,但却来自一条假新闻!
    Godot C# .gitignore配置
    finalize()方法什么时候被调用?析构函数(finalization)的目的是什么?
    verilog学习笔记(1)module实例化2
    软件测试的基本流程是什么?软件测试流程详细介绍
    编译[Bug]——too few arguments for template template parameter “Tuple“ detected
    win10家庭版安装Docker
    [Vue 配置] Vite + Vue3 项目配置 src目录别名为 @
    手把手APP抓包检测实战 - 某汽车APP
  • 原文地址:https://blog.csdn.net/weixin_45707639/article/details/126775358