搜尋
首頁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客戶端檔案結構。

thinkphp如何實作單一登入

相關推薦:《ThinkPHP教學

2. 把source資料夾複製到thinphp5下的extend資料夾下,並重新命名為:phpCAS

thinkphp如何實作單一登入

thinkphp如何實作單一登入

#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

thinkphp如何實作單一登入

修改成

thinkphp如何實作單一登入

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.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具