在实现FOSFacebookBundle时,我面临着很大的问题。
我跟踪这些文档并有以下情况:*当用户单击登录时,会出现一个弹出窗口*在用户授予应用程序权限后,FB按钮将被更改(登录)
但是,我的自定义提供程序没有被调用(只调用了一个构造函数)-是的,我使用了一个noobish调试方法(用类方法的名称创建空文件:-)。
有谁知道原因吗?有小费吗?
编辑
经过一段时间的努力解决这个问题,我觉得我迷路了。
再一次,这是我的配置:
app/config/config.yml:
fos_facebook:
file: %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
alias: facebook
app_id: xxx
secret: xxx
cookie: true
permissions: [email, user_location]app/config/routing.yml:
_security_login:
pattern: /login
defaults: { _controller: TestBundle:Main:login }
_security_check:
pattern: /login_check
defaults: { _controller: TestBundle:Main:loginCheck }
_security_logout:
pattern: /logout
defaults: { _controller: TestBundle:Main:logout }app/config/security.yml
security:
factories:
-"%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
providers:
my_fos_facebook_provider:
id: my.facebook.user
fos_userbundle:
id: fos_user.user_manager
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: /login
check_path: /login_check
logout: true
anonymous: true
public:
pattern: ^/.*
fos_facebook:
app_url: "http://www.facebook.com/apps/application.php?id=xxx"
server_url: "http://symfonytest.com.dev/app_dev.php/"
login_path: /login
check_path: /login_check
provider: my_fos_facebook_provider
default_target_path: /
anonymous: true
logout: true我还将代码实现为twig模板,如docs所示(也实现了来自@Matt的代码片段)。
发布于 2011-08-31 20:47:47
我的工作流程与您和我的自定义用户提供程序被正确调用相同,并且一切正常。
您需要检查的第一件事是:您是否有一个JavaScript脚本,在用户通过弹出成功登录到Facebook后将用户重定向到login_check路由?这一点很重要,因为在进行有效身份验证后调用login_check路由将触发Symfony2的安全机制,该机制将调用FOSFacebookBundle特殊安全代码,然后调用您自己的自定义用户提供程序。我想你可能只是错过了这小块。
在这里,使其工作所需的部分JavaScript代码(使用jQuery):
$(document).ready(function() {
Core.facebookInitialize();
});
var Core = {
/**
* Initialize facebook related things. This function will subscribe to the auth.login
* facebook event. When the event is raised, the function will redirect the user to
* the login check path.
*/
facebookInitialize = function() {
FB.Event.subscribe('auth.login', function(response) {
Core.performLoginCheck();
});
};
/**
* Redirect user to the login check path.
*/
performLoginCheck = function() {
window.location = "http://localhost/app_dev.php/login_check";
}
}我把我的security.yml放在这里只是为了帮助您检查与您自己的文件之间的差异:
security:
factories:
- "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
providers:
acme.facebook_provider:
# This is our custom user provider service id. It is defined in config.yml under services
id: acme.user_provider
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
public:
pattern: ^/
fos_facebook:
app_url: "http://www.facebook.com/apps/application.php?id=FACEBOOK_APP_ID"
server_url: "http://localhost/app_dev.php/"
default_target_path: /
login_path: /login
check_path: /login_check
provider: acme.facebook_provider
anonymous: true
logout: true以及我们使用的自定义用户提供程序的服务定义:
services:
acme.user_provider:
class: Application\AcmeBundle\Security\User\Provider\UserProvider
arguments:
facebook: "@fos_facebook.api"
entityManager: "@doctrine.orm.entity_manager"
validator: "@validator"您还需要为/login_check、/login和/logout路径创建一个新的路由。这些路由将由Symfony2为安全进程连接。这里有一个在我的例子中称为MainController的控制器中操作实现的例子:
<?php
namespace Application\AcmeBundle\Controller;
use ...;
class MainController extends Controller
{
/**
* This action is responsible of displaying the necessary informations for
* a user to perform login. In our case, this will be a button to connect
* to the facebook API.
*
* Important notice: This will be called ONLY when there is a problem with
* the login_check or by providing the link directly to the user.
*
* @Route("/{_locale}/login", name = "_security_login", defaults = {"_locale" = "en"})
*/
public function loginAction()
{
if ($this->request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('AcmeBundle:Main:login.html.twig', array(
'error' => $error
));
}
/**
* This action is responsible of checking if the credentials of the user
* are valid. This will not be called because this will be intercepted by the
* security component of Symfony.
*
* @Route("/{_locale}/login_check", name = "_security_check", defaults = {"_locale" = "en"})
*/
public function loginCheckAction()
{
// Call intercepted by the Security Component of Symfony
}
/**
* This action is responsible of login out a user from the site. This will
* not be called because this will be intercepted by the security component
* of Symfony.
*
* @Route("/{_locale}/logout", name = "_security_logout", defaults = {"_locale" = "en"})
*/
public function logoutAction()
{
return $this->redirect('index');
}
}希望这个帮助,如果你有更多的问题或我误解了你的问题,不要犹豫留下评论。
致以敬意,
哑光
https://stackoverflow.com/questions/7257183
复制相似问题