已配置Swagger授权注解,如何自定义安全认证Type字段值?
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:
- Add a
SecuritySchemedefinition 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(); } }
- Keep your existing
@ApiOperationannotation 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.APIKEYand specifyin = SecuritySchemeIn.HEADER(or QUERY/COOKIE) andparamName = "X-API-Key". - For HTTP Basic Auth: Use
type = SecuritySchemeType.HTTPandscheme = "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

