我已经成功地实现了Symfony2.8的FosoAuthServerBundle,并且它起作用。当我尝试使用Symfony3.2进行操作时,我有错误:
尝试从symfony component component security core加载类" SecurityContext"类。您是否忘记了另一个名称空间的"使用"语句?
所以我谷歌搜索了,现在知道SecurityContext不再存在Symemofny 3.2。但是在fosoauthserverbundle官方文档中,"关于安全性的注释"仍然存在函数loginaction()仅与Symfony2紧缩。
问题是: - 我可以与Symfony 3.2一起使用此捆绑包吗? - 如果是,是否有任何文档如何执行或更好的示例?
非常感谢您的答案
不知道fosoauthserverbundle。但是我猜捆绑包的文档中的示例a_note_about_security刚刚过时。
自Symfony 2.6以来,Security.Context服务已弃用。在这里,您可以找到更改的描述:symfony.com/blog/new-inew-symfony-2-6-security-component-improvements
您可以尝试用SymfonyComponentSecurityCoreSecurity
SymfonyComponentSecurityCoreSecurityContext
<?php
// src/Acme/SecurityBundle/Controller/SecurityController.php
namespace AcmeSecurityBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentSecurityCoreSecurity;
class SecurityController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
} else {
$error = $session->get(Security::AUTHENTICATION_ERROR);
$session->remove(Security::AUTHENTICATION_ERROR);
}
// Add the following lines
if ($session->has('_security.target_path')) {
if (false !== strpos($session->get('_security.target_path'), $this->generateUrl('fos_oauth_server_authorize'))) {
$session->set('_fos_oauth_server.ensure_logout', true);
}
}
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(Security::LAST_USERNAME),
'error' => $error,
));
}
}
我弄清楚了。也许它可以帮助某人。
public function loginAction(Request $request) {
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('AppBundle:Security:login.html.twig', array(
'last_username' => $lastUsername
, 'error' => $error
));
}