Laravel5中集成Jasig cas统一认证系统

时间:2023-03-09 02:05:58
Laravel5中集成Jasig cas统一认证系统

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的时候,会出现服务器的登陆页面。

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

参考:https://sonnguyen.ws/how-to-integrate-phpcas-in-laravel/