zoukankan      html  css  js  c++  java
  • swagger菜单分级

    效果

    实现

    SwaggerAutoConfiguration里新增配置:

    package com.fxkj.common.config;
    
    import com.google.common.base.Predicates;
    import com.google.common.collect.Lists;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.*;
    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 javax.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Jackson
     */
    @Profile({"default", "dev", "test"})
    @Configuration
    @EnableConfigurationProperties({SwaggerProperties.class})
    @ConditionalOnProperty(prefix = "swagger",value = "enable")
    @EnableSwagger2
    public class SwaggerAutoConfiguration {
        @Resource
        private SecurityConfig securityConfig;
    
        @Bean
        public Docket swaggerSpringMvcPlugin(@Autowired SwaggerProperties swaggerConfigProps) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .enable(swaggerConfigProps.isEnable())
                    .useDefaultResponseMessages(false)
                    .apiInfo(apiInfo(swaggerConfigProps))
                    .securitySchemes(getSecuritySchemes())
                    .securityContexts(getSecurityContexts())
                    .select()
                    .paths(Predicates.not(PathSelectors.regex("/error.*")))
                    .build();
        }
    
        private ApiInfo apiInfo(SwaggerProperties properties) {
            return new ApiInfoBuilder()
                    .title(properties.getTitle())
                    .version(properties.getVersion())
                    .description(properties.getDescription())
                    .contact(new Contact(properties.getContactName(),
                            properties.getContactUrl(),
                            properties.getContactEmail()))
                    .license("Apache License Version 2.0")
                    .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                    .version("2.0")
                    .build();
        }
    
        @Bean
        public Docket api_system(@Autowired SwaggerProperties swaggerConfigProps) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfoSystem(swaggerConfigProps))
                    .securitySchemes(getSecuritySchemes())
                    .securityContexts(getSecurityContexts())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.ant("/system/**"))
                    .build()
                    .groupName("系统设置")
                    .pathMapping("/");
        }
    
        @Bean
        public Docket api_role(@Autowired SwaggerProperties swaggerConfigProps) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfoSystem(swaggerConfigProps))
                    .securitySchemes(getSecuritySchemes())
                    .securityContexts(getSecurityContexts())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.ant("/roles/**"))
                    .build()
                    .groupName("角色设置")
                    .pathMapping("/");
        }
    
        @Bean
        public Docket api_dict(@Autowired SwaggerProperties swaggerConfigProps) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfoSystem(swaggerConfigProps))
                    .securitySchemes(getSecuritySchemes())
                    .securityContexts(getSecurityContexts())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.ant("/dict/**"))
                    .build()
                    .groupName("字典维护")
                    .pathMapping("/");
        }
    
        private ApiInfo apiInfoSystem(SwaggerProperties properties) {
            return new ApiInfoBuilder()
                    .title("系统设置相关API")
                    .version(properties.getVersion())
                    .description(properties.getDescription())
                    .contact(new Contact(properties.getContactName(),
                            properties.getContactUrl(),
                            properties.getContactEmail()))
                    .license("Apache License Version 2.0")
                    .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
                    .version("2.0")
                    .build();
        }
    
        private ArrayList<ApiKey> getSecuritySchemes() {
            return Lists.newArrayList(
                    new ApiKey("token", "token", "header")
            );
        }
    
        private List<SecurityContext> getSecurityContexts() {
            AuthorizationScope[] scopes = {
                    new AuthorizationScope("global", "accessEverything")
            };
            List<SecurityReference> securityReferences = Lists.newArrayList(
                    new SecurityReference("token", scopes)
            );
            return Lists.newArrayList(SecurityContext.builder()
                    .securityReferences(securityReferences)
                    .forPaths(Predicates.not(Predicates.in(securityConfig.getIgnoreUrls())))
                    .build());
        }
    }
    

      

  • 相关阅读:
    使用CSS将图片转换成黑白(灰色、置灰)
    require.js
    Tortoisegit安装下载
    谷歌浏览器添加扩展程序
    IOS 照相问题
    java的interface和PHP的区别
    Object中的hashCode方法
    PHP、js、Java中的匿名函数、闭包、回调函数
    Java和PHP中的浅克隆和深克隆
    unicode字符编码中一些专业词汇的全称
  • 原文地址:https://www.cnblogs.com/csyzlm/p/14048231.html
Copyright © 2011-2022 走看看