Silex:安全认证系统尝试运行'login_check'路由
Silex: Security authentication system attempts to run 'login_check' route
我正在尝试在 Silex 中配置身份验证系统。但是,弹出以下错误:
当然,我一直在网上寻找解决方案。但是每个主题都说 'login_check' 应该在防火墙后面。所以放在/Users/区。
我试过进入保护区。但是,防火墙有效,因为我被重定向回登录页面。我正在使用以下代码行来获得我想要的。
注册部分:
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'login_path' => array(
'pattern' => '^/Security/login$',
'anonymous' => true
),
'default' => array(
'pattern' => '^/Security/User/.*$',
'anonymous' => true,
'form' => array(
'login_path' => '/Security/login',
'check_path' => '/Security/User/login_check',
),
'logout' => array(
'logout_path' => '/Security/User/logout',
'invalidate_session' => false
),
'users' => function($app) {
return new User\UserProvider($app['db']);
},
)
),
'security.access_rules' => array(
array('^/Security/login$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/Security/User/.+$', 'ROLE_USER')
)
));
登录路径:
$routes->match('/login', function(Request $request) use ($app) {
return $app['twig']->render('form.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
})->bind('login');
$routes->match('/User/page', function () use ($app) {
return $app['twig']->render('index.html.twig', array());
})->bind('user.page');
Form.html.twig:
{% extends "layout.html.twig" %}
{% block content %}
{% if is_granted('ROLE_USER') %}
<p>Welcome {{ app.security.token.user.username }}!</p>
<p><a href="{{ path('/User/logout') }}">Log out</a></p>
{% else %}
<form action="{{ path('/User/login_check') }}" method="post">
<p><label for="username">Username: </label><input id="username" type="text" name="_username" value="{{ last_username }}"></p>
<p><label for="password">Password: </label><input id="password" type="password" name="_password"></p>
<p><input type="submit" value="Log in"></p>
{% if error %}
<div class="error">{{ error }}</div>
{% endif %}
</form>
{% endif %}
{% endblock %}
谁能告诉我为什么系统仍然尝试执行login_check路由?它应该由 Silex 处理。而且它位于保护区内。
函数 path
接受路由名称作为参数,而不是 url。安全提供商为路径 check_path
和 logout_path
制作假路由。它们的名称取决于路径,路由名称是所有出现的 /
都替换为 _
的路径。
所以对于你来说 check_path
路由名称是 Security_User_login_check
,logout_path
路由名称是 Security_User_logout
。
修复 path
对 {{ path('Security_User_login_check') }}
和 {{ path('Security_User_logout') }}
的调用
我正在尝试在 Silex 中配置身份验证系统。但是,弹出以下错误:
当然,我一直在网上寻找解决方案。但是每个主题都说 'login_check' 应该在防火墙后面。所以放在/Users/区。
我试过进入保护区。但是,防火墙有效,因为我被重定向回登录页面。我正在使用以下代码行来获得我想要的。
注册部分:
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'login_path' => array(
'pattern' => '^/Security/login$',
'anonymous' => true
),
'default' => array(
'pattern' => '^/Security/User/.*$',
'anonymous' => true,
'form' => array(
'login_path' => '/Security/login',
'check_path' => '/Security/User/login_check',
),
'logout' => array(
'logout_path' => '/Security/User/logout',
'invalidate_session' => false
),
'users' => function($app) {
return new User\UserProvider($app['db']);
},
)
),
'security.access_rules' => array(
array('^/Security/login$', 'IS_AUTHENTICATED_ANONYMOUSLY'),
array('^/Security/User/.+$', 'ROLE_USER')
)
));
登录路径:
$routes->match('/login', function(Request $request) use ($app) {
return $app['twig']->render('form.html.twig', array(
'error' => $app['security.last_error']($request),
'last_username' => $app['session']->get('_security.last_username'),
));
})->bind('login');
$routes->match('/User/page', function () use ($app) {
return $app['twig']->render('index.html.twig', array());
})->bind('user.page');
Form.html.twig:
{% extends "layout.html.twig" %}
{% block content %}
{% if is_granted('ROLE_USER') %}
<p>Welcome {{ app.security.token.user.username }}!</p>
<p><a href="{{ path('/User/logout') }}">Log out</a></p>
{% else %}
<form action="{{ path('/User/login_check') }}" method="post">
<p><label for="username">Username: </label><input id="username" type="text" name="_username" value="{{ last_username }}"></p>
<p><label for="password">Password: </label><input id="password" type="password" name="_password"></p>
<p><input type="submit" value="Log in"></p>
{% if error %}
<div class="error">{{ error }}</div>
{% endif %}
</form>
{% endif %}
{% endblock %}
谁能告诉我为什么系统仍然尝试执行login_check路由?它应该由 Silex 处理。而且它位于保护区内。
函数 path
接受路由名称作为参数,而不是 url。安全提供商为路径 check_path
和 logout_path
制作假路由。它们的名称取决于路径,路由名称是所有出现的 /
都替换为 _
的路径。
所以对于你来说 check_path
路由名称是 Security_User_login_check
,logout_path
路由名称是 Security_User_logout
。
修复 path
对 {{ path('Security_User_login_check') }}
和 {{ path('Security_User_logout') }}