SpringBoot結(jié)合Knife4j進(jìn)行API分組授權(quán)管理配置詳解
在現(xiàn)代的微服務(wù)架構(gòu)中,API 文檔和授權(quán)管理是不可或缺的一部分。Knife4j 是一個(gè)基于 Swagger 的增強(qiáng)解決方案,它提供了更豐富的功能和更好的用戶體驗(yàn)。本文將介紹如何在 Spring Boot 應(yīng)用中集成 Knife4j,并進(jìn)行 API 分組和授權(quán)管理配置。
環(huán)境準(zhǔn)備
首先,確保你的項(xiàng)目中已經(jīng)添加了 Knife4j 的依賴:
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.5.0</version> </dependency>
配置 Swagger
在 application.yml
文件中,你可以配置 Swagger 的相關(guān)屬性,包括 API 文檔的路徑、分組配置以及Knife4j的特定設(shè)置:
springdoc: swagger-ui: path: /swagger-ui.html tags-sorter: alpha operations-sorter: order api-docs: path: /v3/api-docs group-configs: - group: '通用模塊' paths-to-match: '/**' packages-to-scan: com.example.springboot.common.controller - group: '系統(tǒng)模塊' paths-to-match: '/**' packages-to-scan: com.example.springboot.system.controller - group: '用戶模塊' paths-to-match: '/**' packages-to-scan: com.example.springboot.user.controller knife4j: enable: true setting: language: zh_cn enable-footer-custom: true footer-custom-content: "Apache License 2.0 | Copyright xiao he like sleep" basic: enable: true username: username password: password
配置 Swagger OpenAPI
在 Spring Boot 應(yīng)用中,你需要定義一個(gè)配置類來配置 OpenAPI 的信息:
@Configuration public class SwaggerConfig { @Bean public OpenAPI springShopOpenAPI() { return new OpenAPI() .info(new Info().title("API管理系統(tǒng)") .contact(new Contact().name("xiaohelikesleep").email("2109664977@qq.com")) .description("API管理系統(tǒng)文檔") .version("v1.0") .license(new License().name("Apache 2.0").url("http://springdoc.org"))) .externalDocs(new ExternalDocumentation() .description("外部文檔") .url("https://springshop.wiki.github.org/docs")) .addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)) .components(new Components().addSecuritySchemes( HttpHeaders.AUTHORIZATION, new SecurityScheme() .name(HttpHeaders.AUTHORIZATION) .type(SecurityScheme.Type.HTTP) .scheme("Bearer") .in(SecurityScheme.In.HEADER) .bearerFormat("JWT") )); } }
自定義 Swagger UI 底部版權(quán)信息
通過 knife4j.setting.enable-footer-custom
屬性,你可以自定義 Swagger UI 底部的版權(quán)信息:
knife4j: enable: true setting: language: zh_cn enable-footer-custom: true footer-custom-content: "Apache License 2.0 | Copyright xiao he like sleep" basic: enable: true username: 1 password: 1
啟動(dòng)應(yīng)用
在 main
方法中,你可以記錄應(yīng)用啟動(dòng)的信息,包括應(yīng)用名稱、本地 URL 和文檔 URL:
public static void main(String[] args) { ConfigurableEnvironment env = SpringApplication.run(ApiApplication.class, args).getEnvironment(); String applicationName = env.getProperty("spring.application.name"); String serverPort = env.getProperty("server.port"); String serverIp = env.getProperty("ip"); log.info( "----------------------------------------------------------%n" + "Application: '%s' is running Success!%n" + "Local URL: http://{}:{}%n" + "Document: http://{}:{}/doc.html%n" + "----------------------------------------------------------", applicationName, serverIp, serverPort, serverIp, serverPort); }
運(yùn)行效果:
Swagger界面:
配置的另一種寫法
package com.example.order.config; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import org.springdoc.core.models.GroupedOpenApi; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Knife4jConfig { @Bean public OpenAPI openAPI() { return new OpenAPI().info(new Info() .title("訂單服務(wù)接口文檔") .version("1.0") .description("訂單服務(wù)相關(guān)接口")); } @Bean public GroupedOpenApi orderApi() { return GroupedOpenApi.builder() .group("訂單服務(wù)") .pathsToMatch("/order/**") .packagesToScan("com.example.order.controller") .build(); } }
到此這篇關(guān)于SpringBoot結(jié)合Knife4j進(jìn)行API分組授權(quán)管理配置詳解的文章就介紹到這了,更多相關(guān)SpringBoot Knife4j進(jìn)行API分組管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
透徹理解Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別
這篇文章主要介紹了Java中Synchronized(對(duì)象鎖)和Static Synchronized(類鎖)的區(qū)別,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧2018-05-05Spring框架學(xué)習(xí)之Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配的代碼
自動(dòng)裝配就是說,你不用手動(dòng)實(shí)現(xiàn)bean之間的組合關(guān)系,只要使用了@Autowired注解,程序就會(huì)自動(dòng)的注入這個(gè)需要的bean,前提是你的Spring容器有這個(gè)bean,這篇文章主要介紹了Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配,需要的朋友可以參考下2021-12-12Java利用 Exchanger 實(shí)現(xiàn)游戲中交換裝備
JDK 1.5 開始 JUC 包下提供的 Exchanger 類可用于兩個(gè)線程之間交換信息。下面我們就來看看Java是如何利用Exchanger一行代碼實(shí)現(xiàn)游戲中交換裝備的2021-09-09Java實(shí)現(xiàn)BASE64編碼和解碼的方法
本篇文章主要介紹了Java實(shí)現(xiàn)BASE64編碼和解碼的方法,BASE64編碼通常用于轉(zhuǎn)換二進(jìn)制數(shù)據(jù)為文本數(shù)據(jù),有需要的可以了解一下。2016-11-11idea創(chuàng)建springboot項(xiàng)目和springcloud項(xiàng)目的詳細(xì)教程
這篇文章主要介紹了idea創(chuàng)建springboot項(xiàng)目和springcloud項(xiàng)目方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10