Rumah  >  Artikel  >  php教程  >  Laravel5中集成Jasig cas统一认证系统,laravel5jasig

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

WBOY
WBOYasal
2016-06-13 08:44:32814semak imbas

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

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

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

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

 

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php常用图片处理类,Artikel seterusnya:文件缓存,php文件缓存