Laravel5中集成Jasigcas一致认证系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Laravel5中集成Jasigcas一致认证系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4703字,纯文字阅读大概需要7分钟。
内容图文
![Laravel5中集成Jasigcas一致认证系统](/upload/InfoBanner/zyjiaocheng/225/315211b0096949ddb472437863822d6e.jpg)
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 $id16 * @return \Illuminate\Auth\UserInterface|null17 */18 public function retrieveById($id) {19 return $this->casUser();20 }21 22 /**23 * Retrieve a user by the given credentials.24 *25 * @param array $credentials26 * @return \Illuminate\Auth\UserInterface|null27 */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 $user36 * @param array $credentials37 * @return bool38 */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 above67 */68 public function retrieveByToken($identifier, $token) {69 return new \Exception('not implemented');70 }71 72 /**73 * Needed by Laravel 4.1.26 and above74 */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 page11 }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/
内容总结
以上是互联网集市为您收集整理的Laravel5中集成Jasigcas一致认证系统全部内容,希望文章能够帮你解决Laravel5中集成Jasigcas一致认证系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。