Home Backend Development PHP Tutorial Laravel5中集成Jasig cas一致认证系统

Laravel5中集成Jasig cas一致认证系统

Jun 13, 2016 pm 12:28 PM
app cas config nbsp

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> ?>
Copy after login

三,修改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>     }
Copy after login

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

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

 

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

How to perform real-name authentication on Jingdong Mall APP How to perform real-name authentication on Jingdong Mall APP Mar 19, 2024 pm 02:31 PM

How to get real-name authentication on Jingdong Mall APP? Jingdong Mall is an online shopping platform that many friends often use. Before shopping, it is best for everyone to conduct real-name authentication so that they can enjoy complete services and get a better shopping experience. The following is the real-name authentication method for JD.com, I hope it will be helpful to netizens. 1. Install and open JD.com, and then log in to your personal account; 2. Then click [My] at the bottom of the page to enter the personal center page; 3. Then click the small [Settings] icon in the upper right corner to go to the setting function interface; 4. Select [Account and Security] to go to the account settings page; 5. Finally, click the [Real-name Authentication] option to fill in the real-name information; 6. The installation system requires you to fill in your real personal information and complete the real-name authentication

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

See all articles