Symfony2 security @Secure annotation not working

2019-07-04 07:59发布

问题:

I'm attempting to use annotations to secure my controllers:

namespace Vinny\StreamBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;


class HomeController extends Controller
{

    /**
     * @Route("/home", name="home") 
     * @Secure(roles="ROLE_USER")
     */
    public function indexAction()
    {
     ...

But I can't seem to get my controllers to actually be secured. Is there any instances on which this would be ignored?

回答1:

On an almost completely unrelated note, the issue of my problem didn't stem from anything in my configuration or controller, but was an migration mixup from the switch from Symfony 2.0 to Symfony 2.1. I was failing to register the JMSDiExtraBundle which was silently crippling the JMSSecurityExtraBundle's (and others') annotations.

In my AppKernel.php, I had been missing:

public function registerBundles()
{
    $bundles = array(
    ...
    new JMS\DiExtraBundle\JMSDiExtraBundle($this),
    ...

With that everything works fine, again.



回答2:

You need to try with ROLE_ADMIN or ROLE_SUPER and then see if its secured or not. ROLE_USER is the deafult role applied to all users , so u are allowed

@Secure(roles="ROLE_SUPER")