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í)體頁
補(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)文章
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-04springboot項(xiàng)目之相互依賴報錯問題(基于idea)
這篇文章主要介紹了springboot項(xiàng)目之相互依賴報錯問題(基于idea),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Spring+Mybatis動態(tài)切換數(shù)據(jù)源的方法
這篇文章主要為大家詳細(xì)介紹了Spring+Mybatis動態(tài)切換數(shù)據(jù)源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01JavaFX實(shí)現(xiàn)界面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了JavaFX實(shí)現(xiàn)界面跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例
正則表達(dá)式常用于字符串處理、表單驗(yàn)證等場合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下2022-06-06Spring中的SpringApplicationRunListener詳細(xì)解析
這篇文章主要介紹了Spring中的SpringApplicationRunListener詳細(xì)解析,SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口,在項(xiàng)目啟動過程的各個階段進(jìn)行事件的發(fā)布,需要的朋友可以參考下2023-11-11