>  기사  >  PHP 프레임워크  >  thinkphp가 싱글 사인온(SSO)을 구현하는 방법

thinkphp가 싱글 사인온(SSO)을 구현하는 방법

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼원래의
2019-08-26 11:02:176434검색

thinkphp가 싱글 사인온(SSO)을 구현하는 방법

1. 전제조건: CAS 서버 구축 완료

이는 이 글의 초점이 아니므로 자세한 내용은 다루지 않겠습니다. . 포털: https://blog.csdn.net/u013825231/article/details/79132399

2. 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

3으로 변경합니다. thinkphp5는 phpCAS 클래스 라이브러리

#🎜 🎜#1을 도입합니다. 다운로드된 phpCAS 클라이언트 파일 구조.

thinkphp가 싱글 사인온(SSO)을 구현하는 방법관련 권장 사항: "

ThinkPHP Tutorial

" 2. 소스 폴더 복사 이동 Thinphp5 아래의 확장 폴더로 이동하고 이름을 다음과 같이 바꿉니다: phpCAS


thinkphp가 싱글 사인온(SSO)을 구현하는 방법

thinkphp가 싱글 사인온(SSO)을 구현하는 방법 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. 제가 Single Sign-On을 요청한 서버는 https가 아닌 http 인증 서버이므로 CAS/client.php를 수정하고 https를 http로 변경합니다(처음에는 client.php 파일이 수정되지 않았고 항상 https 인증을 사용하여 요청이 실패했습니다)

5 CAS 클래스 라이브러리 폴더의 형제 파일 CAS.php를 교체합니다. 이름을 phpCAS.php

thinkphp가 싱글 사인온(SSO)을 구현하는 방법으로 지정하고

thinkphp가 싱글 사인온(SSO)을 구현하는 방법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가 싱글 사인온(SSO)을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.