2017-03-29 25 views

回答

0
  1. 將以下API密鑰定義爲安全方案。在你的原因標題叫X-AUTH-TOKEN。我們使用密鑰mykey來引用此方案。
private ApiKey apiKey() { 
    return new ApiKey("mykey", "X-AUTH-TOKEN", "header"); 
} 
  • 設置的安全上下文。這僅僅意味着你正在設置API的給定路徑的授權範圍。對於例如對於/anyPath/customers,我們可能需要accessEverything的範圍。
  • private SecurityContext securityContext() { 
        return SecurityContext.builder() 
         .securityReferences(defaultAuth()) 
         .forPaths(PathSelectors.regex("/anyPath.*")) 
         .build(); 
    } 
    
    List<SecurityReference> defaultAuth() { 
        AuthorizationScope authorizationScope 
         = new AuthorizationScope("global", "accessEverything"); 
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; 
        authorizationScopes[0] = authorizationScope; 
        return newArrayList(
         new SecurityReference("myKey", authorizationScopes)); 
    } 
    

    然後在你的案卷新創建的安全上下文和安全機制相關聯。

    new Docket(...) 
        .securitySchemes(newArrayList(apiKey())) 
        .securityContexts(newArrayList(securityContext())) 
    

    我們能夠昂首闊步UI,你需要提供以下bean的配置

    @Bean 
    SecurityConfiguration security() { 
        return new SecurityConfiguration(
         "test-app-client-id", 
         "test-app-client-secret", 
         "test-app-realm", 
         "test-app", 
         "YOUR_API_AUTH_TOKEN", 
         ApiKeyVehicle.HEADER, 
         "X-AUTH-TOKEN", 
         "," /*scope separator*/); 
    } 
    

    這是告訴你要使用API​​密鑰,並提供了一個API身份驗證令牌在招搖的UI編譯時間(可能使用加密的配置屬性)

    注意:swagger-ui的可配置性受到限制,可以滿足80%的用例,額外的自定義可能意味着您將無法使用捆綁的swagger -ui。

    相關問題