在 Springfox Swagger 中为 OAuth2 身份验证添加自定义标头

我正在尝试向 OAuth 安全方案 (Springfox Swagger 2.8.0) 添加自定义标头。任何想法如何实现?

我当前的配置(使用带有 ImplicitGrant 的 OAuth,服务器端是 keycloak)看起来像:

@Bean
public SecurityContext securityContext() {
    return SecurityContext.builder().securityReferences(defaultAuth()).build();
}

private List<SecurityReference> defaultAuth() {
    return Arrays.asList(new SecurityReference(SECURITY_SCHEME_OAUTH2, defaultScope().toArray(new AuthorizationScope[] {})));
}
private Predicate<String> postPaths() {
    return regex("/.*");
}

private Predicate<String> springBootActuatorJmxPaths() {
    return regex("^/(?!env|restart|pause|resume|refresh).*$");
}

private List<AuthorizationScope> defaultScope() {
    AuthorizationScope authorizationScope = new AuthorizationScope("openid", "Basic Open ID Connect Scope");
    List<AuthorizationScope> authorizationScopes = new ArrayList<>();
    authorizationScopes.add(authorizationScope);
    return authorizationScopes;
}

@Bean
public Docket postsApi(List<SecurityContext> securityContexts) {
    return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
            .apiInfo(apiInfo()).select().paths(postPaths())
            .apis(RequestHandlerSelectors.basePackage("com.example"))
            .paths(springBootActuatorJmxPaths())
            .build()
            .securitySchemes(Collections.singletonList(oauth()))
            .securityContexts(securityContexts)
            ;
}
@Bean
List<GrantType> grantTypes() {
    List<GrantType> grantTypes = new ArrayList<>();
    grantTypes.add(
            new ImplicitGrant(
                    new LoginEndpoint(oAuthServerUri + "/realms/" + REALM_NAME + "/protocol/openid-connect/auth"),
                    "access_token"
            )
    );
    return grantTypes;
}

@Bean
SecurityScheme oauth() {
    return new OAuthBuilder()
            .name(SECURITY_SCHEME_OAUTH2)
            .scopes(defaultScope())
            .grantTypes(grantTypes())
            .build();
}

@Bean
public SecurityConfiguration securityInfo() {
    return SecurityConfigurationBuilder.builder()
            .clientId(clientId)
            .realm(REALM_NAME)
            .appName(serviceName)
            .scopeSeparator(" ")
            .build();
}

目前在 Springfox Swagger 中是不可能的,更多细节在这里:https : //github.com/springfox/springfox/issues/2266

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Springfox的Swagger 2中为JSONObject请求主体自定义示例值?

如何使用 Springfox 在 swagger2 中添加自定义注释?

使用Springfox在Swagger UI文档中添加标头参数

如何在Springfox Swagger提供的Swagger / v2 / api-docs中启用CORS标头?

django 中自定义用户身份验证和 oauth2 的问题

go-swagger Oauth2 身份验证示例

Springfox Java Bean验证未显示在Swagger输出中

Springfox Swagger UI (api-doc) 中 url 的自定义注释

PathVariable 在 swagger SpringFox(3.0.0) 中作为可选

在springfox-swagger-ui中渲染HTML

@ApiResponse swagger springfox-使用自定义响应容器

将自定义标头添加到.Net中的所有Swagger响应中

Spring boot、Security、OAuth2:是否可以使用自定义 AuthorizationCodeResourceDetails?身份验证服务器需要重定向 url 中的特定参数

ASPNET Core 2中的自定义策略身份验证(jwt身份验证)

OAuth2身份验证和Unity中的操作

在 Outlook 插件中实现 oAuth2 身份验证

Java中的身份验证问题Oauth2

Springfox 在 Swagger 2.0 JSON 定义中创建“ref”类型而不是 java.io.File 对象

如何使用 Swashbuckle 在 ASP.NET Core 中为 Swagger UI 进行 OAuth2 授权

如何从SpringFox向生成的Swagger json添加自定义字段?

如何在Springfox Swagger中将响应模型设置为类?

为Spring MVC生成Swagger JSON:Springfox或Enunciate

Springfox Swagger使用端口80为HTTPS URLS生成请求

Swagger Springfox配置问题

Springfox:@ApiResponse中的@ResponseHeader无法在Swagger UI中呈现

在Spring Security OAuth2中自定义TokenEndpoint

Swagger UI身份验证标头

如何使用Spring Boot使用OAuth1.0和自定义标头对API进行身份验证

Springfox Swagger2和Guava-java.lang.StackOverflowError