Springboot 集成Shiro后 引入Swagger2被拦截的解决方案

Springboot 集成Shiro后 引入Swagger2被拦截的解决方案

1.引入Shiro和Swagger2的依赖

        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- swagger ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- swagger2增强版接口文档 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
		<!--shiro权限管理-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-web-starter</artifactId>
            <version>1.8.0</version>
        </dependency>

2.配置对应配置类

2.1Swagger2配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){

        //添加head参数start
        List<Parameter> pars = new ArrayList<>();

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxxx.controller"))//这里设置要扫描的包名
                .build()
                .globalOperationParameters(pars);

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("xxx-API文档")
                .description("本文档描述了zzz接口定义")
                .version("1.0")
                .contact(new Contact("name", "url", "email"))//文档联系人配置
                .build();
    }

}

2.2 shiro配置拦截处理

		//一定使用LinkedHashMap!!!!
        Map<String, String> filterRuleMap = new LinkedHashMap<>();
        // 放行swagger2相关访问
        filterRuleMap.put("/swagger-ui.html", "anon");
        filterRuleMap.put("/swagger/**", "anon");
        filterRuleMap.put("/swagger-resources/**", "anon");
        filterRuleMap.put("/v2/**", "anon");
        filterRuleMap.put("/webjars/**", "anon");
        filterRuleMap.put("/configuration/**", "anon");

3.运行效果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值