搜索
首页php框架ThinkPHPthinkphp如何实现单点登录

thinkphp如何实现单点登录

Aug 26, 2019 am 11:02 AM
thinkphp单点登录

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客户端文件结构。

z.png

相关推荐:《ThinkPHP教程

2. 把source文件夹复制到thinphp5下的extend文件夹下,并重命名为:phpCAS

x.png

c.png

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 = &#39;phpCAS/&#39;;
 
///////////////////////////////////////
// Basic Config of the phpCAS client //
///////////////////////////////////////
$client_domain = &#39;localhost&#39;; // 客户端 domain 
$client_path = &#39;afschool&#39;;
$client_secure = false;
$client_httpOnly = true;
$client_lifetime = 0;
 
// Full Hostname of your CAS Server 服务器主机
$cas_host = &#39;cas.example.com&#39;;
 
// Context of the CAS Server  
$cas_context = &#39;/cas&#39;;
 
// Port of your CAS server. Normally for a https server it&#39;s 443
$cas_port = 443;
 
// Path to the ca chain that issued the cas server certificate
$cas_server_ca_cert_path = &#39;/path/to/cachain.pem&#39;;
 
//////////////////////////////////////////
// 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 (
&#39;cas-real-1.example.com&#39;,
&#39;cas-real-2.example.com&#39;
);
 
// Database config for PGT Storage
$db = &#39;pgsql:host=localhost;dbname=phpcas&#39;;
//$db = &#39;mysql:host=localhost;dbname=phpcas&#39;;
$db_user = &#39;phpcasuser&#39;;
$db_password = &#39;mysupersecretpass&#39;;
$db_table = &#39;phpcastabel&#39;;
 
///////////////////////////////////////////
// End Configuration -- Don&#39;t edit below //
///////////////////////////////////////////
 
// Generating the URLS for the local cas example services for proxy testing
if ( isset($_SERVER[&#39;HTTPS&#39;]) && $_SERVER[&#39;HTTPS&#39;] == &#39;on&#39;){
$curbase = &#39;https://&#39;.$_SERVER[&#39;SERVER_NAME&#39;];
}else{
$curbase = &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;];
}
if ($_SERVER[&#39;SERVER_PORT&#39;] != 80 && $_SERVER[&#39;SERVER_PORT&#39;] != 443)
$curbase .= &#39;:&#39;.$_SERVER[&#39;SERVER_PORT&#39;];
 
$curdir = dirname($_SERVER[&#39;REQUEST_URI&#39;])."/";
 
 
// CAS client nodes for rebroadcasting pgtIou/pgtId and logoutRequest
$rebroadcast_node_1 = &#39;http://cas-client-1.example.com&#39;;
$rebroadcast_node_2 = &#39;http://cas-client-2.example.com&#39;;
 
// access to a single service
$serviceUrl = $curbase.$curdir.&#39;example_service.php&#39;;
// access to a second service
$serviceUrl2 = $curbase.$curdir.&#39;example_service_that_proxies.php&#39;;
 
$pgtBase = preg_quote(preg_replace(&#39;/^http:/&#39;, &#39;https:&#39;, $curbase.$curdir),&#39;/&#39;);
$pgtUrlRegexp = &#39;/^&#39;.$pgtBase.&#39;.*$/&#39;;
 
$cas_url = &#39;https://&#39;.$cas_host;
if ($cas_port != &#39;443&#39;)
{
$cas_url = $cas_url.&#39;:&#39;.$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&#39;t share its session with a proxied script.
// This is just useful when running the example code, but not normally.
session_name(&#39;session_for:&#39;.preg_replace(&#39;/[^a-z0-9-]/i&#39;, &#39;_&#39;, basename($_SERVER[&#39;SCRIPT_NAME&#39;])));
?>

4. 因为本人请求单点登录的服务器是http认证的,不是https,需要修改CAS/client.php,将其中的https改为http(刚开始没有修改client.php这个文件,总是使用https认证,所以请求失败)

5. 把CAS类库文件夹的同级文件CAS.php,重命名为phpCAS.php

v.png

修改成

b.png

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 &#39;./extend/config.php&#39;;
        // Loader::import(&#39;config.php&#39;,EXTEND_PATH);
        // Load the CAS lib
        //直接引入phpCAS扩展库下的类文件phpCAS.php
        Loader::import(&#39;phpCAS\phpCAS&#39;,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=&#39;http://www.baidu.com&#39;;</script>";
 
        };
        
     }
}

最后访问这个登录的方法,完成单点登录的页面跳转!

以上是thinkphp如何实现单点登录的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
ThinkPHP内置测试框架的关键功能是什么?ThinkPHP内置测试框架的关键功能是什么?Mar 18, 2025 pm 05:01 PM

本文讨论了ThinkPHP的内置测试框架,突出了其关键功能(例如单元和集成测试),以及它如何通过早期的错误检测和改进的代码质量来增强应用程序可靠性。

如何使用ThinkPHP来构建实时股票市场数据源?如何使用ThinkPHP来构建实时股票市场数据源?Mar 18, 2025 pm 04:57 PM

文章讨论了使用ThinkPHP进行实时股票市场数据提要,重点是设置,数据准确性,优化和安全措施。

在无服务器体系结构中使用ThinkPHP的关键注意事项是什么?在无服务器体系结构中使用ThinkPHP的关键注意事项是什么?Mar 18, 2025 pm 04:54 PM

本文讨论了在无服务器体系结构中使用ThinkPHP的关键注意事项,专注于性能优化,无状态设计和安全性。它突出了诸如成本效率和可扩展性之类的收益,但也应对挑战

如何在ThinkPHP微服务中实现服务发现和负载平衡?如何在ThinkPHP微服务中实现服务发现和负载平衡?Mar 18, 2025 pm 04:51 PM

本文讨论了在ThinkPHP微服务中实施服务发现和负载平衡,重点是设置,最佳实践,集成方法和推荐工具。[159个字符]

ThinkPHP依赖性注入容器的高级功能是什么?ThinkPHP依赖性注入容器的高级功能是什么?Mar 18, 2025 pm 04:50 PM

ThinkPHP的IOC容器提供了高级功能,例如懒惰加载,上下文绑定和方法注入PHP App中有效依赖性管理的方法。Character计数:159

如何使用ThinkPHP来构建实时协作工具?如何使用ThinkPHP来构建实时协作工具?Mar 18, 2025 pm 04:49 PM

本文讨论了使用ThinkPHP来构建实时协作工具,重点关注设置,Websocket集成和安全性最佳实践。

使用ThinkPHP来构建SaaS应用程序的主要好处是什么?使用ThinkPHP来构建SaaS应用程序的主要好处是什么?Mar 18, 2025 pm 04:46 PM

ThinkPHP具有轻巧的设计,MVC架构和可扩展性。它通过各种功能提高可扩展性,加快开发并提高安全性。

如何使用ThinkPHP和RabbitMQ构建分布式任务队列系统?如何使用ThinkPHP和RabbitMQ构建分布式任务队列系统?Mar 18, 2025 pm 04:45 PM

本文概述了使用ThinkPhp和RabbitMQ构建分布式任务队列系统,重点是安装,配置,任务管理和可扩展性。关键问题包括确保高可用性,避免常见的陷阱,例如不当

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。