springboot整合swagger2携带token全局配置

导入依赖:

		
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

配置类:

package com.gkl1120.topicmq.conf;

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.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
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 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gkl1120.topicmq.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("1.0")
                        .contact(new Contact("gklTest","blog.csdn.net","[email protected]"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build())
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }


    private List securitySchemes() {
        List apiKeyList= new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

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

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

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author ctx_gao kailong
 * @date 2020/5/25 15:02
 */
@Data
@ApiModel
public class SendDTO {
    @ApiModelProperty(value = "路由键")
    private String key;
    @ApiModelProperty(value = "消息")
    private String msg;
}
package com.gkl1120.topicmq.controller;

import com.gkl1120.topicmq.dto.SendDTO;
import com.gkl1120.topicmq.service.Productr;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ctx_gao kailong
 * @date 2020/5/22 16:26
 */
@RestController
@RequestMapping("/mq")
@Api(tags = "主题模式mq相关接口")
public class MqController {
    @Autowired
    private Productr productr;

    @ApiOperation("发送mq消息")
    @PostMapping("/send")
    public String send(@RequestBody SendDTO sendDTO) {
        productr.send(sendDTO.getMsg() , sendDTO.getKey());
        return "ok";
    }

    @ApiOperation("发送mq消息1")
    @GetMapping("/send1")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msg", value = "消息", defaultValue = "haha"),
            @ApiImplicitParam(name = "key", value = "路由键", defaultValue = "test", required = true)
    })
    public String send1(String msg , String key) {
        productr.send(msg , key);
        return "ok";
    }
}

访问地址:
http://localhost:8080/swagger-ui.html(端口号与当前项目端口号一致)

你可能感兴趣的:(springboot整合swagger2携带token全局配置)