jacekkow / uphpcas
简单的PHP CAS认证库
v1.6.0
2020-10-05 15:46 UTC
Requires
- php: >=5.3.0
README
简单的PHP CAS认证库
介绍
这个库旨在成为复杂过度的 phpCAS 库的替代品。
它只支持基本的 CAS协议,没有代理功能,这对于网站认证已经足够。
使用方法
Composer
-
添加 jacekkow/uphpcas 依赖
composer require jacekkow/uphpcas
-
在您的应用程序中包含自动加载器
<?php require_once(__DIR__ . '/vendor/autoload.php');
-
请参阅下面的使用示例
原始使用方法
-
下载 uphpCAS.php
-
将其包含在您的应用程序中
<?php require_once(__DIR__ . '/uphpCAS.php');
-
请参阅下面的使用示例
示例
需要认证
要求访问页面需要认证
<?php require_once('uphpCAS.php'); try { $cas = new uphpCAS('https://cas.server.local/cas'); $user = $cas->authenticate(); echo 'Authenticated as '.$user->user; } catch(Exception $e) { echo 'Jasig authentication failed: '.$e->getMessage(); die(); }
登录和注销页面
index.php
<?php require_once('uphpCAS.php'); $cas = new uphpCAS(); if($cas->isAuthenticated()) { $user = $cas->authenticate(); echo 'Authenticated as '.$user->user; } else { echo 'Not authenticated. <a href="login.php">Log in</a>'; }
login.php
<?php require_once('uphpCAS.php'); try { $cas = new uphpCAS('https://cas.server.local/cas'); $user = $cas->authenticate(); header('Location: index.php'); } catch(Exception $e) { echo 'Jasig authentication failed: '.$e->getMessage(); die(); }
logout.php
<?php require_once('uphpCAS.php'); try { $cas = new uphpCAS('https://cas.server.local/cas'); $user = $cas->logout(); } catch(Exception $e) { echo 'Jasig authentication failed: '.$e->getMessage(); die(); }
常见问题
CAS服务器重定向无效
默认情况下,uphpCAS会尝试猜测正确的URL并将其作为“服务”参数传递给CAS服务器(请参阅getCurrentUrl()方法)。该URL由CAS服务器用于在成功登录后重定向用户回应用程序。
如果这个猜测是错误的,例如当服务器位于代理后面时,您可以使用setServiceUrl()方法覆盖它
$cas = new uphpCAS('https://cas.server.local/cas'); $cas->setServiceUrl('https://service.local/subpage');
或构造函数的第二个参数
$cas = new uphpCAS('https://cas.server.local/cas', 'https://service.local/subpage');
HTTP POST问题
从CAS服务器传递“票据”到应用程序的标准方法是使用HTTP GET方法。为了避免在单页应用程序的URL中添加额外的“票据”参数,这些参数可能会过期并导致uphpCAS抛出异常,该库默认使用POST方法。
您可以使用setMethod()将方法更改为HTTP GET
$cas = new uphpCAS('https://cas.server.local/cas'); $cas->setMethod('GET');
HTTPS上的CAS
这个库强制执行CAS证书验证。CAS服务器的主机名必须与提供的SSL证书中的主机名匹配。此外,证书必须由CA存储中包含的CA签名(或者自签名 - 然后,证书本身必须包含)。默认情况下,它会在
- /etc/ssl/certs/ca-certificates.crt
- /etc/ssl/certs/ca-bundle.crt
- /etc/pki/tls/certs/ca-bundle.crt
您可以使用setCaFile()方法更改CA存储文件路径
$cas = new uphpCAS('https://cas.server.local/cas'); $cas->setCaFile('./localStore.pem');