php教程 php手册 Laravel5中集成Jasig cas统一认证系统 - mrhyher

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

May 20, 2016 pm 01:15 PM

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

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

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

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

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

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

 

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

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)