? ? ? ?swagger可以在線生成接口文檔,便于前后端溝通,而且還可以在線調用接口,方便后臺調試。但是接口需要經過登錄校驗,部分接口還需要得到登錄token,使用token識別用戶身份進行后續操作。這種情況下,都需要接口增加header,好攜帶認證信息。
? ? ? ? swagger2和swagger3有很大不同,平臺選用的是swagger3。網上很多解決方案都是swagger2的,采用時一定看好自己的版本。無論2或3基本都是改配置類:
? ? ? ? 第一步?new Docket這里要加上? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? .securitySchemes(securitySchemes())
? ? ? ? ? ? ? ? .securityContexts(securityContexts());
? ? ? ? ? ? ? ? 其中:
? ? private List<SecurityScheme> securitySchemes() {
? ? ? ? //設置請求頭信息
? ? ? ? List<SecurityScheme> result = new ArrayList<>();
? ? ? ? ApiKey apiKey = new ApiKey("qlm-jwt", "qlm-jwt", "header");
? ? ? ? result.add(apiKey);
? ? ? ? return result;
? ? }
? ? private List<SecurityContext> securityContexts() {
? ? ? ? //設置需要登錄認證的路徑
? ? ? ? List<SecurityContext> result = new ArrayList<>();
? ? ? ? result.add(buildContext("/.*"));
? ? ? ? return result;
? ? }
? ? ? ? ?第二步:啟動真正的前端進行登錄,在application中找到token
? ? ? ? ?第三步:打開swagger_ui界面,會發現右上角多了個授權的按鈕。打開錄入上面的token
? ? ? ? ?第四步:在線測試接口。發現可以通過接口的登錄驗證了
? ? ?