在Symfony中实现单点登录(SSO)并处理多个用户类型,通常涉及使用FOSUserBundle进行用户管理,并结合OAuth2或OpenID Connect等协议来实现SSO。以下是一个基本的步骤指南,帮助你设置和处理多个用户类型。
首先,安装FOSUserBundle:
composer require friendsofsymfony/user-bundle
然后在app/AppKernel.php
中注册Bundle:
public function registerBundles()
{
$bundles = [
// ...
new FOS\UserBundle\FOSUserBundle(),
];
// ...
}
配置FOSUserBundle,在app/config/config.yml
中添加:
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
创建一个基本的用户实体,例如AppBundle\Entity\User
:
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// 其他字段和方法...
}
假设你有两个用户类型:Admin
和Customer
。你可以创建两个不同的实体继承自User
实体,并添加特定字段。
namespace AppBundle\Entity;
class Admin extends User
{
// 特定于Admin的字段
protected $adminField;
}
namespace AppBundle\Entity;
class Customer extends User
{
// 特定于Customer的字段
protected $customerField;
}
在app/config/security.yml
中配置不同的用户类型:
security:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
admin_provider:
entity:
class: AppBundle\Entity\Admin
property: username
customer_provider:
entity:
class: AppBundle\Entity\Customer
property: username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
为每个用户类型创建表单类型:
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Admin;
class AdminType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('adminField');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_admin_registration';
}
}
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Customer;
class CustomerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('customerField');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_customer_registration';
}
}
配置相应的路由和控制器来处理用户注册和登录:
# app/config/routing.yml
app_admin_registration:
path: /admin/register
defaults: { _controller: AppBundle:Admin:register }
app_customer_registration:
path: /customer/register
defaults: { _controller: AppBundle:Customer:register }
// src/AppBundle/Controller/AdminController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Admin;
use AppBundle\Form\Type\AdminType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class AdminController extends Controller
{
/**
* @Route("/admin/register", name="admin_register")
*/
public function registerAction(Request $request)
{
$admin = new Admin();
$form = $this->createForm(AdminType::class, $admin);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($admin);
$em->flush();
return $this->redirectToRoute('admin_login');
}
return $this->render('AppBundle:Admin:register.html.twig', [
'form' => $form->createView(),
]);
}
}
如果你需要实现单点登录,可以考虑使用OAuth2或OpenID Connect。Symfony提供了相应的Bundle来简化这一过程,例如 HWIOAuthBundle
。
安装HWIOAuthBundle:
composer require hwi/oauth-bundle
配置HWIOAuthBundle:
# app/config/config.yml
hwi_oauth:
firewall_name: main
resource_owners:
google:
type: google
client_id: '%google_client_id%'
client_secret: '%google_client_secret%'
scope: "email profile"
配置路由:
# app/config/routing.yml
hwi_oauth_security:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login
hwi_oauth_connect:
resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
prefix: /connect
领取专属 10元无门槛券
手把手带您无忧上云