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.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>

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值