WebMvcConfigurer 配置swagger的权限

 

 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public AuthorityFilter getCheckFilter(){
        return  new AuthorityFilter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(getCheckFilter()).addPathPatterns("/**")
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/null/**")
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/swagger/**")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/v2/**")
//                .excludePathPatterns("/**/*.js")
//                .excludePathPatterns("/**/*.css")
//                .excludePathPatterns("/**/*.png")
        ;
    }

    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        //super(registry);

    }

//    @Override
//    public void configureViewResolvers(ViewResolverRegistry registry) {
//        System.out.printf("configureViewResolvers");
//        registry.jsp("/WEB-INF/",".jsp");
//        //super.configureViewResolvers(registry);
//        ViewResolver viewResolver=new UrlBasedViewResolver();
//        registry.viewResolver(viewResolver);
//
//    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController(
                "/null/api-docs",
                "/api-docs").setKeepQueryParams(true);
        registry.addRedirectViewController(
                "/null/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/ui");
        registry.addRedirectViewController(
                "/null/swagger-resources/configuration/security",
                "/swagger-resources/configuration/security");
        registry.addRedirectViewController(
                "/null/swagger-resources",
                "/swagger-resources");
        //WebMvcConfigurer.super.addViewControllers(registry);
    }
}

 

swagger根据环境进行启动

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    ;
    @Bean
    public Docket webApiConfig(Environment environment){
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(webApiInfo())
                //.groupName("icar-web")
                .enable(flag)
                .select()
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                //.title("icar-web")
                .description("API接口文档")
                .version("1.0")
                .build();
    }
}

 

posted @ 2022-12-28 22:48  ☆♂安♀★  阅读(756)  评论(0编辑  收藏  举报