sentrasoft/laravel-cas 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sentrasoft/laravel-cas

Composer 安装命令:

composer require sentrasoft/laravel-cas

包简介

Apereo CAS Authentication for Laravel

README 文档

README

Donate Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License

Easy Bring to CAS Authentication for Laravel

Install

Via Composer

$ composer require sentrasoft/laravel-cas

Via edit composer.json

"require": {
	"sentrasoft/laravel-cas": "dev-master"
}

Next, update Composer from the Terminal:

$ composer update

Configuration

After updating composer, add the ServiceProvider to the providers array in config/app.php.

'providers' => array(
    .....
    Sentrasoft\Cas\CasServiceProvider::class,
);

Now add the alias in config/app.php.

'aliases' => array(
    ......
    'Cas' => Sentrasoft\Cas\Facades\Cas::class,
);

Add the middelware to your Kernel.php file or leverage your own:

'cas.auth'  => \Sentrasoft\Cas\Middleware\Authenticate::class,
'cas.guest' => \Sentrasoft\Cas\Middleware\RedirectIfAuthenticated::class,

Now publish the configuration cas.php file:

$ php artisan vendor:publish --provider="Sentrasoft\Cas\CasServiceProvider" --tag="config"

Add new environment variables below to your .env

CAS_HOSTNAME=cas.example.com
CAS_VALIDATION=https://cas.example.com/cas/p3/serviceValidate
CAS_VERSION=3.0
CAS_LOGOUT_URL=https://cas.example.com/cas/logout

To see further configuration, please see and read the description for each configuration item config/cas.php

Route

Authentication

Redirect the user to the authentication page for the provider.

Route::get('/cas/login', function() {
    return cas()->authenticate();
})->name('cas.login');

Controller and Callback Route

You can create a new controller named Auth\CasController.

php artisan make:controller Auth\CasController
class CasController extends Controller
{
    /**
     * Obtain the user information from CAS.
     *
     * @return Illuminate\Http\RedirectResponse
     */
    public function callback()
    {
        // $username = Cas::getUser();
        // Here you can store the returned information in a local User model on your database (or storage).

        // This is particularly usefull in case of profile construction with roles and other details
        // e.g. Auth::login($local_user);

        return redirect()->route('home');
    }
}

When the authentication is performed the callback url is invoked. In that callback you can process the User and create a local entry in the database.

Route::get('/cas/callback', 'Auth\CasController@callback')->name('cas.callback');

Logout

Logout of the CAS Session and redirect users.

Route::post('/cas/logout', [ 'middleware' => 'cas.auth', function() {
    cas()->logout();

    // You can also add @param string $url in param[0]
    cas()->logout(url('/'));

    // Or add @param string $service in param[1]
    cas()->logout('', url('/'));

}])->name('cas.logout');

The cas.auth middleware is optional, but you will need to handle the error when a user tries to logout when they do not have a CAS Session.

If the CAS_LOGOUT_REDIRECT configuration item in .env is added, the value is taken from that configuration. Or if nothing is configured, the value is taken based on the value you specified.

If you want to use SLO (Single Logout) (if the CAS server supports SLO), Your application must have a valid SSL and the CAS server must be able to send HTTP POST /cas/logout without having to verify CsrfToken. Therefore, you must change the App\Http\Middleware\VerifyCsrfToken file and exclude /cas/logout route.

/**
 * The URIs that should be excluded from CSRF verification.
 *
 * @var array
 */
protected $except = [
    //

    '/cas/logout',
];

You can check that it works by trying to send an HTTP POST via cURL.

curl -X POST https://yourapp.com/cas/logout

Usage

Get User

To retrieve authenticated credentials.

Not ID (integer), but given on the CAS login (username) form.

$uid = Cas::user()->id;

Get User Attributes

Get the attributes for for the currently connected user.

foreach (Cas::user()->getAttributes() as $key => $value) {
	...
}

Get User Attribute

Retrieve a specific attribute by key name. The attribute returned can be either a string or an array based on matches.

$value = Cas::user()->getAttribute('key');

Support Us

Donate

Help us to keep making awesome stuff. You don't have to be a developer to support our open source work. If you want to receive personal support, or just feel all warm and fuzzy inside from helping open source development, donations are very welcome. Thank you.

License

The MIT License (MIT). Please see License File for more information.

sentrasoft/laravel-cas 适用场景与选型建议

sentrasoft/laravel-cas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.38k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 04 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「SSO」 「laravel」 「cas」 「phpCAS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sentrasoft/laravel-cas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sentrasoft/laravel-cas 我们能提供哪些服务?
定制开发 / 二次开发

基于 sentrasoft/laravel-cas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-09