使用Annotation进行Swagger身份验证

时间:2017-07-12 10:01:51

标签: swagger swagger-ui swagger-2.0

我正在尝试在我的招摇ui中使用基本的Auth。我使用Swagger 2.0.5作为maven库。使用SwaggerConfig类创建docket Api和其他配置。

`

public class SwaggerConfig {

    /**
     * Every Docket bean is picked up by the swagger-mvc framework - allowing for multiple
     * swagger groups i.e. same code base multiple swagger resource listings.
     */

    @Bean
    public Docket api() {
        AuthorizationScope[] authScopes = new AuthorizationScope[1];
        authScopes[0] = new AuthorizationScopeBuilder()
                .scope("")
                .build();

        SecurityReference securityReference = SecurityReference.builder()
                .reference("basicAuth")
                .scopes(authScopes)
                .build();

        ArrayList<SecurityReference> reference = new ArrayList<SecurityReference>(1);
        reference.add(securityReference);


        ArrayList<SecurityContext> securityContexts = new ArrayList<SecurityContext>(1);
        securityContexts.add(SecurityContext.builder().securityReferences(reference).build());

        ArrayList<SecurityScheme> auth = new ArrayList<SecurityScheme>(1);
        auth.add(new BasicAuth("basicAuth"));

        Documentation Doc = new DocumentationBuilder().basePath("/swagger-ui.html").build();

        return new Docket(DocumentationType.SWAGGER_2)
                .securitySchemes(auth)
                .securityContexts(securityContexts)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.toyota.tme.consumerapi.rest"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
                }

  /*  @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/v1/.*"))
                .build();
    }*/

    private ApiInfo apiInfo() {


        return new ApiInfoBuilder()
                .title("Consumer API")
                .description("Azure based Consumer API.")
                .contact("CarIT")
                .build();
    }
}`

我的问题是,我在我的休息服务中使用授权注释来启用Basic Auth。

    @Api(value = "/ping", tags = "1.Ping", description = "API",authorizations = {@Authorization(value="basicAuth")})
@RestController
@RequestMapping("/api")
public class ping {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(path = "/ping", method = RequestMethod.GET,
            produces = MediaType.TEXT_HTML_VALUE)
    @ApiOperation(value = "Ping service", httpMethod = "GET", response = String.class,
            produces = "text/html", notes = "ping service",authorizations = {@Authorization(value="basicAuth")})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success", response = String.class),
            @ApiResponse(code = 400, message = "Bad request"),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 409, message = "Conflict"),
            @ApiResponse(code = 503, message = "Dependent System(s) are unavailable"),
            @ApiResponse(code = 500, message = "Unknown server Error occurred")})
    public ResponseEntity<String> ping() {
        this.logger.info("springframework api :Ping Request received for ConsumerAPI");
        return new ResponseEntity<>(ApplicationConstants.Ping, HttpStatus.OK);
    }
}

但是这段代码无效。我无法看到Swagger提出的任何授权。请在此处提供帮助

1 个答案:

答案 0 :(得分:0)

对我而言。通过将值授权从Api移动到ApiOperation注释来解决此问题。不漂亮但工作。