这个包为需要本地存储和处理认证的项目提供本地认证库。
完整的文档可以在 http://www.perfect.org/docs/authentication.html 找到
一个 PostgreSQL 版本的模板应用程序可以在 https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template 找到,它提供了一个功能齐全的起点,并演示了系统的使用方法。虽然此模板提供 PostgreSQL 集成,但其原理仍然适用于 MySQL 版本。
这个包使用 Swift Package Manager 构建,并且是 Perfect 项目的一部分。它被编写成独立的,因此不需要 PerfectLib 或任何其他组件。
确保您已安装并激活最新的 Swift 3.x 工具链。
将此项目作为依赖项添加到您的 Package.swift 文件中。
.Package(url: "https://github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL.git", majorVersion: 1)
然后在你的代码中使用 LocalAuthentication 模块
import LocalAuthentication
在 main.swift 中配置以下内容以设置数据库和会话配置非常重要
导入所需的模块
import PerfectSession
import PerfectSessionMySQL
import PerfectCrypto
import LocalAuthentication
初始化 PerfectCrypto
let _ = PerfectCrypto.isInitialized
现在设置一些默认值
// Used in email communications
// The Base link to your system, such as http://www.example.com/
var baseURL = ""
// Configuration of Session
SessionConfig.name = "perfectSession" // <-- change
SessionConfig.idle = 86400
SessionConfig.cookieDomain = "localhost" //<-- change
SessionConfig.IPAddressLock = false
SessionConfig.userAgentLock = false
SessionConfig.CSRF.checkState = true
SessionConfig.CORS.enabled = true
SessionConfig.cookieSameSite = .lax
详细的会话配置文档可以在 https://www.perfect.org/docs/sessions.html 找到
数据库和电子邮件配置应如下设置(如果使用 JSON 文件配置)
let opts = initializeSchema("./config/ApplicationConfiguration.json") // <-- loads base config like db and email configuration
httpPort = opts["httpPort"] as? Int ?? httpPort
baseURL = opts["baseURL"] as? String ?? baseURL
否则,这些需要设置为等同于此函数 https://github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL/blob/master/Sources/PerfectLocalAuthentication/Schema/InitializeSchema.swift。
设置会话驱动程序
let sessionDriver = SessionMySQLDriver()
以下两个会话过滤器需要添加到您的服务器配置中
// (where filter is a [[String: Any]] object)
filters.append(["type":"request","priority":"high","name":SessionMySQLFilter.filterAPIRequest])
filters.append(["type":"response","priority":"high","name":SessionMySQLFilter.filterAPIResponse])
可以根据需要添加以下路由或进行自定义以添加登录、注销、注册
// Login
routes.append(["method":"get", "uri":"/login", "handler":Handlers.login]) // simply a serving of the login GET
routes.append(["method":"post", "uri":"/login", "handler":LocalAuthWebHandlers.login])
routes.append(["method":"get", "uri":"/logout", "handler":LocalAuthWebHandlers.logout])
// Register
routes.append(["method":"get", "uri":"/register", "handler":LocalAuthWebHandlers.register])
routes.append(["method":"post", "uri":"/register", "handler":LocalAuthWebHandlers.registerPost])
routes.append(["method":"get", "uri":"/verifyAccount/{passvalidation}", "handler":LocalAuthWebHandlers.registerVerify])
routes.append(["method":"post", "uri":"/registrationCompletion", "handler":LocalAuthWebHandlers.registerCompletion])
// JSON
routes.append(["method":"get", "uri":"/api/v1/session", "handler":LocalAuthJSONHandlers.session])
routes.append(["method":"get", "uri":"/api/v1/logout", "handler":LocalAuthJSONHandlers.logout])
routes.append(["method":"post", "uri":"/api/v1/register", "handler":LocalAuthJSONHandlers.register])
routes.append(["method":"login", "uri":"/api/v1/login", "handler":LocalAuthJSONHandlers.login])
用户 ID 可以通过以下方式访问
request.session?.userid ?? ""
如果需要用户 ID(即登录状态)才能访问页面,可以使用如下代码来检测并重定向
let contextAuthenticated = !(request.session?.userid ?? "").isEmpty
if !contextAuthenticated { response.redirect(path: "/login") }
有关 Perfect 项目的更多信息,请访问 perfect.org。