1

我已经按照捆绑包的文档(标记版本与 symfony 2.012 一起使用):

http://knpbundles.com/FriendsOfSymfony/FOSFacebookBundle

所以当我打算现在登录我的网站时,我遇到了这个错误:

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() 必须返回一个字符串或 NULL

我使用 facebook 登录,但它没有登录我的网站,这是我的配置文件:

//安全.yml

security:
factories:
    - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
providers:

    fos_userbundle:
        id: fos_user.user_manager
    my_fos_facebook_provider:
       id: my.facebook.user    

firewalls:
    main:
        pattern: .*
        fos_facebook:
            app_url: "http://apps.facebook.com/psiware/"
            server_url: "http://localhost/keepinsport/web/app_dev.php/ks"
            login_path: ^/login
            check_path: ^/login_check$
            default_target_path: /
            provider: my_fos_facebook_provider
        form_login:
            provider:     fos_userbundle
            login_path:   /login
            use_forward:  false
            check_path:   /login_check
            failure_path: null
            default_target_path: /ks
        logout:
            path:   /logout
            target: /
            handlers: ["fos_facebook.logout_handler"] 
        anonymous:    true
        remember_me:
          key:      aSecretKey
          #Correspond à un mois
          lifetime: 2592000
          path:     /
          domain:   ~           

encoders:
    Symfony\Component\Security\Core\User\User: plaintext
    FOS\UserBundle\Model\UserInterface: sha512


# C'est ici que tout se passe : qui a accès à quoi ?
access_control:
    - { path: ^/$,          role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login$,     role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register,   role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting,  role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/,     role: ROLE_ADMIN }
    - { path: ^/ks,         role: ROLE_USER }
    - { path: ^/facebook/,  role: [ROLE_FACEBOOK] }
    - { path: ^/.*,         role: [IS_AUTHENTICATED_ANONYMOUSLY] }

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

//我的 config.yml

fos_facebook:
file:   %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
alias:  facebook
app_id: 106071199501870
secret: 219af5b2792ea6267de3cf1017f91c1a
cookie: true
permissions: [email, user_birthday, user_location]

services:
my.facebook.user:
    class: Ks\UserBundle\Security\User\Provider\FacebookProvider
    arguments:
        facebook: "@fos_facebook.api"
        userManager: "@fos_user.user_manager"
        validator: "@validator"

//我的 login.html.twig

{{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();'}) }}
{{ facebook_login_button({'autologoutlink': true}) }}
 <script>
    function goLogIn(){
        window.location = "{{ path('_security_check') }}";
    }

    function onFbInit() {
        if (response.session || response.authResponse) {
            setTimeout(goLogIn, 500);
        } else {
            window.location.href = "{{ path('_security_logout') }}";
        }
    }
</script>  

//我的用户实体

 /**
 * @var string
 *
 * @ORM\Column(name="firstname", type="string", length=255)
 */
protected $firstname;

/**
 * @var string
 *
 * @ORM\Column(name="lastname", type="string", length=255)
 */
protected $lastname;

/**
 * @var string
 *
 * @ORM\Column(name="facebookId", type="string", length=255)
 */
protected $facebookId;

public function serialize()
{
    return serialize(array($this->facebookID, parent::serialize()));
}

public function unserialize($data)
{
    list($this->facebookID, $parentData) = unserialize($data);
    parent::unserialize($parentData);
}

/**
 * @return string
 */
public function getFirstname()
{
    return $this->firstname;
}

/**
 * @param string $firstname
 */
public function setFirstname($firstname)
{
    $this->firstname = $firstname;
}

/**
 * @return string
 */
public function getLastname()
{
    return $this->lastname;
}

/**
 * @param string $lastname
 */
public function setLastname($lastname)
{
    $this->lastname = $lastname;
}

/**
 * Get the full name of the user (first + last name)
 * @return string
 */
public function getFullName()
{
    return $this->getFirstName() . ' ' . $this->getLastname();
}

/**
 * @param string $facebookId
 * @return void
 */
public function setFacebookId($facebookId)
{
    $this->facebookId = $facebookId;
    $this->setUsername($facebookId);
    $this->salt = '';
}

/**
 * @return string
 */
public function getFacebookId()
{
    return $this->facebookId;
}

/**
 * @param Array
 */
public function setFBData($fbdata)
{
    if (isset($fbdata['id'])) {
        $this->setFacebookId($fbdata['id']);
        $this->addRole('ROLE_FACEBOOK');
    }
    if (isset($fbdata['first_name'])) {
        $this->setFirstname($fbdata['first_name']);
    }
    if (isset($fbdata['last_name'])) {
        $this->setLastname($fbdata['last_name']);
    }
    if (isset($fbdata['email'])) {
        $this->setEmail($fbdata['email']);
    }
}

//我的facebookProvider.php

class FacebookProvider implements UserProviderInterface

{ /** * @var \Facebook */ 受保护的 $facebook; 受保护的 $userManager; 受保护的 $validator;

public function __construct(BaseFacebook $facebook, $userManager, $validator)
{
    $this->facebook = $facebook;
    $this->userManager = $userManager;
    $this->validator = $validator;
}

public function supportsClass($class)
{
    return $this->userManager->supportsClass($class);
}

public function findUserByFbId($fbId)
{
    return $this->userManager->findUserBy(array('facebookId' => $fbId));
}

public function loadUserByUsername($username)
{
    $user = $this->findUserByFbId($username);

    try {
        $fbdata = $this->facebook->api('/me');
    } catch (FacebookApiException $e) {
        $fbdata = null;
    }

    if (!empty($fbdata)) {
        if (empty($user)) {
            $user = $this->userManager->createUser();
            $user->setEnabled(true);
            $user->setPassword('');
        }

        // TODO use http://developers.facebook.com/docs/api/realtime
        $user->setFBData($fbdata);

        if (count($this->validator->validate($user, 'Facebook'))) {
            // TODO: the user was found obviously, but doesnt match our expectations, do something smart
            throw new UsernameNotFoundException('The facebook user could not be stored');
        }
        $this->userManager->updateUser($user);
    }

    if (empty($user)) {
        throw new UsernameNotFoundException('The user is not authenticated on facebook');
    }

    return $user;
}

public function refreshUser(UserInterface $user)
{
    if (!$this->supportsClass(get_class($user)) || !$user->getFacebookId()) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }

    return $this->loadUserByUsername($user->getFacebookId());
}

}

我不明白为什么序列化不起作用感谢您的回答。

4

2 回答 2

1

您使用的是哪个版本的 Symfony?对于 Symfony 2.0.X,您需要分支 2.0,而不是 master。检查捆绑文档(这是分支 2.0 之一):

https://github.com/FriendsOfSymfony/FOSFacebookBundle/blob/2.0/README.md

于 2012-06-20T14:14:15.610 回答
0

和提供者的insecurity.yml check_path不应该相同。form_loginfos_facebook

于 2012-04-26T13:40:03.260 回答