1

我正在尝试实现 Facebook 登录到我的网站。我也使用 FOSUserBundle。使用 FOSUB 登录可以正常工作,但是当我尝试使用 facebook 登录时,我一到 url /login/check-facebook 就会收到错误“发生身份验证异常”。我使用本教程实现了 facebook 登录。我还从 Facebook 获得了 App ID 和 App Secret(不确定这两个是否应该设置为 aclient_idclient_secretin config.yml。也不确定我是否不需要在 facebook 中设置任何其他内容)

FOSUBUserProvider.php

<?php
namespace Atotrukis\MainBundle\Service;

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;

class FOSUBUserProvider extends BaseClass
{

/**
 * {@inheritDoc}
 */
public function connect(UserInterface $user, UserResponseInterface $response)
{
    $property = $this->getProperty($response);
    $username = $response->getUsername();

    //on connect - get the access token and the user ID
    $service = $response->getResourceOwner()->getName();

    $setter = 'set'.ucfirst($service);
    $setter_id = $setter.'Id';
    $setter_token = $setter.'AccessToken';

    //we "disconnect" previously connected users
    if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
        $previousUser->$setter_id(null);
        $previousUser->$setter_token(null);
        $this->userManager->updateUser($previousUser);
    }

    //we connect current user
    $user->$setter_id($username);
    $user->$setter_token($response->getAccessToken());

    $this->userManager->updateUser($user);
}

/**
 * {@inheritdoc}
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
    $username = $response->getUsername();
    $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
    //when the user is registrating
    if (null === $user) {
        $service = $response->getResourceOwner()->getName();
        $setter = 'set'.ucfirst($service);
        $setter_id = $setter.'Id';
        $setter_token = $setter.'AccessToken';
        // create new user here
        $user = $this->userManager->createUser();
        $user->$setter_id($username);
        $user->$setter_token($response->getAccessToken());
        //I have set all requested data with the user's username
        //modify here with relevant data
        $user->setUsername($username);
        $user->setEmail($username);
        $user->setPassword($username);
        $user->setEnabled(true);
        $this->userManager->updateUser($user);
        return $user;
    }

    //if user exists - go with the HWIOAuth way
    $user = parent::loadUserByOAuthUserResponse($response);

    $serviceName = $response->getResourceOwner()->getName();
    $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

        //update access token
        $user->$setter($response->getAccessToken());

        return $user;
    }

}

用户.php

<?php
namespace Atotrukis\MainBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
    protected $facebook_id;

    /** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
    protected $facebook_access_token;

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

    /**
     * @ORM\OneToMany(targetEntity="Event", mappedBy="createdBy")
     */
    protected $events;

    /**
     * @ORM\OneToMany(targetEntity="UserInterest", mappedBy="userId")
     */
    protected $interests;

    /**
     * @ORM\OneToMany(targetEntity="UserAttending", mappedBy="userId")
     */
    protected $attendingTo;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Get id
     *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Add events
 *
 * @param \Atotrukis\MainBundle\Entity\Event $events
 * @return User
 */
public function addEvent(\Atotrukis\MainBundle\Entity\Event $events)
{
    $this->events[] = $events;

    return $this;
}

/**
 * Remove events
 *
 * @param \Atotrukis\MainBundle\Entity\Event $events
 */
public function removeEvent(\Atotrukis\MainBundle\Entity\Event $events)
{
    $this->events->removeElement($events);
}

/**
 * Get events
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getEvents()
{
    return $this->events;
}

/**
 * Add interests
 *
 * @param \Atotrukis\MainBundle\Entity\UserInterests $interests
 * @return User
 */
public function addInterest(\Atotrukis\MainBundle\Entity\UserInterests $interests)
{
    $this->interests[] = $interests;

    return $this;
}

/**
 * Remove interests
 *
 * @param \Atotrukis\MainBundle\Entity\UserInterests $interests
 */
public function removeInterest(\Atotrukis\MainBundle\Entity\UserInterests $interests)
{
    $this->interests->removeElement($interests);
}

/**
 * Get interests
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getInterests()
{
    return $this->interests;
}

/**
 * Add attendingTo
 *
 * @param \Atotrukis\MainBundle\Entity\UserAttending $attendingTo
 * @return User
 */
public function addAttendingTo(\Atotrukis\MainBundle\Entity\UserAttending $attendingTo)
{
    $this->attendingTo[] = $attendingTo;

    return $this;
}

/**
 * Remove attendingTo
 *
 * @param \Atotrukis\MainBundle\Entity\UserAttending $attendingTo
 */
public function removeAttendingTo(\Atotrukis\MainBundle\Entity\UserAttending $attendingTo)
{
    $this->attendingTo->removeElement($attendingTo);
}

/**
 * Get attendingTo
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getAttendingTo()
{
    return $this->attendingTo;
}

public function setEmail($email)
{
    $email = is_null($email) ? '' : $email;
    parent::setEmail($email);
    $this->setUsername($email);
}

/**
 * Set name
 *
 * @param string $name
 * @return User
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set facebook_id
 *
 * @param string $facebookId
 * @return User
 */
public function setFacebookId($facebookId)
{
    $this->facebook_id = $facebookId;

    return $this;
}

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

/**
 * Set facebook_access_token
 *
 * @param string $facebookAccessToken
 * @return User
 */
public function setFacebookAccessToken($facebookAccessToken)
{
    $this->facebook_access_token = $facebookAccessToken;

    return $this;
}

/**
 * Get facebook_access_token
 *
     * @return string 
     */
    public function getFacebookAccessToken()
    {
        return $this->facebook_access_token;
    }
}

路由.yml:

atotrukis_main:
    resource: "@AtotrukisMainBundle/Resources/config/routing.yml"
    prefix:   /

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

#HWIOAuthBundle routes
hwi_oauth_security:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix: /login

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix: /login

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /login

facebook_login:
    pattern: /login/check-facebook

配置.yml

os_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Atotrukis\MainBundle\Entity\User
    registration:
      confirmation:
        from_email:
            address:        info@atotrukis.dev
            sender_name:    Sender name
        enabled: false
      form:
        type: atotrukis_user_registration

hwi_oauth:
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
    #and also, the connecting part (get the token and the user_id)
    connect:
        account_connector: my_user_provider
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main
    fosub:
        username_iterations: 30
        properties:
            # these properties will be used/redefined later in the custom FOSUBUserProvider service.
            facebook: facebook_id
    resource_owners:
        facebook:
            type:                facebook
            client_id:           "app_id"
            client_secret:       "app_secret"
            scope:               ""

security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_USER

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /login
                check_path: /login_check
            oauth:
                resource_owners:
                    facebook:           "/login/check-facebook"
                login_path:        /login
                failure_path:      /login

                oauth_user_provider:
                    #this is my custom user provider, created from FOSUBUserProvider - will manage the
                    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
                    service: my_user_provider
            logout:       true
            anonymous:    true

        login:
            pattern:  ^/login$
            security: false

            remember_me:
                key: "%secret%"
                lifetime: 31536000 # 365 days in seconds
                path: /
                domain: ~ # Defaults to the current domain from $_SERVER

    access_control:
        - { path: ^/mano-renginiai, role: ROLE_USER }
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

服务.yml

parameters:
    my_user_provider.class: Atotrukis\MainBundle\Service\FOSUBUserProvider

services:
    my_user_provider:
        class: "%my_user_provider.class%"
        #this is the place where the properties are passed to the UserProvider - see config.yml
        arguments: [@fos_user.user_manager,{facebook: facebook_id}]
4

1 回答 1

2

所以问题是我使用了错误的路径......我不得不使用 /login/facebook 而不是 /login/check-facebook

于 2014-11-06T19:00:43.837 回答