symfony2 を Facebook SDK と統合しようとしています。そのための優れたバンドルが既にあることは知っていますが、独自のメカニズムを開発したいと考えています。
これが私がこの問題に取り組んだ方法です。Facebook でログインしてアプリを承認すると、Facebook は事前に指定した URL にリダイレクトされます。その URL は、このコントローラーのメソッドにマップされます。
public function facebookLoginAction(Request $request) {
$facebook = $this->get("facebook");
$data = $facebook->api('/me', 'GET');
if ($data) {
$user = new AuthenticatedUser($data["name"], md5($data["id"]), "");
$token = new UsernamePasswordToken($user, $data["id"], 'secured_area', $user->getRoles());
$authToken = $this->get('security.authentication.manager')->authenticate($token);
$this->get('security.context')->setToken($authToken);
}
return new Response("<pre>" . print_r($token, true));
}
これを行うことで、ユーザーのログインを強制して、Facebook から戻ったときにすべてが既にソートされているようにしようとしています。
完全性のために、AuthenticatedUser
のコードは次のとおりです。
namespace Nourdine\BasicBundle\Security\User;
use Symfony\Component\Security\Core\User\UserInterface;
class AuthenticatedUser implements UserInterface {
private $username;
private $password;
private $salt;
/**
* @param string $username
* @param string $password This md5-ied!
* @param string $salt The salt is empty at the mo!
*/
public function __construct($username, $password, $salt) {
$this->username = $username;
$this->password = $password;
$this->salt = $salt;
}
public function getRoles() {
return array("ROLE_USER");
}
public function getPassword() {
return $this->password;
}
/**
* @link http://symfony.com/doc/current/cookbook/security/custom_provider.html
* If getSalt() returns nothing, then the submitted password is simply encoded using the algorithm you specify in security.yml
*/
public function getSalt() {
return $this->salt; // this is empty indeed
}
public function getUsername() {
return $this->username;
}
public function eraseCredentials() {
}
public function equals(UserInterface $user) {
if (!$user instanceof WebserviceUser) {
return false;
}
if ($this->password !== $user->getPassword()) {
return false;
}
if ($this->getSalt() !== $user->getSalt()) {
return false;
}
if ($this->username !== $user->getUsername()) {
return false;
}
return true;
}
}
secured_area
私はそれが重要であることを知っているので、それが私のファイアウォールの名前であることを指摘したいと思います! ここ:
<firewall name="secured_area" pattern="^/">
<anonymous />
<form-login login_path="/login" check_path="/login_check" />
<logout path="/logout" target="/" />
</firewall>
また、コントローラーで使用される「facebook」というサービスはFacebook
、officla sdk のクラスのインスタンスにすぎません。これがservices.xmlでの定義です
<service id="facebook" class="Facebook">
<argument>%facebook.params%</argument>
</service>
何か案が?私は何を間違っていますか?ユーザープロバイダーの概念は、これらすべてに何らかの形で関与していますか? ログインが実際に強制されていても?
乾杯