我通过创建新的symfony4框架将网站从Symfony 3.2更新为Symfony4,并将源代码从symfony3.2移至symfony4。
我已经按照Upgrading Symfony4中所述进行了更改。我还安装了所有必需的软件包。现在我可以进行路由并成功地将我的网页显示在显示器上,我面临的问题是登录表单没有不行。
在我的symfony3.2版本的登录控制器中,
我使用了security.encoder_factory服务
当我在symfony4中运行它时,结果是:
The "security.encoder_factory" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
我用于编码密码的控制器代码为:
public function loginAction(Request $request)
{
#$defaultEncoder = new MessageDigestPasswordEncoder('bcrypt', true, 5000);
$defaultEncoder = new MessageDigestPasswordEncoder('bcrypt');
$encoders = [
User::class => $defaultEncoder, // Your user class. This line specify you ant sha512 encoder for this user class
];
$encoderFactory = new EncoderFactory($encoders);
$data = [
'error' => null,
'_username' => null,
];
if ($request->isMethod('POST')) {
$_username = $request->request->get('_username');
$_password = $request->request->get('_password');
$data['_username'] = $_username;
$user = $this->getDoctrine()->getManager()
->getRepository("App:User")
->findOneBy(array('username' => $_username);
if (!$user) {
$data['error'] = 'User-Id does not exist';
return $this->render(
'login.html.twig',
$data
);
}
$encoder = $encoderFactory->getEncoder($user);
$salt = $user->getSalt();
#$encoder = $this->encodePassword($_password, $salt);
if (!$encoder->isPasswordValid($user->getPassword(), $encoder)) {
$data['error'] = 'User-Id or Password not valid.';
return $this->render(
'login.html.twig',
$data
);
}
return $this->redirect($this->generateUrl('default__home_page'));
}
return $this->render(
'login.html.twig',
$data
);
}
我的编码安全性是:
security:
encoders:
App\Entity\User: bcrypt
App\Security\User\WebserviceUser: bcrypt
Symfony\Component\Security\Core\User\User: bcrypt
FOS\UserBundle\Model\UserInterface: sha512
# https://symfony.com/doc/current/security.html#where-do-users-come-from- user-providers
providers:
#in_memory: { memory: ~ }
our_db_provider:
entity:
class: App:User
property: username
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
secured_area:
anonymous: ~
pattern: ^/s/login$
http_basic: ~
provider: fos_userbundle
user_checker: security.user_checker
form_login:
login_path: login_page
check_path: login_page
failure_handler: security.authentication.failure_handler
guard:
authenticators:
- App\Security\LoginControllerAuthenticator
# activate different ways to authenticate
# http_basic: ~
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
logout:
path: /logout/
target: /login/
是否可以使用其他方法或服务来编码以上代码所表示的密码。
错误:
No encoder has been configured for account "App\Entity\User".
at vendor/symfony/security/Core/Encoder/EncoderFactory.php:51
at Symfony\Component\Security\Core\Encoder\EncoderFactory->getEncoder(object(User))
(src/Controller/StudentController.php:65)
at App\Controller\StudentController->loginAction(object(Request))
(vendor/symfony/http-kernel/HttpKernel.php:149)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
谢谢。
答案 0 :(得分:1)
请参阅文档:
https://symfony.com/doc/current/components/security/authentication.html#the-password-encoder-factory
您可以通过这种方式在控制器中获得编码器。
编辑:
这里有个例子:
$defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000);
$encoders = [
User::class => $defaultEncoder, // Your user class. This line specify you ant sha512 encoder for this user class
];
$encoderFactory = new EncoderFactory($encoders);
$user = new User('test', null); // The user you want to authenticate
$password = $encoderFactory->getEncoder($user)->encodePassword('myPassword', 'mySalt');
编辑2:
您必须在此处定义您的用户类别:
$encoders = [
App\Entity\User::class => $defaultEncoder,
];
检查您是否未使用User Symfony类。