简体   繁体   English

Symfony2 ROLE_ADMIN用于某些IP的请求

[英]Symfony2 ROLE_ADMIN for requests from certain IP

I am using the security component in a symfony2 application. 我在symfony2应用程序中使用了安全组件。

I have the case that I need to assign the ROLE_ADMIN for traffic coming from a certain IP. 我遇到的情况是,我需要为来自某个IP的流量分配ROLE_ADMIN。 I was thinking of using anonymous authentication and assign the anonymous user the 'ROLE_ADMIN' role somewhere later via a service. 我当时正在考虑使用匿名身份验证,并在稍后通过服务将匿名用户分配给“ ROLE_ADMIN”角色。 I have the feeling this is not going to work since from the security context I can get a user but this is just the string "anon" and not a full user entity object. 我有种感觉,这是行不通的,因为我可以从安全上下文中获取用户,但这只是字符串“ anon”,而不是完整的用户实体对象。 There is no way to assign the role. 无法分配角色。

Maybe somebody can point me in he right direction? 也许有人可以指出我正确的方向?

I have a solution, initially I wanted to assign the role ROLE_ADMIN to the anonymous user which is not possible. 我有一个解决方案,最初我想将角色ROLE_ADMIN分配给匿名用户,这是不可能的。 I created a custom security listener + factory which works but you can just simply assign a new token to the security context, from a service or controller. 我创建了一个自定义的安全侦听器+工厂,该工厂可以工作,但是您只需从服务或控制器向安全上下文分配一个新令牌即可。

    $token = new \Symfony\Component\Security\Core\Authentication\Token\AnonymousToken('admin', 'admin', array('ROLE_ADMIN'));
    $this->get('security.context')->setToken($token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM