我是usinig jhipster来生成一个项目,现在我已经用
获得了一些端点@PostMapping("/myEndpoint")
@PreAuthorize("#oauth2.hasScope('write')")
效果很好,但招摇时我看不到发送令牌的位置......
之前我曾经使用过swagger(没有配置它们),我知道这是可能的,但我不确定是否是一个招摇的配置或者我的端点,任何想法?
答案 0 :(得分:1)
您可以使用类似
的方法注释您的方法@ApiOperation(authorizations = {
@Authorization(value = "my_oauth", scopes = {
@AuthorizationScope(scope = "write")
})
})
或者在带有SecurityContext的springfox docket中使用regexp进行设置(如果需要,可以使regexp适应多个端点)
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(writeAuth())
.forPaths(PathSelectors.regex("/myEndpoint"))
.build();
}
List<SecurityReference> writeAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("write", "");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return newArrayList(
new SecurityReference("my_oauth", authorizationScopes));
}
您可能还希望通过配置docket SecuritySchemes
来定义securityDefinitionsprivate OAuth oauth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("write", "can write");
return new OAuth("my_oauth", newArrayList(authorizationScope), newArrayList(new ResourceOwnerPasswordCredentialsGrant("/oauth/token")));
}
我认为现在在jhipster lib中配置了默认文件夹,因此您无法轻松自定义它,您可能需要创建一个新的docket bean来添加SecuritySchemes和SecurityContext
@Bean
public Docket myApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("alt")
.select()
...
.securitySchemes(newArrayList(oauth()))
.securityContexts(newArrayList(securityContext()))
;
}
您的新规范将在http://localhost:8080/v2/api-docs?group=alt
上提供有关此内容的详细信息,请参阅springfox doc:http://springfox.github.io/springfox/docs/current/#getting-started-spring-boot