silents / yii2-auth-cas
为 Yii2 提供的 CAS 认证
Requires
- php: >=7.4.0
- apereo/phpcas: ^1.6
This package is not auto-updated.
Last update: 2024-10-02 18:43:56 UTC
README
使用 phpCAS 库实现的 CAS 认证功能的 Yii2 库。
用法
-
使用
composer require silecs/yii2-auth-cas
将其添加到项目中 -
配置 Yii2 应用程序,例如在
backend/config/main.php
中return [ ... 'modules' => [ 'cas' => [ 'class' => 'silecs\yii2auth\cas\CasModule', 'config' => [ 'host' => 'ssoserver.example.com', 'port' => '443', 'path' => '/idp/profile/cas', // optional parameters 'certfile' => '', // empty, or path to a SSL cert, or false to ignore certs 'debug' => true, // will add many logs into X/runtime/logs/cas.log ], ],
-
在
SiteController
中添加使用此 CAS 模块的操作public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } return $this->redirect(['/cas/auth/login']); } public function actionLogout() { if (Yii::$app->user->isGuest) { return $this->redirect(['/cas/auth/logout']); } return $this->goHome(); }
注意
实现 yii\web\IdentityInterface
的 user
组件将在查询 CAS 服务器后用于获取本地配置文件。这意味着,如果 User
是应用程序组件,并且 CAS 返回的用户名为 "bibendum",则只有当 User::findIdentity("bibendum")
的结果不为空时,认证才会成功。
操作路径 '/cas/auth/login' 以应用程序配置中定义的模块别名开头,例如在 'modules' => [ 'cas' => [ ... ] ]
中的 'cas'
。
使用 CAS 容器进行测试
以下是一些部署 Docker CAS 服务器以测试此库的说明。此过程将使用 Shibboleth 实例的 CAS 接口。这在 Debian Stretch 和 Buster(测试版)上进行了测试。
-
从 docker.io 的额外仓库安装
docker
(我使用官方 Debian 仓库中的旧版 docker 时遇到了错误)。 -
从 Debian 或 docker.io 安装
docker-compose
。 -
Git 克隆 https://hub.docker.com/r/unicon/shibboleth-idp/ 如果使用旧版 docker-compose,则切换到 3c29f10,因为后续提交需要较新的功能。
-
修改
docker-compose.yml
以确保容器不会尝试使用端口 80,因此将"80:80"
替换为"8080:80"
。 -
如果您的本地 Yii2 应用程序未使用 HTTPS,请将
idp/shibboleth-idp/conf/cas-protocol.xml
中的c:regex="https://idptestbed/.*"
替换为c:regex="https?://idptestbed/.*"
。 -
将
127.0.0.1 idptestbed
添加到/etc/hosts
,作为 root。 -
配置您的 Yii2 应用程序以使用
'host' => 'idptestbed', 'port' => '443', 'path' => '/idp/profile/cas', 'certfile' => false, 'debug' => true,
-
启动容器
docker-compose build docker-compose run
-
访问您的 Yii2 应用的登录页面。
-
在容器终端中按 Ctrl-C 结束它们。
如果您想向 CAS 添加用户,可以修改 ldap/users.ldif
。别忘了在此之后重建 Docker 镜像。