我正在使用swagger,swagger ui和spring rest api来获得测试/记录API的平台,所以我需要在swagger ui中使用oAuth2授权,我在授权服务器上使用密码授权,所以我不得不使用ResourceOwnerPasswordCredentialsGrant
从包springfox.documentation.service
which有一个参数到它的构造函数,即令牌url,我将它设置为我的授权服务器中的令牌端点,但不幸的是,它不会持久化令牌网址并在授权窗口中显示为null如下:
我找不到任何使用这种特殊类型的赠款与swagger ui的例子,非常感谢任何帮助。
这是我的配置
public Docket oauth() {
return new Docket(DocumentationType.SWAGGER_2).groupName("oauth")
.securitySchemes(Arrays.asList(userOAuthScheme())).securityContexts(Arrays.asList(securityContext()))
.select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
.paths(not(ant("/admin/**")))
.paths(not(ant("/admin.json")))
.paths(not(ant("/error/**")))
.paths(not(ant("/exception/**")))
.paths(not(ant("/ping/**"))).build();
}
private OAuth userOAuthScheme() {
List<AuthorizationScope> authorizationScopeList = new ArrayList<AuthorizationScope>();
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("http://localhost:8080/authServer/oauth/token");
return new OAuth("oauth2", authorizationScopeList, Arrays.asList(grantType));
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build();
}
@Bean
public SecurityConfiguration securityInfo() {
return new SecurityConfiguration("myClientId", "myClientSecret", "", "", "", ApiKeyVehicle.HEADER, "",
" ");
}
private List<SecurityReference> defaultAuth() {
final AuthorizationScope[] authorizationScopes = new AuthorizationScope[0];
return Arrays.asList(new SecurityReference("oauth2", authorizationScopes));
}
在Swagger屏幕上,请注意“设置客户端身份验证”部分
Type: Basic auth/ Request Body
这取决于你的实现,在我的情况下工作Basic auth。
我不使用范围,但你可以添加它
AuthorizationScope[] authorizationScopes
List<AuthorizationScope> authorizationScopeList