Silex 웹 사이트에 보안 영역을 설정했습니다. 사용자가 연결될 때 헤더에 사용자 이름을 표시하거나 사용자가 연결되지 않은 경우 로그인 양식에 대한 링크를 표시해야합니다. 그러나 사용자가 보안되지 않은 페이지 (방화벽 외부)에있을 app.user
때는 정의되지 않습니다.
이 솔루션 을 시도했지만 작동하지 않습니다.
여기 내 보안 구성 :
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'account' => array(
'pattern' => '^/account',
'form' => array('login_path' => '/login', 'check_path' => '/account/login_check'),
'users' => $app->share(function () use ($app) {
return new UserProvider($app['db']);
}),
),
'unsecured' => array(
'anonymous' => true,
),
)
));
그리고 여기에 사용자 이름을 표시하는 헤더 :
{% if app.user %}
{{ app.user.username }}<br />
<a href="{{ path('account') }}">Mon compte</a>
{% else %}
<a href="{{ path('login') }}">se connecter</a><br />
<a href="{{ path('signup') }}">créer un compte</a>
{% endif %}
당신은 수정하여 모든 응용 프로그램에 방화벽을 확장 할 수 있습니다 pattern
에 ^/
익명 액세스를 허용 'anonymous' => true
. 보안되어야하는 경로는security.access_rules
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'account' => array(
'pattern' => '^/',
'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
'users' => $app->share(function () use ($app) {
return new UserProvider($app['db']);
}),
'anonymous' => true,
)
)
));
$app['security.access_rules'] = array(
array('^/account', 'ROLE_USER', null)
);
사용자 메서드 getRoles()
는 role ROLE_USER
을 반환해야 합니다. 이는 사용자가 security.access_rules
role 에서 모든 경로에 액세스 할 수 있음을 의미합니다 ROLE_USER
.
class User implements \Symfony\Component\Security\Core\User\AdvancedUserInterface
{
...
public function getRoles()
{
return array(new \Symfony\Component\Security\Core\Role\Role('ROLE_USER'));
}
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다