session - go-swagger 不调用我的标题检查

标签 session go swagger go-swagger

我有一个通过 go-swagger 生成的 api。我正在尝试进行 session 检查,检查它是否没有按我预期的那样触发。我关注了一个example我在 github 上找到的,但似乎对我不起作用。

我的代码:

// Applies when the "X-Session-Key" header is set
    api.SessionKeyHeaderAuth = func(token string) (interface{}, error) {
    // test the token
    success := routeHandler.HandleSessionHeaderKey(token)
    if success{
        return nil, nil
    }
    //We are pessimistic, if they aren't successful then we return a 401
    api.Logger("Access attempt with incorrect api key auth: %s", token)
    return nil, errors.New(401, "incorrect api key auth")
    }

我的 Yaml(用于我正在 curl 的端点):

/auth/logout:
  post:
    summary: Logs in the user
    consumes:
      - application/x-www-form-urlencoded
    operationId: authLogoutUser
    tags:
      - auth
    description:
      Allow users to log out and their session will be terminated
    produces:
      - application/json
    parameters:
      - in: header
        name: X-Session-Key
        type: string
        required: true
      - in: header
        name: X-Profile-Key
        type: string
        required: true
    responses:
      200:
       description: Login Success
       headers:
        ProfileKeyHeader:
          type: string
          description: The key for the profile data
        SessionKeyHeader:
          type: string
          description: The key for the session data
      400:
       description: Whether the user is not found or error while login, decided on a generic login failure error
       schema:
        $ref: 'definitions.yaml#/definitions/Error'
      429:
       description: Too many requests and being throttled
       schema:
        $ref: 'definitions.yaml#/definitions/Error'
      500:
        description: Too many requests and being throttled
        schema:
          $ref: 'definitions.yaml#/definitions/Error'

如果您能帮我看看我做错了什么,我们将不胜感激。

最佳答案

所以,我是个白痴......

问题是我忘记将安全性添加到我的 swagger yaml 中。一旦我这样做了,我的函数就会被调用。

operationId: authLogoutUser
        tags:
          - auth
        description:
          Allow users to log out and their session will be terminated
        produces:
          - application/json
        security:
          - SessionKeyHeader: []

关于session - go-swagger 不调用我的标题检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048275/

相关文章:

Java - Google App Engine - 当我更改存储在 session 范围内的类时出现 InvalidClassException

go - 为什么在尝试构建任何Go程序时出现重新声明的错误?

json - 在 Go 中解析 JSON(没有 Unmarshal)

java - 在 Java 中隐藏 swagger UI 中不重要的 getter 方法

java - Blob 的 Swagger 规范表示

ruby-on-rails - Redis 存储中的动态 session key 取决于请求域

php - 让javascript知道php服务器端登录/注销的推荐方法?

php - 在 PHP 中,什么会导致 $_SESSION 变为 null?

docker - 无法使用 Go SDK for Docker 推送图像

c# - Servicestack 对象参数未通过 Swagger-UI 传递给服务