一个可插拔的框架,用于在使用 Kitura 的 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)
其中
CookieParameter 枚举中指定的):name - cookie 的名称,默认为 "kitura-session-id"。path - cookie 的 path 属性,它定义了应提供此 cookie 的路径。默认为 "/",这意味着允许任何路径。secure - cookie 的 secure 属性,它指示是否应仅通过安全 (https) 连接提供 cookie。默认为 false。maxAge - cookie 的 maxAge 属性,即,从发布之时起 cookie 应保留的最长时间(以秒为单位)。默认为 -1.0,即,永不过期。Store 协议。如果未设置,则使用 InMemoryStore。cookie 和 store 参数是可选的。
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)
在您的 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 可序列化的。否则,会话在尝试保存会话时将失败。
从 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()
}
要在 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 参考。
我们喜欢谈论服务器端 Swift 和 Kitura。加入我们的 Slack 与团队见面!
此库已获得 Apache 2.0 许可。完整的许可证文本可在 LICENSE 中找到。