Php HWIOAuthBundle和自定义UserBundle可以';他似乎不在一起工作
我似乎无法让Hwioauthbundle在symfony3中与我的自定义userbundle一起工作。我想在我的应用程序中使用googleoauth,用户可以使用google登录,也可以使用注册表。这两种方法都会将用户存储在数据库中。注册表单本身工作得很好,但在引入hwioauthbundle时,事情对我来说并不顺利。我遇到的一些问题包括:Php HWIOAuthBundle和自定义UserBundle可以';他似乎不在一起工作,php,symfony,security,google-oauth,symfony-3.2,Php,Symfony,Security,Google Oauth,Symfony 3.2,我似乎无法让Hwioauthbundle在symfony3中与我的自定义userbundle一起工作。我想在我的应用程序中使用googleoauth,用户可以使用google登录,也可以使用注册表。这两种方法都会将用户存储在数据库中。注册表单本身工作得很好,但在引入hwioauthbundle时,事情对我来说并不顺利。我遇到的一些问题包括: 在打开我的登录页面时,我得到一个空白页面,页面上有一个链接,文本为“google”,这与我预期的登录表单相反。但是,如果我将登录页面更改为“登录”和“登录”
providers:
intersect_provider:
entity:
class: UserBundle:User
property: username
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
guard:
authenticators:
- intersect.authenticator.form_login
entry_point: intersect.authenticator.form_login
form_login:
provider: intersect_provider
login_path: /login
check_path: /login
logout:
path: /signout
target: /
oauth:
resource_owners:
google: "/login/check-google"
login_path: /
use_forward: false
failure_path: /login
oauth_user_provider:
service: intersect.authenticator.oauth
Security.yml文件
hwi_oauth:
firewall_names: [ "main" ]
resource_owners:
google:
type: google
client_id: <clientid>
client_secret: <secret>
scope: "email profile"
options:
access_type: offline
csrf: true
services.yml文件
hwi_oauth_login:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /login
hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix: /login
google_login:
path: /login/check-google
user:
resource: "@UserBundle/Controller/"
type: annotation
prefix: /
app:
resource: "@AppBundle/Controller/"
type: annotation
Routing.yml文件
<?php
namespace Intersect\UserBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Security;
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
private $em;
private $passwordEncoder;
private $router;
public function __construct(EntityManager $em, RouterInterface $router, UserPasswordEncoder $passwordEncoder )
{
$this->passwordEncoder = $passwordEncoder;
$this->em = $em;
$this->router = $router;
}
public function getCredentials(Request $request)
{
$isLoginSubmit = $request->getPathInfo() == '/login' && $request->isMethod('POST');
if(!$isLoginSubmit){
return;
}
$username = $request->request->get('_username');
$request->getSession()->set(Security::LAST_USERNAME, $username);
$password = $request->request->get('_password');
return [
'username'=>$username,
'password'=>$password
];
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$username=$credentials['username'];
return $this->em->getRepository('UserBundle:User')->findByUsernameOrEmail($username);
}
public function checkCredentials($credentials, UserInterface $user)
{
$password=$credentials['password'];
if ($this->passwordEncoder->isPasswordValid($user, $password)) {
return true;
}
return false;
}
protected function getLoginUrl()
{
return $this->router->generate('intersect_login');
}
protected function getDefaultSuccessRedirectUrl()
{
return $this->router->generate('homepage');
}
}
<?php
namespace Intersect\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Intersect\UserBundle\Entity\User;
class SecurityController extends Controller
{
/**
* @Route("/login",name="intersect_login")
*/
public function loginAction(){
$helper = $this->get('security.authentication_utils');
return $this->render('Intersect/login.html.twig',[
'last_username' => $helper->getLastUsername(),
'error' => $helper->getLastAuthenticationError(),
]
);
}
/**
* @Route("/signup",name="intersect_signup")
*/
public function signupAction(Request $request)
{
$user = new User;
$regform = $this->createForm('Intersect\UserBundle\Form\SignUpType', $user);
$regform->handleRequest($request);
if($regform->isSubmitted() && $regform->isValid()){
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('homepage');
}
return $this->render('Intersect/signup.html.twig',[
'regform'=>$regform->createView()
]);
}
}
我想我已经解决了前面提到的问题,但是我还没有完成整个过程。我是怎么解决的
我将hwi_oauth_登录前缀更改为
/连接
从
/登录
在Routing.yml中
hwi_oauth_login:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /connect
然后在SecurityController.php中为我的登录路径添加了一个正斜杠
/**
* @Route("/login/",name="intersect_login")
*/
public function loginAction(){
$helper = $this->get('security.authentication_utils');
return $this->render('Intersect/signin.html.twig',[
'last_username' => $helper->getLastUsername(),
'error' => $helper->getLastAuthenticationError(),
]
);
}
用于启动google Authentication的url为
/连接/
“谷歌”的占位符在哪里
现在我得到了一份工作
无法从不支持的EntityUserProvider刷新用户
包含一个标识符。用户对象必须使用其
由条令映射的自己的标识符
错误,我希望我能从其他线程获得解决方案
hwi_oauth_login:
resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
prefix: /connect
/**
* @Route("/login/",name="intersect_login")
*/
public function loginAction(){
$helper = $this->get('security.authentication_utils');
return $this->render('Intersect/signin.html.twig',[
'last_username' => $helper->getLastUsername(),
'error' => $helper->getLastAuthenticationError(),
]
);
}