swagger2如何集成OAuth2,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
桐城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,桐城網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為桐城成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的桐城做網(wǎng)站的公司定做!
GitHub地址
碼云地址
swagger是一款優(yōu)雅的接口api展示工具,在這里我們具體不展開講解,有興趣的自行百度。 該篇文章主要講解的是如何集成OAuth3的驗證即在請求中添加token,驗證接口是否具有權(quán)限。
方式一:在每個請求上加一個Authorization 窗口自己手動輸入token:
/**
* @Description Swagger api 配置
* @Author wwz
* @Date 2019/08/05
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig2 {
@Value("${swagger.is.enable}")
private boolean SWAGGER_IS_ENABLE; //是否激活開關(guān),在application.yml中配置注入
@Bean
public Docket docket() {
//添加head參數(shù)配置start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.enable(SWAGGER_IS_ENABLE)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wwz.frame.controller"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);//注意這里;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 頁面標(biāo)題
.title("OAuth3權(quán)限管理API文檔")
.contact(new Contact("wwz", "", "wwzwtf@qq.com"))
.description("OAuth3維護文檔")
.version("1.0")
.extensions(Collections.emptyList())
.build();
}
}效果截圖:

方式二:配置application.yml文件 設(shè)置好token登錄的地址,是否啟用swagger,新建配置文件
/**
* @Description Swagger api 配置 模式二:增加登錄
* @Author wwz
* @Date 2019/08/05
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${swagger.is.enable}")
private boolean SWAGGER_IS_ENABLE; //是否激活開關(guān),在application.yml中配置注入
@Value("${swagger.auth.server}")
private String AUTH_SERVER;
@Value("${swagger.service.name}")
private String SERVICE_NAME;
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(SWAGGER_IS_ENABLE)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wwz.frame.controller"))
.paths(PathSelectors.any())
.build()
// .pathMapping(SERVICE_NAME)
.securitySchemes(Collections.singletonList(securityScheme()))
.securityContexts(Collections.singletonList(securityContext()));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 頁面標(biāo)題
.title("OAuth3權(quán)限管理API文檔")
.contact(new Contact("wwz", "", "wwzwtf@qq.com"))
.description("OAuth3維護文檔")
.version("1.0")
.extensions(Collections.emptyList())
.build();
}
/**
* 這個類決定了你使用哪種認證方式,我這里使用密碼模式
*/
private SecurityScheme securityScheme() {
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(AUTH_SERVER);
return new OAuthBuilder()
.name("OAuth3")
.grantTypes(Collections.singletonList(grantType))
.scopes(Arrays.asList(scopes()))
.build();
}
/**
* 這里設(shè)置 swagger2 認證的安全上下文
*/
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(Collections.singletonList(new SecurityReference("OAuth3", scopes())))
.forPaths(PathSelectors.any())
.build();
}
/**
* 這里是寫允許認證的scope
*/
private AuthorizationScope[] scopes() {
return new AuthorizationScope[]{
};
}
}在MySecurityResourceServerConfig 放行swagger相關(guān)。
界面截圖:

登錄截圖:

測試:

swagger 整合OAuth2完成。
看完上述內(nèi)容,你們掌握swagger2如何集成OAuth2的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁題目:swagger2如何集成OAuth2
文章URL:http://chinadenli.net/article20/ppssjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、靜態(tài)網(wǎng)站、定制網(wǎng)站、建站公司、移動網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)