未定义的方法已在Symfony 4中授予

时间:2019-03-06 17:00:36

标签: php symfony symfony4

我正在使用Symfony4中的Symfony2应用程序中的一条语句:

$securityContext = $this->container->get('security.token_storage');         
if($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ){
. . .
}

我总是出错:

Attempted to call an undefined method named "isGranted" of class "Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage

我想念什么?

2 个答案:

答案 0 :(得分:1)

  

Symfony提供了几种执行授权的方法,包括[…]直接在isGranted服务上使用security.authorization_checker

     

source

您应该在isGranted服务而不是security.authorization_checker上致电security.token_storage

答案 1 :(得分:0)

对于SF4,根据文档:

%load_ext tensorboard.notebook
import tensorflow as tf
import numpy as np
x_train = [np.ones((1, 2))]
y_train = [np.ones(1)]

model = tf.keras.models.Sequential([tf.keras.layers.Dense(2, input_shape=(2, ))])

model.compile(
    optimizer='sgd',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])

logdir = "logs/"

tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)

model.fit(x_train,
          y_train,
          batch_size=1,
          epochs=1,
          callbacks=[tensorboard_callback])

%tensorboard --logdir logs/

您必须使用security.authorization_checker服务。上面的代码与:

public function hello($name)
{
    $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');

    // ...
}

在此处https://symfony.com/doc/4.0/security.html#securing-controllers-and-other-code

检查文档