栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在access_control规则重定向后通知用户的最佳方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在access_control规则重定向后通知用户的最佳方法是什么?

因此,经过大量研究,我找到了正确的方法。您需要使用入口点服务,并在防火墙配置中对其进行定义。

此方法 不会
与您在防火墙配置中指定的用于登录的默认页面设置混淆。


编码

security.yml:

firewalls:    main:        entry_point: entry_point.user_login #or whatever you name your service        pattern: ^/        form_login:        # ...

src / Acme / UserBundle / config / services.yml

services:    entry_point.user_login:        class: AcmeUserBundleServiceLoginEntryPoint        arguments: [ @router ] #I am going to use this for URL generation since I will be redirecting in my service

src / Acme / UserBundle / Service / LoginEntryPoint.php:

namespace AcmeUserBundleService;use SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface,    SymfonyComponentSecurityCoreExceptionAuthenticationException,    SymfonyComponentHttpFoundationRequest,    SymfonyComponentHttpFoundationRedirectResponse;class LoginEntryPoint implements AuthenticationEntryPointInterface{    protected $router;    public function __construct($router)    {        $this->router = $router;    }        public function start(Request $request, AuthenticationException $authException = null)    {        $session = $request->getSession();        // I am choosing to set a FlashBag message with my own custom message.        // Alternatively, you could use AuthenticationException's generic message         // by calling $authException->getMessage()        $session->getFlashBag()->add('warning', 'You must be logged in to access that page');        return new RedirectResponse($this->router->generate('login'));    }}

login.html.twig:

{# bootstrap ready for your convenience ;] #}{% if app.session.flashbag.has('warning') %}    {% for flashMessage in app.session.flashbag.get('warning') %}        <div > <button type="button"  data-dismiss="alert">&times;</button> {{ flashMessage }}        </div>    {% endfor %}{% endif %}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号