因此,经过大量研究,我找到了正确的方法。您需要使用入口点服务,并在防火墙配置中对其进行定义。
此方法 不会
与您在防火墙配置中指定的用于登录的默认页面设置混淆。
编码
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">×</button> {{ flashMessage }} </div> {% endfor %}{% endif %}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)