thinkcmf集成phpCAS单点登录客户端

@高效码农  May 17, 2019

一、搭建服务端

服务端搭建请参考:https://blog.csdn.net/fireofjava/article/details/79142703

二、导入phpCAS依赖

在thinkCMF项目根目录下的composer.json文件中添加"apereo/phpcas": "^1.3.7"
composer.json文件示例:

{
    "name": "thinkcmf/thinkcmf",
    "description": "ThinkCMF based on ThinkPHP 5.0 , it is a free and open source Content Management Framework(CMF)",
    "type": "project",
    "keywords": [
        "cmf",
        "thinkcmf",
        "framework",
        "thinkphp",
        "ORM"
    ],
    "homepage": "http://www.thinkcmf.com/",
    "license": "Apache-2.0",
    "authors": [
        {
            "name": "thinkcmf",
            "email": "zxxjjforever@163.com"
        }
    ],
    "require": {
        "php": ">=5.4.0",
        "ext-json": "*",
        "topthink/framework": "~5.0.0",
        "topthink/think-helper": "^1.0",
        "topthink/think-captcha": "^1.0",
        "phpmailer/phpmailer": "^5.2",
        "mindplay/annotations": "^1.3",
        "topthink/think-image": "^1.0",
        "ezyang/htmlpurifier": "^4.9",
        "topthink/think-queue": "^1.1.4",
        "electrolinux/phpquery": "^0.9.6",
        "apereo/phpcas": "^1.3.7"
    },
    "extra": {
        "think-path": "simplewind/thinkphp"
    },
    "config": {
        "vendor-dir": "simplewind/vendor"
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
}

导入依赖完成后目录:
2019-05-16T10:05:30.png

注意:如果是http访问需要修改CAS/client.php文件中的317行https修改为http
2019-05-16T10:29:26.png

三、新建cas应用

在app目录下新建cas应用目录如:
2019-05-16T10:06:45.png
IndexController 类中的代码

<?php

namespace app\cas\controller;

use cmf\controller\HomeBaseController;
use phpCAS;

class IndexController extends HomeBaseController
{
    public function index()
    {
        phpCAS::setDebug();
        // Enable verbose error messages. Disable in production!
        phpCAS::setVerbose(true);
        // Initialize phpCAS
        phpCAS::client(CAS_VERSION_2_0, "localhost", 8443, "cas");
        // For production use set the CA certificate that is the issuer of the cert
        // on the CAS server and uncomment the line below
        // phpCAS::setCasServerCACert($cas_server_ca_cert_path);
        // 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();
        // force CAS authentication
        phpCAS::forceAuthentication();
        // at this step, the user has been authenticated by the CAS server
        // and the user's login name can be read with phpCAS::getUser().
        // logout if desired
        if (isset($_REQUEST['logout'])) {
            phpCAS::logout();
        }
        echo phpCAS::getVersion();
        $this->assign("user", phpCAS::getUser());
        $this->fetch();

    }
}

四:访问

在浏览器中访问:http://localhost/thinkcas/public/cas

thinkcas是你自己的项目名称

2019-05-16T10:30:45.png

输入用户名/密码:casuser::Mellon

登录成功后:显示phpCAS的版本号1.3.7



添加新评论