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/

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境