欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot整合knife4j與shiro的操作

 更新時間:2021年07月28日 12:01:10   作者:坎布里奇  
這篇文章主要介紹了Springboot整合knife4j與shiro的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、介紹knife4j

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

二、Spring Boot 整合knife4j

第一步

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

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <!--在引用時請?jiān)趍aven中央倉庫搜索最新版本號-->
    <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()
                //為當(dāng)前包下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() {
        //設(shè)置請求頭信息
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
        result.add(apiKey);
        return result;
    }
    private List<SecurityContext> securityContexts() {
        //設(shè)置需要登錄認(rèn)證的路徑
        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;
    }
}

第三步

如果項(xiàng)目中沒有使用shiro、SpringSecurity 等權(quán)限框架,可以訪問,如下地址:

http://localhost:8080/doc.html

第四步

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

1、實(shí)現(xiàn)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后,恢復(fù)正常。

2、樓主用的shiro,需要配置,放開相應(yīng)的路徑:

@Bean
protected ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
    chainDefinition.addPathDefinition("/doc.html", "anon");
    chainDefinition.addPathDefinition("/webjars/**/**", "anon");
    return chainDefinition;
}

第五步,展示結(jié)果:

首頁

首頁

實(shí)體頁

在這里插入圖片描述

knife4j 的官網(wǎng)地址

補(bǔ)充一點(diǎn)知識:

classpath和classpath*區(qū)別:

  • classpath:默認(rèn)只會在你項(xiàng)目的class路徑中查找文件。
  • classpath*:默認(rèn)不僅包含class路徑,還包括jar文件中(class路徑)進(jìn)行查找。
  • 注意:
  • 使用classpath*:Spring需要遍歷所有的classpath,所以加載速度是很慢的;故在設(shè)計(jì)中,應(yīng)該盡可能劃分好資源文件所在的路徑,盡量避免使用classpath*。

classpath*的使用:

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

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳談@Autowired和static的關(guān)系

    詳談@Autowired和static的關(guān)系

    這篇文章主要介紹了@Autowired和static的關(guān)系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java 運(yùn)行報錯has been compiled by a more recent version of the Java Runtime

    java 運(yùn)行報錯has been compiled by a more recent version of the J

    java 運(yùn)行報錯has been compiled by a more recent version of the Java Runtime (class file version 54.0)
    2021-04-04
  • springboot項(xiàng)目之相互依賴報錯問題(基于idea)

    springboot項(xiàng)目之相互依賴報錯問題(基于idea)

    這篇文章主要介紹了springboot項(xiàng)目之相互依賴報錯問題(基于idea),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring Task定時任務(wù)的配置和使用詳解

    Spring Task定時任務(wù)的配置和使用詳解

    本篇文章主要介紹了Spring Task定時任務(wù)的配置和使用詳解,實(shí)例分析了Spring Task定時任務(wù)的配置和使用的技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-04-04
  • Spring+Mybatis動態(tài)切換數(shù)據(jù)源的方法

    Spring+Mybatis動態(tài)切換數(shù)據(jù)源的方法

    這篇文章主要為大家詳細(xì)介紹了Spring+Mybatis動態(tài)切換數(shù)據(jù)源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • JavaFX實(shí)現(xiàn)界面跳轉(zhuǎn)

    JavaFX實(shí)現(xiàn)界面跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了JavaFX實(shí)現(xiàn)界面跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例

    Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例

    正則表達(dá)式常用于字符串處理、表單驗(yàn)證等場合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • springboot?接收LocalDateTime方式

    springboot?接收LocalDateTime方式

    這篇文章主要介紹了springboot?接收LocalDateTime方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Spring中的SpringApplicationRunListener詳細(xì)解析

    Spring中的SpringApplicationRunListener詳細(xì)解析

    這篇文章主要介紹了Spring中的SpringApplicationRunListener詳細(xì)解析,SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口,在項(xiàng)目啟動過程的各個階段進(jìn)行事件的發(fā)布,需要的朋友可以參考下
    2023-11-11
  • 淺談Java中的private方法是否可以被代理

    淺談Java中的private方法是否可以被代理

    這篇文章主要介紹了淺談Java中的private方法是否可以被代理,在?Java?8之前,接口可以有常量變量和抽象方法,我們不能在接口中提供方法實(shí)現(xiàn),如果我們要提供抽象方法和非抽象方法(方法與實(shí)現(xiàn))的組合,那么我們就得使用抽象類,需要的朋友可以參考下
    2023-12-12

最新評論