新聞中心
大家好, 我是指北君。

甘泉網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
相信大家平時開發(fā)的過程中,都會使用到 API文檔工具吧?大家都在使用什么呀?Java docs,I/O Docs, apiary.io, Docco, Dexy, Doxygen, TurnAPI,Swagger。今天我就來教大家如何使用 Swagger 搭建 API 文檔,并且配置權(quán)限使用。畢竟開發(fā)文檔還是內(nèi)容使用的為好,萬一上線到生產(chǎn)環(huán)境,沒有關(guān)swagger 又沒有設(shè)置權(quán)限,那可不GG啦。
好,我們這就上手搞起來。
我們將使用 Springfox 對 Swagger 2 規(guī)范的實現(xiàn),并通過 JWT 的方式來設(shè)置權(quán)限。
配置SwaggerUI
第一步:向Spring Boot項目添加Maven依賴項
打開 pom.xml 文件,添加 springfox-boot-starter 到 maven 依賴中。
io.springfox
springfox-boot-starter
3.0.0
添加 springfox-boot-starter 依賴后,spring boot 能啟動配置功能,配置好 swagger,所以我們不需要手動添加注解來啟用 Swagger。
我們啟動一下項目訪問 Swagger 文檔的 JSON API , 來看看 Springfox 是否正常運行。我們可以在瀏覽器中輸入以下URL:
http://localhost:8080/v3/api-docs
能夠看到以上的類似結(jié)果,說明我們第一步已經(jīng)成功了。
第二步:將 Swagger 2 集成到 Spring Boot項目中去
我們創(chuàng)建一個 SwaggerConfig 類,并用 @Configuration 注解來注釋。Swagger 的配置主要圍繞著 Docket 對象來完成。我們可以在 SwaggerConfig 類中添加以下代碼。
@Configuration
public class SwaggerConfiguration {
private ApiInfo apiInfo() {
return new ApiInfo("Blog REST APIs",
"REST APIs for Blog Application",
"1.0",
"Terms of service",
new Contact("xxx", "xxx", "xxx"),
"License of API",
"API license URL",
Collections.emptyList());
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
在構(gòu)造 Docket 對象之后,它的 select() 方法返回了 ApiSelectorBuilder 的一個實例,它提供了一種控制 Swagger 所暴露的端點的方法。
我們可以通過使用 RequestHandlerSelectors 和 PathSelectors 配置選擇 RequestHandlers 的路徑。如果兩者兩者使用 any() , 那就說明配置所有的 API 都能在 Swagger 上顯示了。
第三步:訪問 Swagger UI
Swagger UI 是一個內(nèi)置的解決方案,使用戶與Swagger 生成的API文檔的交互變得更加容易。我們在瀏覽器中輸入下面URL即可查看:
http://localhost:8080/swagger-ui/
結(jié)果應(yīng)該是這樣的。
好,到這里 Swagger 的使用配置就算結(jié)束了。那接下來我們來看看怎么用JWT增加權(quán)限配置呢?
配置 JWT
JWT 是什么?相信大家都一定聽過吧,它就是 JSON Web Token 的縮寫。話不多說,直接上手代碼配置起來。在 SwaggerConfig 里面新增代碼。
我們先來配置 ApiKey 作為 JWT 的認證 header信息:
public static final String AUTHORIZATION_HEADER = "Authorization";
private ApiKey apiKey(){
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
}
下一步,我們配置 JWT 的 SecurityContext , 對 SecurityContext 配置全局的 AuthorizationScope :
private SecurityContext securityContext(){
return SecurityContext.builder().securityReferences(defaultAuth()).build();
}
private List defaultAuth(){
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
}
然后我們配置 Docket 對象,對 Docket 對象設(shè)置 SecurityContext ,SecuritySchemes。
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.securityContexts(Arrays.asList(securityContext()))
.securitySchemes(Arrays.asList(apiKey()))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
到這里,JWT 就配置完成了,感覺是不是挺簡單的?好,我們再來運行一下,看看效果
我們點擊右上角的 Authorize 按鈕,彈出一個輸入 apiKey 的彈出層。
輸入 api key 之后,點擊 Authorize 認證通過,我們就又能調(diào)用 API 接口調(diào)試了。
用注解定制 Swagger API 文檔
為了能夠定制 Swagger 文檔,swagger-core 提供了一套注解來聲明和操作輸出。
Swagger-core 注解:
|
Name |
Description |
|
@Api |
標記為 Swagger 資源 |
|
@ApiModel |
標記為 Swagger 模型 |
|
@ApiModelProperty |
模型字段的屬性說明 |
|
@ApiOperation |
http接口的說明 |
|
@ApiParam |
http 接口參數(shù)說明 |
更多詳細的說明可以參考 GitHub 上的解釋:https://github.com/swagger-api/swagger-core/wiki/annotations
現(xiàn)在我們就舉個例子來解釋怎么使用這些個注解, 首先來看 @Api 和 @ApiOperation 怎么使用:
@Api(value = "CRUD Rest APIs for Post resources")
@RestController
@RequestMapping()
public class PostController {
@ApiOperation(value = "Get All Posts REST API")
@GetMapping("/api/v1/posts")
public PostResponse getAllPosts(
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
@RequestParam(value = "pageSize", defaultValue = "100", required = false) int pageSize
){
...
}
}
再來看看 @ApiModel 和 @ApiModelProperty 怎么使用:
@ApiModel(description = "Post model information")
@Data
public class PostDto {
@ApiModelProperty(value = "Blog post id")
private long id;
}
是不是感覺 so easy?
總結(jié)
通過這篇文章我們學習了如何通過 Springfox 來搭建 Swagger API 文檔平臺,然后也學會了如何設(shè)置 JWT 的方式做認證,保證 API 不會被別人能夠惡意使用。
網(wǎng)頁標題:三步輕松集成SwaggerAPI文檔
瀏覽地址:http://www.fisionsoft.com.cn/article/dpgiped.html


咨詢
建站咨詢
