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/