Is it possible to secure whole Controller in Symfony 2?

杀马特。学长 韩版系。学妹 提交于 2020-01-15 04:25:07

问题


I am using JMSSecurityExtra bundle for securing methods in my contoller. But is there any way that i can secure the whole controller with @Secure?


回答1:


This can be done as per Documentaion

https://github.com/schmittjoh/JMSSecurityExtraBundle/issues/50

Tip: If you like to secure all actions of the controller with the same rule, you may also specify @PreAuthorize on the class itself. Caution though, this rule is only applied to the methods which are declared in the class.

use JMS\SecurityExtraBundle\Annotation\PreAuthorize;

 /** @PreAuthorize("hasRole('A') or (hasRole('B') and hasRole('C'))") */
class MyService
{

    public function secureMethod()
    {
        // ...
    }
}



回答2:


This annotation can only be applied to methods.

You could do it like this though (it's a regex):

jms_security_extra:
    method_access_control:
        'AcmeDemoBundle:AdminController:.*Action': 'hasRole("ROLE_ADMIN")'

Read the documentation: http://jmsyst.com/bundles/JMSSecurityExtraBundle/master/method_security_authorization



来源:https://stackoverflow.com/questions/11999210/is-it-possible-to-secure-whole-controller-in-symfony-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!