ladamalina / remote-user-bundle
Symfony RemoteUserBundle
0.1
2016-08-06 15:44 UTC
Requires
- php: >=5.6
- symfony/symfony: ~3.0
This package is auto-updated.
Last update: 2024-09-14 17:21:32 UTC
README
安装
下载此包的最新稳定版本
$ composer require ladamalina/remote-user-bundle
启用该包
<?php // app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Ladamalina\RemoteUserBundle\RemoteUserBundle(), ); // ... } // ... }
无论您如何进行身份验证,您都需要创建一个实现 UserInterface
的 User 类
<?php // src/AppBundle/Entity/User.php namespace AppBundle\Entity; use Symfony\Component\Security\Core\User\UserInterface; class User implements UserInterface { private $id; private $username; private $name; public function getUsername() { return $this->username; } public function getRoles() { return ['ROLE_USER']; } public function getPassword() {} public function getSalt() {} public function eraseCredentials() {} // more getters/setters }
创建一个用户提供者。在这里,您必须实现用户凭证检查
<?php // src/AppBundle/Security/UserProvider.php // ... class UserProvider extends AbstractRemoteUserProvider { /** * @var string */ protected $userClassName; public function __construct($userClassName) { if (!class_exists($userClassName)) { throw new \InvalidArgumentException("Class `$userClassName` does not exists. Invalid service configuration: services.remote_user_provider"); } $this->userClassName = $userClassName; } public function loadUserByUsernameAndPassword($username, $password) { try { // Remote API call checking $username and $password here // Populate new User instance with response data return $user; } catch (\Exception $e) { throw new UsernameNotFoundException(); } } }
配置认证器和用户提供者服务 app/config/services.yml
services: remote_user_provider: class: AppBundle\Security\UserProvider arguments: ["AppBundle\\Entity\\User"] remote_user_authenticator: class: RemoteUserBundle\Security\Guard\Authenticator
配置安全用户提供者 app/config/security.yml
security: providers: remote: id: remote_user_provider
配置防火墙守卫 app/config/security.yml
security: firewalls: main: anonymous: ~ # activate different ways to authenticate guard: authenticators: - remote_user_authenticator
用法
带有 rua_username
和 rua_password
字段的 POST 请求将启动远程授权调用。
curl --request POST \
--url http://site.com/ \
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
--form rua_username=username \
--form rua_password=password
如果凭证无效或远程服务不可用,您将收到 HTTP 状态码 403 禁止,否则为 200 正常。