依赖

        <dependency>
            <groupId&gt;io.springfox</groupId&gt;
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

配置

application.yaml

spring:
  # mvc这部分解swagger3在新版本Springboot中无法展示问题
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

java实现配置

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()// 自行修改自己接口路径,不配置扫描全部路径
                .apis(RequestHandlerSelectors.basePackage("com.itheima.controller"))
                .paths(PathSelectors.any()).build()//;
                .securitySchemes(securitySchemes())
                .securityContexts(Collections.singletonList(securityContext()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("FH Admin Swagger3 RESTful API")     // 页面标题
                .description("fhadmin.org")                 // 描述
                .contact(new Contact("张三丰", "http://sanfen.com", "150@163.com"))
                .version("3.0")                             // 版本号
                .license("The apache license")
                .licenseUrl("http://www.apache.com.org")
                .build();
    }

    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        //注意这里对应登录token鉴权对应k-v
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /*** 这里设置 swagger2 认证安全上下文*/
    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(Collections.singletonList(new SecurityReference("Authorization", scopes()))).build();
    }

    /*** 这里是写允许认证scope*/
    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{new AuthorizationScope("web", "All scope is trusted!")};
    }

}

拦截配置

/**
 * 说明:Swagger 拦截配置
 * 作者:FH Admin
 * from fhadmin.org
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }

}

原文地址:https://blog.csdn.net/sixpp/article/details/134748688

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_46946.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注