溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springboot整合knife4j與shiro的示例分析

發(fā)布時間:2021-07-28 14:58:52 來源:億速云 閱讀:211 作者:小新 欄目:開發(fā)技術

小編給大家分享一下Springboot整合knife4j與shiro的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

一、介紹knife4j

增強版本的Swagger 前端UI,取名knife4j是希望她能像一把匕首一樣小巧,輕量,并且功能強悍,更名也是希望把她做成一個為Swagger接口文檔服務的通用性解決方案,不僅僅只是專注于前端Ui前端。

二、Spring Boot 整合knife4j

第一步

在Maven中的pom.xml文件引入:

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <!--在引用時請在maven中央倉庫搜索最新版本號-->
    <version>2.0.4</version>
</dependency>

第二步

增加配置類,主要添加@Configuration、EnableSwagger2、@EnableKnife4j以及@Import(BeanValidatorPluginsConfiguration.class)注解:

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                //為當前包下controller生成API文檔
                .apis(RequestHandlerSelectors.basePackage("com.dream"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerUI")
                .description("mall-tiny")
                .contact("macro")
                .version("1.0")
                .build();
    }
    private List<ApiKey> securitySchemes() {
        //設置請求頭信息
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        result.add(apiKey);
        return result;
    }
    private List<SecurityContext> securityContexts() {
        //設置需要登錄認證的路徑
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/misty/.*"));
        return result;
    }
    private SecurityContext getContextByPath(String pathRegex){
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
}

第三步

如果項目中沒有使用shiro、SpringSecurity 等權限框架,可以訪問,如下地址:

http://localhost:8080/doc.html

第四步

如果使用了權限框架,如shiro、SpringSecurity,需要添加配置:

1、實現WebMvcConfigurer
@SpringBootApplication
public class SwaggerBootstrapUiDemoApplication  implements WebMvcConfigurer{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath*:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/");
    }
}

注意: 樓主在這里遇到一個很大的坑,就是如果我使用classpath*:,會一直報錯;修改為classpath后,恢復正常。

2、樓主用的shiro,需要配置,放開相應的路徑:
@Bean
protected ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
    chainDefinition.addPathDefinition("/doc.html", "anon");
    chainDefinition.addPathDefinition("/webjars/**/**", "anon");
    return chainDefinition;
}

第五步,展示結果:

首頁

Springboot整合knife4j與shiro的示例分析

實體頁

Springboot整合knife4j與shiro的示例分析

knife4j 的官網地址

補充一點知識:

classpath和classpath*區(qū)別:

  • classpath:默認只會在你項目的class路徑中查找文件。

  • classpath*:默認不僅包含class路徑,還包括jar文件中(class路徑)進行查找。

  • 注意:

  • 使用classpath*:Spring需要遍歷所有的classpath,所以加載速度是很慢的;故在設計中,應該盡可能劃分好資源文件所在的路徑,盡量避免使用classpath*。

classpath*的使用:

  • 當項目中有多個classpath路徑,并同時加載多個classpath路徑下(此種情況多數不會遇到)的文件,就發(fā)揮了作用,如果不加,則表示僅僅加載第一個classpath路徑。

以上是“Springboot整合knife4j與shiro的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI