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