2017-08-08 226 views
0

我usinig jhipster生成一個項目,現在香港專業教育學院獲得了一些端點與如何使swagger接受oauth2令牌?

@PostMapping("/myEndpoint") 
@PreAuthorize("#oauth2.hasScope('write')") 

它的偉大工程,但在招搖我看不到的地方送令牌......

香港專業教育學院之前,曾大搖大擺(沒有配置他們),我知道是可能的,但我不確定是否是一個Swagger配置或是我的端點,任何想法?

+1

你可以分享你招搖的配置嗎? –

回答

0

您可以批註的東西你的方法類似

@ApiOperation(authorizations = { 
    @Authorization(value = "my_oauth", scopes = { 
     @AuthorizationScope(scope = "write") 
    }) 
}) 

或用正則表達式中使用的SecurityContext一個springfox案設置(適應的正則表達式來覆蓋多個端點如果你想)

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)); 
} 

您也可能想通過配置文件來定義安全定義:SecuritySchemes

private OAuth oauth() { 
    AuthorizationScope authorizationScope 
     = new AuthorizationScope("write", "can write"); 
    return new OAuth("my_oauth", newArrayList(authorizationScope), newArrayList(new ResourceOwnerPasswordCredentialsGrant("/oauth/token"))); 
} 

我覺得默認的案卷在jhipster lib應該配置的,所以你將不能夠輕鬆定製它,你可能要創建一個新的文案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