• 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
  • 相关阅读:
    题目:2695.包装数组
    建立一种特殊的字典访问不存在的键k时可以自动创建键k同时给k赋初值defaultdict()
    Nginx+Tomcat 实现反向代理
    css实现一个炫酷动画loading(四)
    最近身边一个技术负责人裸辞了...
    【毕业设计源码】Python学生兼职平台系统
    30天刷题计划(三)
    什么是 Cooke、Session 和 Token?
    网络安全深入学习第八课——代理与端口转发
    java中的interface(接口)
  • 原文地址:https://blog.csdn.net/weixin_45707639/article/details/126775358