Symfony 注销处理程序 [英] Symfony logout handler

查看:16
本文介绍了Symfony 注销处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个小时来弄清楚如何在注销操作后使 Flash 消息正常工作.

security.yml

 登录:模式:^/登录$安全:假安全区域:模式:^/表单登录:检查路径:/检查登录路径:/登录failure_handler: authentication_handler登出:路径:/退出成功处理程序:身份验证处理程序

config.yml

服务:身份验证处理程序:类:Project\LoginBundle\Handler\AuthenticationHandler

AuthenticationHandler.php

class AuthenticationHandler 实现了AuthenticationFailureHandlerInterface、LogoutSuccessHandlerInterface{公共函数 onAuthenticationFailure(Request $request, AuthenticationException $exception){$referer = $request->headers->get('referer');$request->getSession()->setFlash('error', $exception->getMessage());返回新的重定向响应($referer);}公共函数 onLogoutSuccess(Request $request){$referer = $request->headers->get('referer');$request->getSession()->setFlash('success', 'Wylogowano');返回新的重定向响应($referer);}}

登录后查看hello

{% extends "ProjectCMSBundle:Secured:layout.html.twig" %}{% 块标题你好"~名字 %}{% 块内容 %}<h1>你好{{姓名}}!</h1><a href="{{ path('_project_secured_hello_admin', { 'name': name }) }}">为<strong>admin</strong>保护的Hello资源仅.{% 结束块 %}{% 设置代码 = 代码(_self)%}

查看登录表单

{% extends 'ProjectCMSBundle::layout.html.twig' %}{% 块标题 %}标题{% 结束块 %}{% 块内容 %}<form action="{{ path("_project_security_check") }}" method="post" id="login"><div class="数据"><div class="用户名"><label for="username">&nbsp;</label><input type="text" id="username" name="_username" value="{{ last_username }}"/>

<div class="密码"><label for="password">&nbsp;</label><input type="password" id="password" name="_password"/>

{% 如果错误 %}<div class="error">{{ error.message|trans({},'messages') }}</div>{% 万一 %}<input type="submit" class="submit"/></表单>{% 结束块 %}{% 设置代码 = 代码(_self)%}

布局主模板

<头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><link rel="stylesheet" href="{{ asset('bundles/project/css/demo.css') }}" type="text/css" media="all"/>{% 块标题 %}{% endblock %}<link rel="shortcut icon" href="{{ asset('favicon.ico') }}"/><身体><div id="symfony-wrapper">{% if app.session.flash('error') %}<div class="flash-message">{{ app.session.flash('error')|trans }}

{% 万一 %}{% if app.session.flash('success') %}<div class="flash-message">{{ app.session.flash('success')}}

{% 万一 %}{% if app.user %}{% 块 content_header %}<ul id="菜单">{% 块 content_header_more %}{% 结束块 %}<div style="clear: both"></div>{% 结束块 %}{% 万一 %}<div class="symfony-content">{% 块内容 %}{% 结束块 %}

{#{% 如果定义了代码 %}<h2>此页面后面的代码</h2><div class="symfony-content">{{ code|raw }}</div>{% 万一 %}#}

问题是,在重定向过程中,Flash 消息似乎被丢弃了.有没有办法做到这一点?

感谢您的回答.

解决方案

注销时会话被破坏.但是您可以通过在 security.yml 文件中添加 invalidate_session: false 来更改此行为.

注销:路径:/退出成功处理程序:身份验证处理程序invalidate_session: 假

查看参考文档以获取更多信息.

如果您仍然想使会话无效,您可以直接在您的请求中设置一个标志并将其与内核侦听器一起使用.

I am just trying already couple of hours to figure out how to get flash message working after logout action.

security.yml

    login:
        pattern:  ^/login$
        security: false

    secured_area:
        pattern:    ^/
        form_login:
            check_path: /check
            login_path: /login
            failure_handler: authentication_handler
        logout:
            path:   /logout
            success_handler: authentication_handler

config.yml

services:
    authentication_handler:
        class: Project\LoginBundle\Handler\AuthenticationHandler

AuthenticationHandler.php

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface
{
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {       
        $referer = $request->headers->get('referer');       
        $request->getSession()->setFlash('error', $exception->getMessage());

        return new RedirectResponse($referer);
    }

    public function onLogoutSuccess(Request $request) 
    {
        $referer = $request->headers->get('referer');
        $request->getSession()->setFlash('success', 'Wylogowano');


        return new RedirectResponse($referer);
    }
}

view hello after login

{% extends "ProjectCMSBundle:Secured:layout.html.twig" %}

{% block title "Hello " ~ name %}

{% block content %}
    <h1>Hello {{ name }}!</h1>

    <a href="{{ path('_project_secured_hello_admin', { 'name': name }) }}">Hello resource secured for <strong>admin</strong> only.</a>
{% endblock %}

{% set code = code(_self) %}

view login form

{% extends 'ProjectCMSBundle::layout.html.twig' %}

{% block title %}
    Title
{% endblock %}

{% block content %}


    <form action="{{ path("_project_security_check") }}" method="post" id="login">
        <div class="data">
          <div class="username">
              <label for="username">&nbsp;</label>
              <input type="text" id="username" name="_username" value="{{ last_username }}" />
          </div>

          <div class="password">
              <label for="password">&nbsp;</label>
              <input type="password" id="password" name="_password" />
          </div>
        </div>
    {% if error %}
        <div class="error">{{ error.message|trans({},'messages') }}</div>
    {% endif %}
    <input type="submit" class="submit" />
</form>
{% endblock %}

{% set code = code(_self) %}

layout main template

<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="{{ asset('bundles/project/css/demo.css') }}" type="text/css" media="all" />
    <title>{% block title %}{% endblock %}</title>
    <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
    <div id="symfony-wrapper">

        {% if app.session.flash('error') %}
            <div class="flash-message">
                {{ app.session.flash('error')|trans }}
            </div>
        {% endif %}

        {% if app.session.flash('success') %}
            <div class="flash-message">
                {{ app.session.flash('success')}}
            </div>
        {% endif %}

        {% if app.user %}
          {% block content_header %}
            <ul id="menu">
                {% block content_header_more %}
                {% endblock %}
            </ul>

            <div style="clear: both"></div>
          {% endblock %}
        {% endif %}

        <div class="symfony-content">
            {% block content %}
            {% endblock %}
        </div>

        {#{% if code is defined %}
            <h2>Code behind this page</h2>
            <div class="symfony-content">{{ code|raw }}</div>
        {% endif %}#}
    </div>
</body>

The problem is that it seems that flash message is getting dropped during redirect. Is there any way to accomplish this?

Thanks for the answers.

解决方案

Session is destroyed on logout. But you can change this behavior by adding invalidate_session: false in your security.yml file.

logout:
    path:   /logout
    success_handler: authentication_handler
    invalidate_session: false

Check the reference documentation to have more information.

If you still want to invalidate session, you can set a flag in your Request directly and use it with a Kernel listener.

这篇关于Symfony 注销处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆