Is it possible to secure whole Controller in Symfo

2019-07-13 10:56发布

问题:

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



标签: php symfony jms