Tianjin college, USTB 统一认证系统(SSO)HTTP 客户端 PHP SDK。
使用 Composer 安装:
composer require airmole/tjustb-authsys
SDK 支持通过环境变量或 .env 文件配置。
支持的配置键:
AUTHSYS_URL:统一认证系统根地址,默认 http://authserver.bkty.topAUTHSYS_TIMEOUT:请求超时时间(秒),默认 5AUTHSYS_ENV:自定义 .env 文件绝对路径;若不设置,则默认读取项目根目录上级的 .env(即 src/.. 的同级目录)示例 .env:
AUTHSYS_URL=https://authserver.example.edu AUTHSYS_TIMEOUT=8
也可在运行时设置:
use Airmole\TjustbAuthsys\Base;
$base = new Base();
$base->setAuthsysUrl('https://authserver.example.edu');
use Airmole\TjustbAuthsys\Authsys;
use Airmole\TjustbAuthsys\Exception\Exception;
$auth = new Authsys();
try {
// 1) 获取登录参数(包含 salt、execution 及初始 cookie)
$params = $auth->loginPara();
// 2) 登录
$usercode = '2020xxxxxx';
$password = 'your_password';
$result = $auth->login($usercode, $password, $params);
if ($result['code'] !== 200) {
// 可能是 ['code'=>403,'data'=>'账号或密码错误'] 等
throw new Exception("登录失败: " . $result['data']);
}
// 3) 获取在线应用列表
$apps = $auth->appList(page: 1, pageSize: 50); // ['code'=>200,'data'=>数组或原始字符串]
// 4) 获取应用名称列表
$appNames = $auth->appNameList();
// 5) 获取登录日志
$logs = $auth->loginLogs(
startTime: '2024-09-01 00:00:00',
endTime: '2024-12-31 23:59:59',
page: 1,
pageSize: 20,
result: '',
loginLocation: '',
typeCode: '',
appName: ''
);
// 6) 获取访问应用日志
$accessLogs = $auth->accessAppLogs(
startTime: null,
endTime: null,
page: 1,
pageSize: 10,
result: '',
typeCode: '',
appName: '',
appId: ''
);
// 7) 访问其他系统(统一认证跳转)
$visit = $auth->visitOtherSystem('https://target.example.edu/app');
// $visit = ['url' => 最终跳转URL, 'ticket' => '...']
// 8) 用户配置 & 账号设置(需已登录 cookie)
$userConf = $auth->getUserConf();
$accountSetting = $auth->accountSetting();
// 9) 注销登录
$logoutRes = $auth->logout();
} catch (Exception $e) {
// 捕获 SDK 抛出的异常(网络错误、流程异常等)
echo "异常: " . $e->getMessage();
}
除非特别说明,方法成功时通常返回:
['code' => 200, 'data' => mixed]:data 可能是字符串或已解析为数组(SDK 会尝试 json_decode)Exceptionpublic function loginPara(): array
public function login(string $usercode, string $password, array $params = []): bool|array
['code'=>200,'data'=>'success'],并在对象上设置 usercode 与 cookie['code'=>403,'data'=>'用户名或密码错误'];特殊错误可能抛出 Exceptionpublic function logout(): array
['code'=>200,'data'=>'success']public function appList(int $page = 1, int $pageSize = 100): array
public function appNameList(): array
public function visitOtherSystem(string $targetUrl = ''): array
['url'=>跳转URL,'ticket'=>CAS ticket]public function onlineList(): array
public function loginLogs(...): array
public function accessAppLogs(...): array
appId 等筛选public function getUserConf(): array
public function accountSetting(): array
public function parseCookieString(string $cookieString = ''): array
"Cookie: a=b; c=d" 转数组,并写入对象 cookiepublic function parseCookieArray(array $cookie = []): string
CODE_SUCCESS=200, CODE_REDIRECT=302setConfigPath(string $path=''): voidsetAuthsysUrl(string $url='http://authserver.bkty.top'): voidgetConfig(string $key, $default=null, string $path=''): mixedhttpRequest(string $method, string $url, mixed $body, mixed $cookie, array $headers=[], bool $showHeaders=false, bool $followLocation=false, int $timeout=5): arrayinsertCookie(string $key, string $value): voidgetCookieString(array $cookie=[]): stringparseCookieString(string $cookieString=''): arrayparseCookieArray(array $cookie=[]): stringgetCookieFromHeader(string $key, string $headerString=''): stringgetLocationFromRedirectHeader(string $header=''): stringloginPara(): arraylogin(string $usercode, string $password, array $params=[]): bool|arraylogout(): arrayvalidateLoginResult(array $response): bool|arrayencryptPassword(string $data, string $aesKey): stringdecryptPassword(string $data, string $aesKey): stringrandomString(int $length): stringappList(int $page=1, int $pageSize=100): arrayappNameList(): arrayvisitOtherSystem(string $targetUrl=''): arrayonlineList(): arrayloginLogs(...): arrayaccessAppLogs(...): arraygetUserConf(): arrayaccountSetting(): arrayAirmole\TjustbAuthsys\Exception\Exception['code'=>200,'data'=>...]Login::validateLoginResult):
Exceptionjson_decode;若能解析则将 data 替换为数组loginPara() 再 login()REFERERCE_TOKEN、happyVoyage、MOD_AUTH_CAS、JSESSIONID 等 Cookie,SDK 登录成功后会自动维护 cookieArray 与 cookieStringApp::appNameList 要求请求体携带一个随机数字段,否则服务端可能返回 400,SDK 已内置处理AUTHSYS_URL 指向生产环境认证地址;超时时间可通过 AUTHSYS_TIMEOUT 调整若已从浏览器获取 Cookie,可直接注入后使用:
use Airmole\TjustbAuthsys\Authsys;
$auth = new Authsys();
$cookieString = 'MOD_AUTH_CAS=...; happyVoyage=...; REFERERCE_TOKEN=...; JSESSIONID=...';
$auth->parseCookieString($cookieString);
// 现在可直接调用需要登录态的接口
$apps = $auth->appList();
或从数组生成:
$cookieArray = [
'MOD_AUTH_CAS' => '...',
'happyVoyage' => '...',
'REFERERCE_TOKEN' => '...',
'JSESSIONID' => '...',
];
$cookieString = $auth->parseCookieArray($cookieArray);
.env 中配置测试环境 URL 与合适的超时Exception,以便定位网络/重定向异常GPL-3.0-or-later
鸣谢