首页一个> 欧宝体育电话 安全一个> 如何模拟用户一个> 如何模拟用户 编辑本页一个> 警告:您正在浏览的文档欧宝体育电话<一个href="//www.oldmanjams.com/releases/2.7">欧宝娱乐app下载地址Symfony 2.7一个>,现已不再维护。 读<一个href="//www.oldmanjams.com/doc/current/security/impersonating_user.html">本页的更新版本一个>用于Sy欧宝娱乐app下载地址mfony 6.2(当前稳定版本)。 谨慎 与用户模拟不兼容<一个href="//www.oldmanjams.com/doc/2.7/security/pre_authenticated.html" class="reference internal">预认证防火墙一个>.原因是,模拟需要在服务器端维护身份验证状态,但是预身份验证信息(SSL_CLIENT_S_DN_Email,REMOTE_USER或其他)在每个请求中发送。 方法可以轻松地模拟用户switch_user防火墙侦听器: YAML XML PHP 1 2 3 4 5 6 7 8 # app / config / security.yml安全:#……防火墙:主要:#……switch_user:真正的 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!--一个pp/config/security.xml --><?xml version="1.0" encoding="UTF-8"?><srv:容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/security”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xmlns:深水救生艇=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><配置><!--...--><防火墙的名字=“主要”><!--...--><切换用户/>防火墙>配置>srv:容器> 1 2 3 4 5 6 7 8 9 10 11 / / app / config / security.php$容器->loadFromExtension (“安全”,数组(/ /……“防火墙”= >数组(“主要”= >数组(/ /……“switch_user”= >真正的,),),)); 要切换到另一个用户,只需使用_switch_user参数和用户名作为当前URL的值: 1 http://example.com/somewhere?_switch_user=thomas 若要切换回原始用户,请使用特殊_exit用户名: 1 http://example.com/somewhere?_switch_user=_exit 在模拟过程中,为用户提供了一个名为ROLE_PREVIOUS_ADMIN.例如,在模板中,此角色可用于显示退出模拟的链接: 嫩枝 PHP 1 2 3 {%如果is_granted (ROLE_PREVIOUS_ADMIN) %}<一个href="{{path('主页',{'_switch_user': '_exit'})}}">退出模拟一个>{%endif%} 1 2 3 4 5 6 7 8 9 <?php如果($视图[“安全”]->isGranted (“ROLE_PREVIOUS_ADMIN”)):? ><一个href=“< ?php echo $视图(“路由器”)- >生成(“主页”,数组(“_switch_user”= >“_exit”))? > ">退出模拟一个><?phpendif? > 在某些情况下,您可能需要获取表示模拟用户而不是模拟用户的对象。使用下面的代码片段遍历用户的角色,直到找到一个SwitchUserRole对象: 12 3 4 5 6 7 8 9 10 11 12 13 使用欧宝娱乐app下载地址\组件\安全\核心\角色\SwitchUserRole;$authorizationChecker=$这->get (“security.authorization_checker”);$tokenStorage=$这->get (“security.token_storage”);如果($authorizationChecker->isGranted (“ROLE_PREVIOUS_ADMIN”)) {foreach($tokenStorage->getToken ()->将getRoles ()作为$角色){如果($角色运算符SwitchUserRole) {$impersonatorUser=$角色->getSource ()->getUser ();打破;}}} 当然,这个特性需要提供给一小部分用户。默认情况下,访问限制为具有ROLE_ALLOWED_TO_SWITCH的角色。方法修改此角色的名称角色设置。方法更改查询参数名称,以提高安全性参数设置: YAML XML PHP 1 2 3 4 5 6 7 8 # app / config / security.yml安全:#……防火墙:主要:#……switch_user:{角色:ROLE_ADMIN,参数:_want_to_be_this_user} 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!--一个pp/config/security.xml --><?xml version="1.0" encoding="UTF-8"?><srv:容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/security”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xmlns:深水救生艇=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><配置><!--...--><防火墙的名字=“主要”><!--...--><切换用户角色=“ROLE_ADMIN”参数=“_want_to_be_this_user”/>防火墙>配置>srv:容器> 12 3 4 5 6 7 8 9 10 11 12 13 14 / / app / config / security.php$容器->loadFromExtension (“安全”,数组(/ /……“防火墙”= >数组(“主要”= >数组(/ /……“switch_user”= >数组(“角色”= >“ROLE_ADMIN”,“参数”= >“_want_to_be_this_user”,),),),)); security.switch_user事件。的<一个href="https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php" class="reference external" title="SwitchUserEvent"rel="external noopener noreferrer" target="_blank">SwitchUserEvent一个>传递给侦听器,您可以使用它来获取您现在正在模拟的用户。 的<一个href="//www.oldmanjams.com/doc/2.7/session/locale_sticky_session.html" class="reference internal">在用户会话期间使区域设置“粘滞”一个>当您模拟用户时,Article不会更新区域设置。下面的代码示例将展示如何更改粘性区域设置: YAML XML PHP 1 2 3 4 5 6 # app / config / services.yml服务:app.switch_user_listener:类:AppBundle \ EventListener \ SwitchUserListener标签:-{名称:kernel.event_listener,事件:security.switch_user,方法:onSwitchUser} 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!--一个pp/config/services.xml --><??> . xml version="1.0" encoding="UTF-8"<容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><服务><服务id=“app.switch_user_listener”类=“AppBundle \ EventListener \ SwitchUserListener”><标签的名字=“kernel.event_listener”事件=“security.switch_user”方法=“onSwitchUser”/>服务>服务>容器> 1 2 3 4 5 6 7 8 9 10 / / app / config / services.php使用AppBundle\EventListener\SwitchUserListener;$容器->注册(“app.switch_user_listener”, SwitchUserListener::类)->addTag (“kernel.event_listener”,数组(“事件”= >“security.switch_user”,“方法”= >“onSwitchUser”,)); 谨慎 侦听器实现假设您的用户实体有一个getLocale ()方法: 12 3 4 5 6 7 8 9 10 11 12 13 14 15 / / src / AppBundle / EventListener / SwitchUserListener.php名称空间AppBundle\EventListener;使用欧宝娱乐app下载地址\组件\安全\Http\事件\SwitchUserEvent;类SwitchUserListener{公共函数onSwitchUser(SwitchUserEvent$事件){$事件->getRequest ()->getSession ()->集(“_locale”,$事件->getTargetUser ()->getLocale ());}} 此工作,包括代码示例,是根据<一个rel="license" href="https://creativecommons.org/licenses/by-sa/3.0/">创作共用BY-SA 3.0一个>许可证。 今天就从舒适的控制台进行部署一个> 代码消耗服务器资源。黑火告诉你怎么做一个> 接受SensioLabs专家的培训(2 - 6天的课程-法语或英语)。一个>
如何模拟用户 编辑本页一个> 警告:您正在浏览的文档欧宝体育电话<一个href="//www.oldmanjams.com/releases/2.7">欧宝娱乐app下载地址Symfony 2.7一个>,现已不再维护。 读<一个href="//www.oldmanjams.com/doc/current/security/impersonating_user.html">本页的更新版本一个>用于Sy欧宝娱乐app下载地址mfony 6.2(当前稳定版本)。 谨慎 与用户模拟不兼容<一个href="//www.oldmanjams.com/doc/2.7/security/pre_authenticated.html" class="reference internal">预认证防火墙一个>.原因是,模拟需要在服务器端维护身份验证状态,但是预身份验证信息(SSL_CLIENT_S_DN_Email,REMOTE_USER或其他)在每个请求中发送。 方法可以轻松地模拟用户switch_user防火墙侦听器: YAML XML PHP 1 2 3 4 5 6 7 8 # app / config / security.yml安全:#……防火墙:主要:#……switch_user:真正的 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!--一个pp/config/security.xml --><?xml version="1.0" encoding="UTF-8"?><srv:容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/security”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xmlns:深水救生艇=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><配置><!--...--><防火墙的名字=“主要”><!--...--><切换用户/>防火墙>配置>srv:容器> 1 2 3 4 5 6 7 8 9 10 11 / / app / config / security.php$容器->loadFromExtension (“安全”,数组(/ /……“防火墙”= >数组(“主要”= >数组(/ /……“switch_user”= >真正的,),),)); 要切换到另一个用户,只需使用_switch_user参数和用户名作为当前URL的值: 1 http://example.com/somewhere?_switch_user=thomas 若要切换回原始用户,请使用特殊_exit用户名: 1 http://example.com/somewhere?_switch_user=_exit 在模拟过程中,为用户提供了一个名为ROLE_PREVIOUS_ADMIN.例如,在模板中,此角色可用于显示退出模拟的链接: 嫩枝 PHP 1 2 3 {%如果is_granted (ROLE_PREVIOUS_ADMIN) %}<一个href="{{path('主页',{'_switch_user': '_exit'})}}">退出模拟一个>{%endif%} 1 2 3 4 5 6 7 8 9 <?php如果($视图[“安全”]->isGranted (“ROLE_PREVIOUS_ADMIN”)):? ><一个href=“< ?php echo $视图(“路由器”)- >生成(“主页”,数组(“_switch_user”= >“_exit”))? > ">退出模拟一个><?phpendif? > 在某些情况下,您可能需要获取表示模拟用户而不是模拟用户的对象。使用下面的代码片段遍历用户的角色,直到找到一个SwitchUserRole对象: 12 3 4 5 6 7 8 9 10 11 12 13 使用欧宝娱乐app下载地址\组件\安全\核心\角色\SwitchUserRole;$authorizationChecker=$这->get (“security.authorization_checker”);$tokenStorage=$这->get (“security.token_storage”);如果($authorizationChecker->isGranted (“ROLE_PREVIOUS_ADMIN”)) {foreach($tokenStorage->getToken ()->将getRoles ()作为$角色){如果($角色运算符SwitchUserRole) {$impersonatorUser=$角色->getSource ()->getUser ();打破;}}} 当然,这个特性需要提供给一小部分用户。默认情况下,访问限制为具有ROLE_ALLOWED_TO_SWITCH的角色。方法修改此角色的名称角色设置。方法更改查询参数名称,以提高安全性参数设置: YAML XML PHP 1 2 3 4 5 6 7 8 # app / config / security.yml安全:#……防火墙:主要:#……switch_user:{角色:ROLE_ADMIN,参数:_want_to_be_this_user} 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!--一个pp/config/security.xml --><?xml version="1.0" encoding="UTF-8"?><srv:容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/security”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xmlns:深水救生艇=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><配置><!--...--><防火墙的名字=“主要”><!--...--><切换用户角色=“ROLE_ADMIN”参数=“_want_to_be_this_user”/>防火墙>配置>srv:容器> 12 3 4 5 6 7 8 9 10 11 12 13 14 / / app / config / security.php$容器->loadFromExtension (“安全”,数组(/ /……“防火墙”= >数组(“主要”= >数组(/ /……“switch_user”= >数组(“角色”= >“ROLE_ADMIN”,“参数”= >“_want_to_be_this_user”,),),),)); security.switch_user事件。的<一个href="https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php" class="reference external" title="SwitchUserEvent"rel="external noopener noreferrer" target="_blank">SwitchUserEvent一个>传递给侦听器,您可以使用它来获取您现在正在模拟的用户。 的<一个href="//www.oldmanjams.com/doc/2.7/session/locale_sticky_session.html" class="reference internal">在用户会话期间使区域设置“粘滞”一个>当您模拟用户时,Article不会更新区域设置。下面的代码示例将展示如何更改粘性区域设置: YAML XML PHP 1 2 3 4 5 6 # app / config / services.yml服务:app.switch_user_listener:类:AppBundle \ EventListener \ SwitchUserListener标签:-{名称:kernel.event_listener,事件:security.switch_user,方法:onSwitchUser} 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!--一个pp/config/services.xml --><??> . xml version="1.0" encoding="UTF-8"<容器xmlns=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services”xmlns: xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi: schemaLocation=“http://欧宝娱乐app下载地址www.oldmanjams.com/schema/dic/services //www.oldmanjams.com/schema/dic/services/services-1.0.xsd”><服务><服务id=“app.switch_user_listener”类=“AppBundle \ EventListener \ SwitchUserListener”><标签的名字=“kernel.event_listener”事件=“security.switch_user”方法=“onSwitchUser”/>服务>服务>容器> 1 2 3 4 5 6 7 8 9 10 / / app / config / services.php使用AppBundle\EventListener\SwitchUserListener;$容器->注册(“app.switch_user_listener”, SwitchUserListener::类)->addTag (“kernel.event_listener”,数组(“事件”= >“security.switch_user”,“方法”= >“onSwitchUser”,)); 谨慎 侦听器实现假设您的用户实体有一个getLocale ()方法: 12 3 4 5 6 7 8 9 10 11 12 13 14 15 / / src / AppBundle / EventListener / SwitchUserListener.php名称空间AppBundle\EventListener;使用欧宝娱乐app下载地址\组件\安全\Http\事件\SwitchUserEvent;类SwitchUserListener{公共函数onSwitchUser(SwitchUserEvent$事件){$事件->getRequest ()->getSession ()->集(“_locale”,$事件->getTargetUser ()->getLocale ());}}