thinkphp如何实现单点登录
一、前提:CAS服务器搭建完成
这个不是本次的重点,不多讲。传送门:https://blog.csdn.net/u013825231/article/details/79132399
二、下载phpCAS客户端
php客户端下载:https://github.com/apereo/phpCAS
php客户端配置的注意事项等内容:https://apereo.atlassian.net/wiki/spaces/CASC/pages/103252517/phpCAS
php客户端的要求:https://apereo.atlassian.net/wiki/spaces/CASC/pages/103252625/phpCAS+requirements
注意:php配置文件php.ini需要开启php_curl,找到 ;extension=php_curl.dll ,将该句前面的分号去掉即可,改为 extension=php_curl.dll
三、thinkphp5引入phpCAS类库
1.下载好的phpCAS客户端文件结构。
相关推荐:《ThinkPHP教程》
2. 把source文件夹复制到thinphp5下的extend文件夹下,并重命名为:phpCAS
3. config.php文件的配置
<?php // The purpose of this central config file is configuring all examples // in one place with minimal work for your working environment // Just configure all the items in this config according to your environment // and rename the file to config.php $phpcas_path = 'phpCAS/'; /////////////////////////////////////// // Basic Config of the phpCAS client // /////////////////////////////////////// $client_domain = 'localhost'; // 客户端 domain $client_path = 'afschool'; $client_secure = false; $client_httpOnly = true; $client_lifetime = 0; // Full Hostname of your CAS Server 服务器主机 $cas_host = 'cas.example.com'; // Context of the CAS Server $cas_context = '/cas'; // Port of your CAS server. Normally for a https server it's 443 $cas_port = 443; // Path to the ca chain that issued the cas server certificate $cas_server_ca_cert_path = '/path/to/cachain.pem'; ////////////////////////////////////////// // Advanced Config for special purposes // ////////////////////////////////////////// // The "real" hosts of clustered cas server that send SAML logout messages // Assumes the cas server is load balanced across multiple hosts $cas_real_hosts = array ( 'cas-real-1.example.com', 'cas-real-2.example.com' ); // Database config for PGT Storage $db = 'pgsql:host=localhost;dbname=phpcas'; //$db = 'mysql:host=localhost;dbname=phpcas'; $db_user = 'phpcasuser'; $db_password = 'mysupersecretpass'; $db_table = 'phpcastabel'; /////////////////////////////////////////// // End Configuration -- Don't edit below // /////////////////////////////////////////// // Generating the URLS for the local cas example services for proxy testing if ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){ $curbase = 'https://'.$_SERVER['SERVER_NAME']; }else{ $curbase = 'http://'.$_SERVER['SERVER_NAME']; } if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) $curbase .= ':'.$_SERVER['SERVER_PORT']; $curdir = dirname($_SERVER['REQUEST_URI'])."/"; // CAS client nodes for rebroadcasting pgtIou/pgtId and logoutRequest $rebroadcast_node_1 = 'http://cas-client-1.example.com'; $rebroadcast_node_2 = 'http://cas-client-2.example.com'; // access to a single service $serviceUrl = $curbase.$curdir.'example_service.php'; // access to a second service $serviceUrl2 = $curbase.$curdir.'example_service_that_proxies.php'; $pgtBase = preg_quote(preg_replace('/^http:/', 'https:', $curbase.$curdir),'/'); $pgtUrlRegexp = '/^'.$pgtBase.'.*$/'; $cas_url = 'https://'.$cas_host; if ($cas_port != '443') { $cas_url = $cas_url.':'.$cas_port; } $cas_url = $cas_url.$cas_context; // Set the session-name to be unique to the current script so that the client script // doesn't share its session with a proxied script. // This is just useful when running the example code, but not normally. session_name('session_for:'.preg_replace('/[^a-z0-9-]/i', '_', basename($_SERVER['SCRIPT_NAME']))); ?>
4. 因为本人请求单点登录的服务器是http认证的,不是https,需要修改CAS/client.php,将其中的https改为http(刚开始没有修改client.php这个文件,总是使用https认证,所以请求失败)
5. 把CAS类库文件夹的同级文件CAS.php,重命名为phpCAS.php
修改成
6. 登录的控制器方法为:
<?php namespace app\index\controller; use think\Db; use think\Loader; class Index extends \think\Controller { public function login() { // Example for a simple client // Load the settings from the central config file require './extend/config.php'; // Loader::import('config.php',EXTEND_PATH); // Load the CAS lib //直接引入phpCAS扩展库下的类文件phpCAS.php Loader::import('phpCAS\phpCAS',EXTEND_PATH); //直接引入库文件需要实例化类 $phpCAS = new \phpCAS(); // Uncomment to enable debugging $phpCAS->setDebug(); // Initialize phpCAS $phpCAS->client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context); // For quick testing you can disable SSL validation of the CAS server. // THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION. // VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL! $phpCAS->setNoCasServerValidation(); //这里会检测服务器端的退出的通知,就能实现php和其他语言平台间同步登出了 $phpCAS->handleLogoutRequests(); //访问CAS的验证通过后,跳转到网页 if($phpCAS->forceAuthentication()){ echo "<script language=\"javascript\">parent.location.href='http://www.baidu.com';</script>"; }; } }
最后访问这个登录的方法,完成单点登录的页面跳转!
以上是thinkphp如何实现单点登录的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

运行 ThinkPHP 项目需要:安装 Composer;使用 Composer 创建项目;进入项目目录,执行 php bin/console serve;访问 http://localhost:8000 查看欢迎页面。

ThinkPHP 拥有多个版本,针对不同 PHP 版本而设计。主要版本包括 3.2、5.0、5.1 和 6.0,而次要版本用于修复 bug 和提供新功能。当前最新稳定版本为 ThinkPHP 6.0.16。在选择版本时,需考虑 PHP 版本、功能需求和社区支持。建议使用最新稳定版本以获得最佳性能和支持。

ThinkPHP Framework 的本地运行步骤:下载并解压 ThinkPHP Framework 到本地目录。创建虚拟主机(可选),指向 ThinkPHP 根目录。配置数据库连接参数。启动 Web 服务器。初始化 ThinkPHP 应用程序。访问 ThinkPHP 应用程序 URL 运行。

《开发建议:如何利用ThinkPHP框架实现异步任务》随着互联网技术的迅猛发展,Web应用程序对于处理大量并发请求和复杂业务逻辑的需求也越来越高。为了提高系统的性能和用户体验,开发人员常常会考虑利用异步任务来执行一些耗时操作,比如发送邮件、处理文件上传、生成报表等。在PHP领域,ThinkPHP框架作为一款流行的开发框架,提供了一些便捷的方式来实现异步任务。

Laravel 和 ThinkPHP 框架的性能比较:ThinkPHP 性能通常优于 Laravel,专注于优化和缓存。Laravel 性能良好,但对于复杂应用程序,ThinkPHP 可能更适合。

ThinkPHP 安装步骤:准备 PHP、Composer、MySQL 环境。使用 Composer 创建项目。安装 ThinkPHP 框架及依赖项。配置数据库连接。生成应用代码。启动应用并访问 http://localhost:8000。

如何使用PHP实现高效稳定的SSO单点登录引言:随着互联网应用的普及,用户面临着大量的注册和登录过程。为了提高用户体验,并减少用户的注册和登录间隔,许多网站和应用开始采用单点登录(SingleSign-On,简称SSO)技术。本文将介绍如何使用PHP实现高效稳定的SSO单点登录,并提供具体的代码示例。一、SSO单点登录原理SSO单点登录是一种身份认证的解决

ThinkPHP 是一款高性能的 PHP 框架,具备缓存机制、代码优化、并行处理和数据库优化等优势。官方性能测试显示,它每秒可处理超过 10,000 个请求,实际应用中被广泛用于京东商城、携程网等大型网站和企业系统。
