推荐:HWIOAuthBundle - 简化OAuth身份验证的PHP库
    		       		warning:
    		            这篇文章距离上次修改已过451天,其中的内容可能已经有所变动。
    		        
        		                
                HWIOAuthBundle是一个用于简化OAuth身份验证流程的PHP库。以下是如何使用HWIOAuthBundle来集成OAuth身份验证到Symfony项目的步骤和示例代码。
- 安装HWIOAuthBundle:
 
composer require hwi/oauth-bundle- 将HWIOAuthBundle添加到你的
app/AppKernel.php文件中(如果是Symfony 2.x)或config/bundles.php文件中(如果是Symfony 3.x或4.x): 
// Symfony 2.x 的 AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
            // ...
        );
        // ...
    }
}
// Symfony 3.x 或 4.x 的 bundles.php
return [
    // ...
    HWI\Bundle\OAuthBundle\HWIOAuthBundle::class => ['all' => true],
    // ...
];- 配置
config.yml文件以添加OAuth提供商: 
# app/config/config.yml
hwi_oauth:
    firewall_name: oauth_firewall
    resource_owners:
        any_name:
            type: google
            client_id: <client_id>
            client_secret: <client_secret>- 配置security.yml以创建新的firewall:
 
# app/config/security.yml
security:
    firewalls:
        oauth_firewall:
            oauth:
                resource_owners:
                    any_name: "/connect/oauth/service"
                token_param: oauth_token
                use_forward: false
                check_path: /connect/oauth/check
                failure_path: /connect/oauth/failure- 创建控制器以处理OAuth流程:
 
// src/AppBundle/Controller/ConnectController.php
namespace AppBundle\Controller;
 
use HWI\Bundle\OAuthBundle\Controller\DefaultController as HWIOAuthController;
 
class ConnectController extends HWIOAuthController
{
    // 可以在这里添加自定义逻辑
}- 确保你的应用程序有路由来匹配上述配置中的路径。
 
这样,你就可以通过定义的firewall_name在应用中触发OAuth身份验证流程。用户将被重定向到指定的OAuth服务进行登录,登录成功后,会被重定向回你的应用,并带上授权码或令牌。
评论已关闭