如何在swagger-ui中显示”/oauth/token”端点?

huangapple go评论86阅读模式
英文:

How to display "/oauth/token" endpoint on swagger-ui?

问题

@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("all").select()
            .apis(RequestHandlerSelectors.basePackage("com.demo.userservice.api.controller"))
            .paths(regex("/api.*"))
            .build()
            .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {
        ApiInfo apiInfo = new ApiInfo(
            "Spring Boot Swagger USERSERVICE API",
            "Spring Boot Swagger USERSERVICE API",
            "1.0",
            "Terms of Service",
            new Contact("userservice", "",
                    ""),
            "Apache License Version 2.0",
            "https://www.apache.org/licesen.html", new ArrayList<VendorExtension>()
        );

        return apiInfo;
    }
}
英文:

My project use spring cloud oauth to authenticate user with "/oauth/token" endpoint, but i can't find any way to display this api operation on swagger ui. What should I do?

Swagger Configuration:

@EnableSwagger2
@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(&quot;all&quot;).select()
            .apis(RequestHandlerSelectors.basePackage(&quot;com.demo.userservice.api.controller&quot;))
            .paths(regex(&quot;/api.*&quot;))
            .build()
            .apiInfo(metaInfo());
}



private ApiInfo metaInfo() {

    ApiInfo apiInfo = new ApiInfo(
            &quot;Spring Boot Swagger USERSERVICE API&quot;,
            &quot;Spring Boot Swagger USERSERVICE API&quot;,
            &quot;1.0&quot;,
            &quot;Terms of Service&quot;,
            new Contact(&quot;userservice&quot;, &quot;&quot;,
                    &quot;&quot;),
            &quot;Apache License Version 2.0&quot;,
            &quot;https://www.apache.org/licesen.html&quot;, new ArrayList&lt;VendorExtension&gt;()
    );

    return apiInfo;
}


}

答案1

得分: 1

一种解决方案是打开所有终端以显示oauth/token终端,方法如下:

.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().pathMapping("/");

或者

如果您只想显示所需路径,例如/api/.*,则可以使用以下方式修改oauth终端的端点:

@Configuration
@EnableAuthorizationServer
public class EndPointModificationConfig extends AuthorizationServerConfigurerAdapter {    

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
            .pathMapping("/oauth/token", "/api/oauth/token");
    }
}

并且使用以下代码获取所有API:

.apis(RequestHandlerSelectors.any())
英文:

One solution is open all endpoint to show oauth/token endpoint this way

.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().pathMapping(&quot;/&quot;)

Or

If you want to show only desired path like /api/.* then you can modify the endpoint of oauth using this

@Configuration
@EnableAuthorizationServer
public class EndPointModificationConfig extends AuthorizationServerConfigurerAdapter {    

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
            endpoints
                .pathMapping(&quot;/oauth/token&quot;, &quot;/api/oauth/token&quot;);
        }
}

And use this for get all api

.apis(RequestHandlerSelectors.any())

huangapple
  • 本文由 发表于 2020年5月3日 19:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定