此包为需要本地存储和处理身份验证的项目提供本地认证库。
完整文档请参见 http://www.perfect.org/docs/authentication.html
模板应用程序可在 https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template 找到,它提供了一个功能齐全的起点,并演示了该系统的用法。
此包使用 Swift Package Manager 构建,并且是 Perfect 项目的一部分。 它的编写是独立的,因此不需要 PerfectLib 或任何其他组件。
请确保您已安装并激活了最新的 Swift 3.x 工具链。
将此项目作为依赖项添加到您的 Package.swift 文件中。
.Package(url: "https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL.git", majorVersion: 1)
然后在您的代码中使用 LocalAuthentication 模块
import LocalAuthentication
在 main.swift 中配置以下内容以设置数据库和会话配置非常重要
导入所需的模块
import PerfectSession
import PerfectSessionPostgreSQL
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-PostgreSQL/blob/master/Sources/LocalAuthentication/Schema/InitializeSchema.swift。
设置会话驱动程序
let sessionDriver = SessionPostgresDriver()
以下两个会话过滤器需要添加到您的服务器配置中
// (where filter is a [[String: Any]] object)
filters.append(["type":"request","priority":"high","name":SessionPostgresFilter.filterAPIRequest])
filters.append(["type":"response","priority":"high","name":SessionPostgresFilter.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。