Home > Article > Backend Development > Integrate Jasig cas unified authentication system in Laravel5, laravel5jasig_PHP tutorial
CAS: CAS (Central Authentication Service) is a good single sign-on framework for web applications. Here I will introduce the cas that I just successfully built on laravel5. Preparation work in advance: The laravel5 project can be run, and the cas server side already exists.
Environment: Linux (ubuntu)
1. Download the phpcas source code.
Create the library directory in the laravel5 project app directory, download the phpcas library, git clone https://github.com/Jasig /phpCAS.git, cloned is a phpcas file directory.
2. Create provider
Create the directory cas under the app and create CasAuthProvider.php with the following content:
<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> ?>
3. Modify config
Add the following three configuration items in config/app.php:
'cas_host'=>'****', //Authentication server
'cas_context'=>'',//I haven't figured out what it is yet
'cas_port'=>000 ,//Authentication service port
'url'=>'http://localhost/',
4. Load the authentication library
In app/providers/AppServiceProvider.php, add the authentication method in the register function of class AppServiceProvider:
Auth::extend('cas', function($app) {
return new CasAuthProvider;
});
Modify app/config/auth.php authentication driver: 'driver' => 'cas',
Configure the add-in in composer.json and add the following path to the classmap in autoload:
"autoload": {
"classmap": [
"app/library" "app/library",
"app/library/ phpCAS",
"app/cas"
]
Execute in the project root directory: composer dump-autoload
Five, realize
Create CasAuthController.php under app/http/controllers/ and add login and logout methods:
<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> }It’s OK to add routing rules in routes.php. Point the project’s default login and logout methods here. When logging in, the server’s login page will appear.
I have a problem. After making this change, the page I originally set up to be able to browse without logging in will now jump out of the login page when I enter it. I don’t know why. I hope an expert can guide me. Thank you!
Reference: https://sonnguyen.ws/how-to-integrate-phpcas-in-laravel/