Chr*_*ris 2 security authentication login symfony
我找不到任何这个特别的东西;
当用户登录前端时,我需要进行简单的检查,如果此用户具有例如角色:ROLE_CUSTOMER
我需要在身份验证过程中完成此操作,因为如果用户没有此角色,则必须返回错误.
Security.yml
public:
pattern: ^/
context: site
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
check_path: site_login_check
login_path: site_login
success_handler: authentication_handler
failure_handler: authentication_handler
logout:
path: site_logout
target: site_login
anonymous: true
Run Code Online (Sandbox Code Playgroud)
我认为在这种情况下你必须编写自己的用户提供程序,因为在FOSuserbundle中假设默认ROLE是ROLE_USER,如果用户没有,它会自动将其与用户关联,并在用户启用后考虑FOS用户作为有效登录.或者您需要指定自己的login_check.
还有另一种方法,基本上你假设FOSuser进行登录和身份验证然后你可以创建一个服务,称之为successLoginHandler,用户"登录"后,你可以在那里做自己的东西并检查为了这个角色.如果不存在,则发回拒绝访问权限的异常.
public function __construct($securityContext)
{
$this->securityContext = $securityContext;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if(!$this->securityContext->isGranted('ROLE_CUSTOMER')){
throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
}
}
Run Code Online (Sandbox Code Playgroud)