Silex:令牌存储不包含身份验证令牌

时间:2015-01-15 07:59:32

标签: silex

尝试检查用户是否在布局中进行了身份验证

{% if is_granted('IS_AUTHENTICATED_FULLY') %}
    <p>Username: {{ app.user.username }}</p>
{% endif %}

我收到错误

Twig_Error_Runtime in Template.php line 304:
An exception has been thrown during the rendering of a template ("The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.") in "layout.html" at line 39.

这是安全防火墙的配置。我只需要允许登录用户访问该网站。

$app->register(new SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'dev' => array(
            'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
            'security' => false
        ),
        'login' => array(
            'pattern' => '^/login$',
        ),
        'secured' => array(
            'pattern' => '^.*$',
            'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
            'logout' => array('logout_path' => '/logout'),
            'users' => $app->share(function() use ($app) {
                // Specific class App\User\UserProvider is described below
                return new App\User\UserProvider($app['db']);
            }),
        ),
        'unsecured' => array(
            'anonymous' => true,
        )
    ),
    'security.access_rules' => array(
        // You can rename ROLE_USER as you wish
        array('^/.+$', 'ROLE_USER'),
        array('^/login$', 'SS'), // This url is available as anonymous user
    )
));

任何解决此问题的想法都是受欢迎的。

谢谢

1 个答案:

答案 0 :(得分:10)

由于错误消息表明错误发生在layout.html中,我猜它会在每个页面上使用,即使像/ login那样不在防火墙之后。错误是由于在不在防火墙后面调用is_granted引起的。

所以有几个选择:

  1. 为不调用is_granted
  2. 的登录页面使用单独的布局
  3. 在致电is_granted
  4. 之前,请检查是否有现有的安全令牌

    选项1应该是显而易见的,所以不要详细说明。

    使用选项2,您可以执行以下操作来检查现有的安全令牌:

    {% if app.security.token is not null and is_granted('IS_AUTHENTICATED_FULLY') %}