阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Laravel5中集成JASIG CAS统一认证系统

111次阅读
没有评论

共计 3744 个字符,预计需要花费 10 分钟才能阅读完成。

CAS:CAS(Central Authentication Service) 是一款不错的针对 Web 应用的单点登录框架,这里介绍下我刚在 laravel5 上搭建成功的 cas。提前准备工作:可运行的 laravel5 的工程,cas 的服务器端已经存在。

环境:Linux(Ubuntu)

一,下载 phpcas 源代码。

在 laravel5 的项目 app 目录下创建 library 目录,下载 phpcas 库,git clone https://github.com/Jasig/phpCAS.git,clone 下来是一个 phpcas 的文件目录。

二,创建 provider

在 app 下创建目录 cas,创建 CasAuthProvider.php,内容如下:

<?php

namespace cas;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\GenericUser;

class CasAuthProvider implements UserProvider {

    /**
    * Retrieve a user by their unique identifier.
    *
    * @param  mixed  $id
    * @return \Illuminate\Auth\UserInterface|null
    */
    public function retrieveById($id) {
        return $this->caSUSEr();
    }

    /**
    * Retrieve a user by the given credentials.
    *
    * @param  array  $credentials
    * @return \Illuminate\Auth\UserInterface|null
    */
    public function retrieveByCredentials(array $credentials) {
        return $this->casUser();
    }

    /**
    * Validate a user against the given credentials.
    *
    * @param  \Illuminate\Auth\UserInterface  $user
    * @param  array  $credentials
    * @return bool
    */
    public function validateCredentials(Authenticatable $user, array $credentials) {
        return true;
    }

    protected function casUser() {
        $cas_host = \Config::get(‘app.cas_host’);
        //dump($cas_host);
        $cas_context = \Config::get(‘app.cas_context’);
        $cas_port = \Config::get(‘app.cas_port’);
        \phpCAS::setDebug();
        \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
        \phpCAS::setNoCasServerValidation();

        if (\phpCAS::isAuthenticated()) {
            $attributes = array(
                ‘id’ => \phpCAS::getUser(),
                ‘name’ => \phpCAS::getUser()
            );
            return new GenericUser($attributes);
        } else {
            //\phpCAS::setServerURL(\Config::get(‘app.url’));
            \phpCAS::forceAuthentication();
        }
        return null;
    }

    /**
    * Needed by Laravel 4.1.26 and above
    */
    public function retrieveByToken($identifier, $token) {
        return new \Exception(‘not implemented’);
    }

    /**
    * Needed by Laravel 4.1.26 and above
    */
    public function updateRememberToken(Authenticatable $user, $token) {
        return new \Exception(‘not implemented’);
    }

}

?>

三,修改 config

在 config/app.php 中添加如下三个配置项:

    ‘cas_host’=>’****’, // 认证服务器
    ‘cas_context’=>”,// 还没弄明白是什么
    ‘cas_port’=>000,// 认证服务端口
    ‘url’=>’http://localhost/’,

四,加载认证库

在 app/providers/AppServiceProvider.php 里,在类 AppServiceProvider 的 register 函数里添加认证方式:

        Auth::extend(‘cas’, function($app) {
            return new CasAuthProvider;
        });

修改 app/config/auth.php 认证 driver:’driver’ => ‘cas’,

在 composer.json 里配置加载项,在 autoload 里的 classmap 中添加如下路径:

    “autoload”: {
        “classmap”: [
            **************
            “app/library”,
            “app/library/phpCAS”,
            “app/cas”
        ]

}

在项目根目录下执行:composer dump-autoload

五,实现

在 app/http/controllers/ 下创建 CasAuthController.php,添加 login 和 logout 方法:

public function login() {

        $message_error = “”;
        if (Auth::check()) {
           
        } else {
            if (Auth::attempt(array())) {
                // Redirect to link after login
            }
            // Redirect to un-logged in page
        }
        dump(\phpCAS::getUser());
        dump(Auth::user());
    }

    public function logout() {

        $cas_host = \Config::get(‘app.cas_host’);
        //dump($cas_host);
        $cas_context = \Config::get(‘app.cas_context’);
        $cas_port = \Config::get(‘app.cas_port’);
        \phpCAS::setDebug();
        \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
        \phpCAS::setNoCasServerValidation();
        \phpCAS::logoutWithRedirectService(\Config::get(‘app.url’));
    }

在 routes.php 里添加路由规则就 OK 了,把项目默认的登陆和注销方法指到这里来,当 login 的时候,会出现服务器的登陆页面。

有个问题,就是这么改动之后,原来我设置的不需要登陆就能浏览的页面,现在进入的时候也会跳出登陆页面,不知道为什么,希望高手指导下,谢谢!

如何在 CentOS 7 / Ubuntu 15.04 上安装 PHP 框架 Laravel http://www.linuxidc.com/Linux/2016-01/127113.htm

Ubuntu 下使用 Nginx 部署 Laravel  http://www.linuxidc.com/Linux/2015-08/121988.htm

Ubuntu 14.04 上使用 Nginx 部署 Laravel 5.0  http://www.linuxidc.com/Linux/2015-08/121986.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-03/129228.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3744字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中