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,内容如下:

 1 <?php
 2 
 3 namespace cas;
 4 
 5 use Illuminate\Contracts\Auth\UserProvider;
 6 use Illuminate\Contracts\Hashing\Hasher;
 7 use Illuminate\Contracts\Auth\Authenticatable;
 8 use Illuminate\Auth\GenericUser;
 9 
10 class CasAuthProvider implements UserProvider {
11 
12     /**
13      * Retrieve a user by their unique identifier.
14      *
15      * @param  mixed  $id
16      * @return \Illuminate\Auth\UserInterface|null
17      */
18     public function retrieveById($id) {
19         return $this->casUser();
20     }
21 
22     /**
23      * Retrieve a user by the given credentials.
24      *
25      * @param  array  $credentials
26      * @return \Illuminate\Auth\UserInterface|null
27      */
28     public function retrieveByCredentials(array $credentials) {
29         return $this->casUser();
30     }
31 
32     /**
33      * Validate a user against the given credentials.
34      *
35      * @param  \Illuminate\Auth\UserInterface  $user
36      * @param  array  $credentials
37      * @return bool
38      */
39     public function validateCredentials(Authenticatable $user, array $credentials) {
40         return true;
41     }
42 
43     protected function casUser() {
44         $cas_host = \Config::get('app.cas_host');
45         //dump($cas_host);
46         $cas_context = \Config::get('app.cas_context');
47         $cas_port = \Config::get('app.cas_port');
48         \phpCAS::setDebug();
49         \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
50         \phpCAS::setNoCasServerValidation();
51 
52         if (\phpCAS::isAuthenticated()) {
53             $attributes = array(
54                 'id' => \phpCAS::getUser(),
55                 'name' => \phpCAS::getUser()
56             );
57             return new GenericUser($attributes);
58         } else {
59             //\phpCAS::setServerURL(\Config::get('app.url'));
60             \phpCAS::forceAuthentication();
61         }
62         return null;
63     }
64 
65     /**
66      * Needed by Laravel 4.1.26 and above
67      */
68     public function retrieveByToken($identifier, $token) {
69         return new \Exception('not implemented');
70     }
71 
72     /**
73      * Needed by Laravel 4.1.26 and above
74      */
75     public function updateRememberToken(Authenticatable $user, $token) {
76         return new \Exception('not implemented');
77     }
78 
79 }
80 
81 ?>

三,修改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方法:

 1  public function login() {
 2 
 3         $message_error = "";
 4         if (Auth::check()) {
 5             
 6         } else {
 7             if (Auth::attempt(array())) {
 8                 // Redirect to link after login
 9             }
10             // Redirect to un-logged in page
11         }
12         dump(\phpCAS::getUser());
13         dump(Auth::user());
14     }
15 
16     public function logout() {
17 
18         $cas_host = \Config::get('app.cas_host');
19         //dump($cas_host);
20         $cas_context = \Config::get('app.cas_context');
21         $cas_port = \Config::get('app.cas_port');
22         \phpCAS::setDebug();
23         \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
24         \phpCAS::setNoCasServerValidation();
25         \phpCAS::logoutWithRedirectService(\Config::get('app.url'));
26     }

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

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

 

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

posted @ 2016-03-15 14:52  mrhyher  阅读(1620)  评论(0编辑  收藏  举报