Laravel5中集成Jasig cas统一认证系统,laravel5jasig
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/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

The best version of the Apple 16 system is iOS16.1.4. The best version of the iOS16 system may vary from person to person. The additions and improvements in daily use experience have also been praised by many users. Which version of the Apple 16 system is the best? Answer: iOS16.1.4 The best version of the iOS 16 system may vary from person to person. According to public information, iOS16, launched in 2022, is considered a very stable and performant version, and users are quite satisfied with its overall experience. In addition, the addition of new features and improvements in daily use experience in iOS16 have also been well received by many users. Especially in terms of updated battery life, signal performance and heating control, user feedback has been relatively positive. However, considering iPhone14

On April 11, Huawei officially announced the HarmonyOS 4.2 100-machine upgrade plan for the first time. This time, more than 180 devices will participate in the upgrade, covering mobile phones, tablets, watches, headphones, smart screens and other devices. In the past month, with the steady progress of the HarmonyOS4.2 100-machine upgrade plan, many popular models including Huawei Pocket2, Huawei MateX5 series, nova12 series, Huawei Pura series, etc. have also started to upgrade and adapt, which means that there will be More Huawei model users can enjoy the common and often new experience brought by HarmonyOS. Judging from user feedback, the experience of Huawei Mate60 series models has improved in all aspects after upgrading HarmonyOS4.2. Especially Huawei M

Detailed explanation of the method of modifying the system date in the Oracle database. In the Oracle database, the method of modifying the system date mainly involves modifying the NLS_DATE_FORMAT parameter and using the SYSDATE function. This article will introduce these two methods and their specific code examples in detail to help readers better understand and master the operation of modifying the system date in the Oracle database. 1. Modify NLS_DATE_FORMAT parameter method NLS_DATE_FORMAT is Oracle data

Linux and Windows are two common operating systems, representing the open source Linux system and the commercial Windows system respectively. In both operating systems, there is a command line interface for users to interact with the operating system. In Linux systems, users use the Shell command line, while in Windows systems, users use the cmd command line. The Shell command line in Linux system is a very powerful tool that can complete almost all system management tasks.

In which folder are the system fonts located? In modern computer systems, fonts play a vital role, affecting our reading experience and the beauty of text expression. For some users who are keen on personalization and customization, it is particularly important to understand the storage location of system fonts. So, in which folder are system fonts stored? This article will reveal them one by one for everyone. In the Windows operating system, system fonts are stored in a folder called "Fonts". This folder is located in the Win C drive by default.

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,
