首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用注释的Swagger身份验证

使用注释的Swagger身份验证
EN

Stack Overflow用户
提问于 2017-07-12 18:01:51
回答 1查看 1K关注 0票数 0

我正在尝试在我的swagger in中有一个基本的身份验证,我使用Swagger 2.0.5作为一个maven library.Using SwaggerConfig类来创建docket Api和其他配置。

`

代码语言:javascript
复制
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();
    }
}`

我的问题是,我在rest服务中使用授权注释来启用基本身份验证。

代码语言:javascript
复制
    @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.Please要求的任何授权在这方面需要帮助

EN

回答 1

Stack Overflow用户

发布于 2018-05-08 20:33:14

我也一样。通过将值授权从Api转移到ApiOperation注释,解决了这个问题。不是很漂亮,但很有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45054532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档