|
| 1 | +# Perfect Local Authentication (PostgreSQL) |
| 2 | + |
| 3 | +[](http://perfect.org/get-involved.html) |
| 4 | + |
| 5 | +[](https://github.com/PerfectlySoft/Perfect) |
| 6 | +[](https://gitter.im/PerfectlySoft/Perfect) |
| 7 | +[](https://twitter.com/perfectlysoft) |
| 8 | +[](http://perfect.ly) |
| 9 | + |
| 10 | + |
| 11 | +[](https://developer.apple.com/swift/) |
| 12 | +[](https://developer.apple.com/swift/) |
| 13 | +[](http://perfect.org/licensing.html) |
| 14 | +[](http://twitter.com/PerfectlySoft) |
| 15 | +[](https://gitter.im/PerfectlySoft/Perfect?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| 16 | +[](http://perfect.ly) [](https://badge.fury.io/gh/PerfectlySoft%2FPerfect-CURL) |
| 17 | + |
| 18 | +## Perfect Local Authentication (PostgreSQL) Library |
| 19 | + |
| 20 | +基于PostgreSQL数据库为驱动的服务器端用户身份验证函数库 |
| 21 | + |
| 22 | +## 参考实例 |
| 23 | + |
| 24 | +请参考如下链接的模板应用程序:[https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template),内容为完整的函数使用方法展示。 |
| 25 | + |
| 26 | +## 添加组件 |
| 27 | + |
| 28 | +欲使用本函数库,请选择如下适当的模块并追加到您项目的Package.swift文件中去。 |
| 29 | + |
| 30 | +``` swift |
| 31 | +.Package(url: "https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL.git", majorVersion: 1) |
| 32 | +``` |
| 33 | +## 模块配置 |
| 34 | + |
| 35 | +具体使用之前,请务必做好模块配置工作(以下例子假设您使用main.swift作为程序入口)。首先导入函数库: |
| 36 | + |
| 37 | +``` swift |
| 38 | +import PerfectSession |
| 39 | +import PerfectSessionPostgreSQL |
| 40 | +import PerfectCrypto |
| 41 | +import LocalAuthentication |
| 42 | +``` |
| 43 | + |
| 44 | +然后请初始化 PerfectCrypto 加密函数库: |
| 45 | + |
| 46 | +``` swift |
| 47 | +let _ = PerfectCrypto.isInitialized |
| 48 | +``` |
| 49 | + |
| 50 | +设置默认选项: |
| 51 | + |
| 52 | +``` swift |
| 53 | +// 以下配置在电子邮件系统中会用上。 |
| 54 | +// 请设置链接到您自己系统主页的基本地址,比如 http://www.example.com/ |
| 55 | +var baseURL = "" |
| 56 | + |
| 57 | +// 配置会话过程 |
| 58 | +SessionConfig.name = "perfectSession" // <-- 请自行设定名称 |
| 59 | +SessionConfig.idle = 86400 |
| 60 | +SessionConfig.cookieDomain = "localhost" //<-- 请自行设定主机名 |
| 61 | +SessionConfig.IPAddressLock = false |
| 62 | +SessionConfig.userAgentLock = false |
| 63 | +SessionConfig.CSRF.checkState = true |
| 64 | +SessionConfig.CORS.enabled = true |
| 65 | +SessionConfig.cookieSameSite = .lax |
| 66 | +``` |
| 67 | + |
| 68 | +详细的会话过程文档请参考 [https://www.perfect.org/docs/sessions_zh_CN.html](https://www.perfect.org/docs/sessions_zh_CN.html) |
| 69 | + |
| 70 | +数据库和电邮配置方法参考如下(假设您使用JSON进行配置): |
| 71 | + |
| 72 | +``` swift |
| 73 | +let opts = initializeSchema("./config/ApplicationConfiguration.json") // <-- 该JSON文件包含数据库和邮件配置信息 |
| 74 | +httpPort = opts["httpPort"] as? Int ?? httpPort |
| 75 | +baseURL = opts["baseURL"] as? String ?? baseURL |
| 76 | +``` |
| 77 | + |
| 78 | +否则,请使用下列函数完成等价的配置: |
| 79 | + |
| 80 | +* PostgreSQL: [https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL/blob/master/Sources/LocalAuthentication/Schema/InitializeSchema.swift](https://github.com/PerfectlySoft/Perfect-LocalAuthentication-PostgreSQL/blob/master/Sources/LocalAuthentication/Schema/InitializeSchema.swift) |
| 81 | + |
| 82 | +### 设置会话数据库驱动 |
| 83 | + |
| 84 | +``` swift |
| 85 | +let sessionDriver = SessionPostgresDriver() |
| 86 | +``` |
| 87 | + |
| 88 | +### 设置请求/响应过滤器 |
| 89 | + |
| 90 | +下列两个会话过滤器需要追加到您服务器的配置中: |
| 91 | + |
| 92 | +``` swift |
| 93 | +// (where filter is a [[String: Any]] object) |
| 94 | +filters.append(["type":"request","priority":"high","name":SessionPostgresFilter.filterAPIRequest]) |
| 95 | +filters.append(["type":"response","priority":"high","name":SessionPostgresFilter.filterAPIResponse]) |
| 96 | +``` |
| 97 | + |
| 98 | +关于过滤器的使用,请参考范例: [https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Filters.swift](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Filters.swift) |
| 99 | + |
| 100 | +### 追加注册及登录函数 |
| 101 | + |
| 102 | +请将下列函数句柄追加到服务器路由,用于实现注册、登录和注销命令。 |
| 103 | + |
| 104 | +``` swift |
| 105 | +// Login |
| 106 | +routes.append(["method":"get", "uri":"/login", "handler":Handlers.login]) // simply a serving of the login GET |
| 107 | +routes.append(["method":"post", "uri":"/login", "handler":LocalAuthWebHandlers.login]) |
| 108 | +routes.append(["method":"get", "uri":"/logout", "handler":LocalAuthWebHandlers.logout]) |
| 109 | + |
| 110 | +// Register |
| 111 | +routes.append(["method":"get", "uri":"/register", "handler":LocalAuthWebHandlers.register]) |
| 112 | +routes.append(["method":"post", "uri":"/register", "handler":LocalAuthWebHandlers.registerPost]) |
| 113 | +routes.append(["method":"get", "uri":"/verifyAccount/{passvalidation}", "handler":LocalAuthWebHandlers.registerVerify]) |
| 114 | +routes.append(["method":"post", "uri":"/registrationCompletion", "handler":LocalAuthWebHandlers.registerCompletion]) |
| 115 | + |
| 116 | +// JSON |
| 117 | +routes.append(["method":"get", "uri":"/api/v1/session", "handler":LocalAuthJSONHandlers.session]) |
| 118 | +routes.append(["method":"get", "uri":"/api/v1/logout", "handler":LocalAuthJSONHandlers.logout]) |
| 119 | +routes.append(["method":"post", "uri":"/api/v1/register", "handler":LocalAuthJSONHandlers.register]) |
| 120 | +routes.append(["method":"login", "uri":"/api/v1/login", "handler":LocalAuthJSONHandlers.login]) |
| 121 | +``` |
| 122 | + |
| 123 | +详细例子可以参考这里: [https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Routes.swift](https://github.com/PerfectlySoft/Perfect-Local-Auth-PostgreSQL-Template/blob/master/Sources/PerfectLocalAuthPostgreSQLTemplate/configuration/Routes.swift) |
| 124 | + |
| 125 | +## 测试验证模块是否工作 |
| 126 | + |
| 127 | +如果配置成功,您可以通过下列方式获取用户身份编号: |
| 128 | + |
| 129 | +``` swift |
| 130 | +request.session?.userid ?? "" |
| 131 | +``` |
| 132 | + |
| 133 | +如果在具体页面实现中需要用户处于登录状态,您可以用下列代码测试用户是否登录;如果没有登录则自动调用登录过程: |
| 134 | + |
| 135 | +``` swift |
| 136 | +let contextAuthenticated = !(request.session?.userid ?? "").isEmpty |
| 137 | +if !contextAuthenticated { response.redirect(path: "/login") } |
| 138 | +``` |
| 139 | + |
| 140 | + |
| 141 | +### 问题报告、内容贡献和客户支持 |
| 142 | + |
| 143 | +我们目前正在过渡到使用JIRA来处理所有源代码资源合并申请、修复漏洞以及其它有关问题。因此,GitHub 的“issues”问题报告功能已经被禁用了。 |
| 144 | + |
| 145 | +如果您发现了问题,或者希望为改进本文提供意见和建议,[请在这里指出](http://jira.perfect.org:8080/servicedesk/customer/portal/1). |
| 146 | + |
| 147 | +在您开始之前,请参阅[目前待解决的问题清单](http://jira.perfect.org:8080/projects/ISS/issues). |
| 148 | + |
| 149 | +## 更多信息 |
| 150 | +关于本项目更多内容,请参考[perfect.org](http://perfect.org). |
| 151 | + |
| 152 | +## 扫一扫 Perfect 官网微信号 |
| 153 | +<p align=center><img src="https://raw.githubusercontent.com/PerfectExamples/Perfect-Cloudinary-ImageUploader-Demo/master/qr.png"></p> |
0 commit comments