thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

一、前提:CAS服務(wù)器搭建完成

這個(gè)不是本次的重點(diǎn),不多講。傳送門(mén):https://blog.csdn.net/u013825231/article/details/79132399

二、下載phpCAS客戶端

php客戶端下載:https://github.com/apereo/phpCAS

立即學(xué)習(xí)PHP免費(fèi)學(xué)習(xí)筆記(深入)”;

php客戶端配置的注意事項(xiàng)等內(nèi)容: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需要開(kāi)啟php_curl,找到 ;extension=php_curl.dll ,將該句前面的分號(hào)去掉即可,改為 extension=php_curl.dll

三、thinkphp5引入phpCAS類庫(kù)

1.下載好的phpCAS客戶端文件結(jié)構(gòu)。

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

相關(guān)推薦:《ThinkPHP教程

2. 把source文件夾復(fù)制到thinphp5下的extend文件夾下,并重命名為:phpCAS

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

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 服務(wù)器主機(jī) $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. 因?yàn)楸救苏?qǐng)求單點(diǎn)登錄的服務(wù)器是http認(rèn)證的,不是https,需要修改CAS/client.php,將其中的https改為http(剛開(kāi)始沒(méi)有修改client.php這個(gè)文件,總是使用https認(rèn)證,所以請(qǐng)求失敗)

5. 把CAS類庫(kù)文件夾的同級(jí)文件CAS.php,重命名為phpCAS.php

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

修改成

thinkphp如何實(shí)現(xiàn)單點(diǎn)登錄

6. 登錄的控制器方法為:

<?php namespace appindexcontroller; use thinkDb; use thinkLoader;   class Index extends thinkController {     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擴(kuò)展庫(kù)下的類文件phpCAS.php         Loader::import(&#39;phpCASphpCAS&#39;,EXTEND_PATH);                 //直接引入庫(kù)文件需要實(shí)例化類         $phpCAS = new phpCAS();         // Uncomment to enable debugging         $phpCAS->setDebug(); ???????? ????????//?Initialize?phpCAS ????????$phpCAS-&gt;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-&gt;setNoCasServerValidation(); ? ????????//這里會(huì)檢測(cè)服務(wù)器端的退出的通知,就能實(shí)現(xiàn)php和其他語(yǔ)言平臺(tái)間同步登出了 ????????$phpCAS-&gt;handleLogoutRequests(); ? ????????//訪問(wèn)CAS的驗(yàn)證通過(guò)后,跳轉(zhuǎn)到網(wǎng)頁(yè) ????????if($phpCAS-&gt;forceAuthentication()){? ? ????????echo?"<script>parent.location.href=&#39;http://www.baidu.com&#39;;</script>"; ? ????????}; ???????? ?????} }

最后訪問(wèn)這個(gè)登錄的方法,完成單點(diǎn)登錄的頁(yè)面跳轉(zhuǎn)!

以上就是

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊13 分享