linux

Linux环境中Swagger的认证机制有哪些

小樊
34
2025-12-02 04:47:46
栏目: 智能运维

Linux环境中Swagger的认证机制

一、常用认证机制与规范位置

二、在规范中的配置示例 OpenAPI 2.0

swagger: '2.0'
info:
  title: Sample API
  version: '1.0.0'
securityDefinitions:
  ApiKeyAuth:
    type: apiKey
    name: Authorization
    in: header
  BasicAuth:
    type: basic
  OAuth2:
    type: oauth2
    flow: accessCode
    authorizationUrl: https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      read: Grants read access
      write: Grants write access
  openid:
    type: openid-connect
    authorizationUrl: https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      email: Access to the user's email
paths:
  /users:
    get:
      security:
        - OAuth2: [read]
        - ApiKeyAuth: []

要点:securityDefinitions 定义方案;paths 下通过 security 指定该路径需要的方案与权限范围(scopes)。

三、在规范中的配置示例 OpenAPI 3.0

openapi: 3.0.0
info:
  title: Sample API
  version: '1.0.0'
components:
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
    BasicAuth:
      type: http
      scheme: basic
    OAuth2:
      type: oauth2
      flows:
        authorizationCode:
          authorizationUrl: https://example.com/oauth/authorize
          tokenUrl: https://example.com/oauth/token
          scopes:
            read: Read access
            write: Write access
    OpenID:
      type: openIdConnect
      openIdConnectUrl: https://example.com/.well-known/openid-configuration
paths:
  /users:
    get:
      security:
        - OAuth2: [read]
        - BearerAuth: []

要点:OpenAPI 3.0 将安全方案统一到 components.securitySchemes,在 security 中按操作或全局引用。

四、服务端与网关侧的安全实践

五、选型建议

0
看了该问题的人还看了