Kitura

APIDoc Build Status - Master macOS Linux Apache 2 Slack Status

Kitura-Session

一个可插拔的框架,用于在使用 Kitura 的 Swift 服务器中管理用户会话。

Swift 版本

最新版本的 Kitura-Session 需要 Swift 5.2 或更高版本。您可以按照此链接下载此版本的 Swift 二进制文件。不保证与其他 Swift 版本的兼容性。

用法

添加依赖项

Kitura-Session 包添加到应用程序的 Package.swift 文件中的依赖项。将 "x.x.x" 替换为最新的 Kitura-Session 版本

.package(url: "https://github.com/Kitura/Kitura-Session.git", from: "x.x.x")

KituraSession 添加到您的目标依赖项

.target(name: "example", dependencies: ["KituraSession"]),

导入包

import KituraSession

原始路由会话

入门

为了在原始路由上使用 Session 中间件,必须创建一个 Session 实例

public init(secret: String, cookie: [CookieParameter]?=nil, store: Store?=nil)

其中


cookiestore 参数是可选的。

secret 参数用于保护会话 ID 并确保会话 ID 无法被猜测。Secret 用于通过 PBKDF2 和固定 IV 导出加密和签名密钥对,以使会话 ID cookie 被认证和加密。 Secret 不直接用于加密或计算 cookie 的 MAC。

例子

在此示例中,创建一个 RedisStore 实例,该实例将用于持久保存会话数据(有关更多信息,请参见 KituraSessionRedis)。然后创建一个 Session 实例,指定 redisStore 作为会话存储。最后,将 session 实例注册为所需路径上的中间件。

import Kitura
import KituraSession
import KituraSessionRedis

let redisStore = RedisStore(redisHost: host, redisPort: port)
let session = Session(secret: "Some secret", store: redisStore)
router.all(middleware: session)

在会话中存储 Any

在您的 Kitura 路由中,您可以为给定键在 request.session 中存储 Any 类型。然后可以将其作为 Any 检索并强制转换为所需的类型

router.post("/session") {request, response, next in
    request.session?["key"] = "value"
    next()
}
router.get("/session") {request, response, next in
    let value = request.session?["key"] as? String
    next()
}

Any 类型必须是 JSON 可序列化的。否则,会话在尝试保存会话时将失败。

在会话中存储 Codable

Swift 4.1 或更高版本开始可用

在您的 Kitura 路由中,您还可以为给定键在 request.session 中存储 Codable 对象。然后可以将其作为声明的类型检索

public struct User: Codable {
    let name: String
}
router.post("/user") { request, response, next in
    let user = User(name: "Kitura")
    request.session?["User"] = user
    next()
}
router.get("/user") { request, response, next in
    let user: User? = request.session?["Kitura"]
    next()
}

TypeSafeSession 示例

要在 Codable 路由上使用会话,请声明一个符合 TypeSafeSession 协议的类型

// Defines the session instance data
final class MySession: TypeSafeSession {
    let sessionId: String                       // Requirement: every session must have an ID
    var books: [Book]                           // User-defined type, where Book conforms to Codable

    init(sessionId: String) {                   // Requirement: must be able to create a new (empty)
        self.sessionId = sessionId              // session containing just an ID. Assign a default or
        books = []                              // empty value for any non-optional properties.
    }
}

// Defines the configuration of the user's type: how the cookie is constructed, and how the session is persisted.
extension MySession {
    static let sessionCookie: SessionCookie = SessionCookie(name: "MySession", secret: "Top Secret")
    static var store: Store?
}

然后可以将 MySession 类型包含在应用程序的 Codable 路由处理程序中。例如

struct Book: Codable {
    let title: String
    let author: String
}

router.get("/cart") { (session: MySession, respondWith: ([Book]?, RequestError?) -> Void) in
    respondWith(session.books, nil)
}

router.post("/cart") { (session: MySession, book: Book, respondWith: (Book?, RequestError) -> Void) in
    var session = session       // Required when mutating a Struct
    session.books.append(book)
    session.save()
    respondWith(book, nil)
}

插件

API 文档

有关更多信息,请访问我们的 API 参考

社区

我们喜欢谈论服务器端 Swift 和 Kitura。加入我们的 Slack 与团队见面!

许可证

此库已获得 Apache 2.0 许可。完整的许可证文本可在 LICENSE 中找到。