Symfony2 - 检测用户是否在安全防火墙内 [英] Symfony2 - detect if user is inside a secure firewall

查看:24
本文介绍了Symfony2 - 检测用户是否在安全防火墙内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定用户是否在安全防火墙内.

I'm trying to find out if a user is inside a secure firewall.

security.yml:

security.yml:

firewalls:
    non_secure_area:
        pattern: ^/
        anonymous: true
    secure_area:
        pattern: ^/admin
        form_login:
            #etc.
        logout:
            #etc.

所以我需要知道用户是否在网站的secure_area"安全部分内.

So I need to know if the user is inside the 'secure_area' secure part of the site.

我已经使用过这个,但当然它只会告诉我是否有人登录"并且在 HTTPS 页面上.一定有更好的方法:

I have used this, but of course it only tells me if somebody is 'logged in' AND on a HTTPS page. There must be a better way:

if( $request->isSecure() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ) {

} 

推荐答案

您可以获得安全令牌和访问提供程序密钥.

You can get security token and access provider key on it.

$token = $securityContext->getToken();
$providerKey = $token->getProviderKey(); // secured_area

不要忘记检查令牌是否存在并且它不是AnonymousToken

Dont forget to check that token exist and its not an instance of AnonymousToken

这篇关于Symfony2 - 检测用户是否在安全防火墙内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆