You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已配置Swagger授权注解,如何自定义安全认证Type字段值?

Fixing "Unknown" Security Type in Swagger Docs When Using @Authorization

Great question! The "Unknown" type you're seeing happens because you've referenced a security scheme (petoauth) in your @ApiOperation, but haven't explicitly defined what type of authentication that scheme uses in your global Swagger configuration. Swagger has no way to infer the type on its own, so it defaults to "Unknown".

Here's how to fix this and set a custom type, depending on which Swagger implementation you're using:

For Springfox (Swagger 2.x)

You need to define your security scheme in your Swagger configuration bean, then link it to your @Authorization annotation:

  1. Add a SecurityScheme definition to your config class:
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.your.app.package"))
                .paths(PathSelectors.any())
                .build()
                // Register your custom security scheme here
                .securitySchemes(Collections.singletonList(petOAuthScheme()));
    }

    private SecurityScheme petOAuthScheme() {
        // Example: Define an OAuth2 Authorization Code flow
        GrantType authCodeGrant = new AuthorizationCodeGrantBuilder()
                .authorizationEndpoint(new AuthorizationEndpoint("https://your-auth-server/authorize", "petoauth"))
                .tokenEndpoint(new TokenEndpoint("https://your-auth-server/token", "oauth2_code"))
                .build();

        return new OAuthBuilder()
                .name("petoauth") // Must match the `value` in your @Authorization annotation
                .grantTypes(Collections.singletonList(authCodeGrant))
                .scopes(Collections.singletonList(new AuthorizationScope("add:pet", "allows adding of pets")))
                .build();
    }
}
  1. Keep your existing @ApiOperation annotation as-is — now that the scheme is defined, Swagger will recognize its type (in this case, OAuth2) and display it correctly instead of "Unknown".

For SpringDoc (OpenAPI 3.x)

SpringDoc uses OpenAPI 3 annotations which simplify this process. Just add a @SecurityScheme annotation to your config class (or any Spring-managed component):

@Configuration
@OpenAPIDefinition
// Define your security scheme directly here
@SecurityScheme(
        name = "petoauth", // Matches the `value` in your @Authorization
        type = SecuritySchemeType.OAUTH2, // Set your custom type here (e.g., APIKEY, OAUTH2, HTTP)
        flows = OAuthFlows.authorizationCode(
                OAuthAuthorizationCodeFlow.builder()
                        .authorizationUrl("https://your-auth-server/authorize")
                        .tokenUrl("https://your-auth-server/token")
                        .scopes(new OAuthScope(name = "add:pet", description = "allows adding of pets"))
                        .build()
        )
)
public class OpenApiConfig {
    // Additional config if needed
}

For Other Authentication Types

If you're using something like API Key authentication instead of OAuth2, adjust the SecurityScheme type accordingly:

  • For API Key: Set type = SecuritySchemeType.APIKEY and specify in = SecuritySchemeIn.HEADER (or QUERY/COOKIE) and paramName = "X-API-Key".
  • For HTTP Basic Auth: Use type = SecuritySchemeType.HTTP and scheme = "basic".

Once you've defined the security scheme globally, your Swagger docs will show the exact type you specified instead of "Unknown".

内容的提问来源于stack exchange,提问作者Ankzz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:32:14