简体   繁体   中英

Symfony2 ROLE_ADMIN for requests from certain IP

I am using the security component in a symfony2 application.

I have the case that I need to assign the ROLE_ADMIN for traffic coming from a certain IP. I was thinking of using anonymous authentication and assign the anonymous user the 'ROLE_ADMIN' role somewhere later via a service. 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. 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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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