我正在使用 LexikJWTAuthenticationBundle 来生成json令牌,我的问题是我在验证用户是否在BD后不知道如何生成令牌。我能帮忙吗?
我的代码。
your_ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
songslist= new ArrayList<String>();
Toast.makeText(DataFromFirebase.this, "number of songs are " + dataSnapshot.getChildrenCount(), Toast.LENGTH_LONG).show();
//String value = (String) dataSnapshot.getValue();
sizetemp=dataSnapshot.getChildrenCount();
// Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
songslist.add(String.valueOf(dsp.getKey())); //add result into array list
//NOW YOU HAVE ARRAYLIST WHICH HOLD RESULTS
}
@Override
public void onCancelled(DatabaseError firebaseError) {
}
});
My Utilizador Class
public function postLoginAction(Request $request)
{
$user = new Utilizador();
$json = json_decode($request->getContent(), true);
$user->setUser($json['user']);
$user->setPass($json['pass']);
$em = $this->getDoctrine()->getManager();
$existuser = $em->getRepository('RestBundle:Utilizador')->findOneBy(array(
'user' => $user->getUser(),
'pass' => $user->getPass()
));
if($existuser)
{
$token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user);
return new JsonResponse(['token' => $token]);
}
}
Security.yml
<?php
namespace RestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Utilizador
*
* @ORM\Entity
* @ORM\Table(name="utilizador")
*/
class Utilizador implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nome", type="string", length=100, nullable=true)
*/
private $nome;
/**
* @var string
*
* @ORM\Column(name="user", type="string", length=100, nullable=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="pass", type="string", length=100, nullable=true)
*/
private $pass;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=100, nullable=true)
*/
private $email;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nome
*
* @param string $nome
*
* @return Utilizador
*/
public function setNome($nome)
{
$this->nome = $nome;
return $this;
}
/**
* Get nome
*
* @return string
*/
public function getNome()
{
return $this->nome;
}
/**
* Set user
*
* @param string $username
*
* @return Utilizador
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get user
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set pass
*
* @param string $pass
*
* @return Utilizador
*/
public function setPassword($pass)
{
$this->pass = $pass;
return $this;
}
/**
* Get pass
*
* @return string
*/
public function getPassword()
{
return $this->pass;
}
/**
* Set email
*
* @param string $email
*
* @return Utilizador
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function getSalt()
{
return null;
}
}
另一个API类
security:
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory: ~
firewalls:
login:
pattern: ^/api/logins
stateless: true
anonymous: true
api:
pattern: ^/api
stateless: true
lexik_jwt: ~
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
答案 0 :(得分:1)
以下是如何从有效的安全用户:
创建令牌$token = $this->get('lexik_jwt_authentication.jwt_manager')->create($user);
您可以直接尝试,但我想这对您不起作用,因为您的用户实体看起来非常自定义
要使其有效,您的用户实体必须实现Symfony\Component\Security\Core\User\UserInterface
。
为此,您应该真正配置实体用户提供程序,而不是通过实体管理器手动从控制器加载用户。 见How to load security users from the database
希望这能帮助你以正确的方式前进。
编辑:
以下是一个完整的示例,说明了从数据库加载用户并使用此捆绑包对用户进行身份验证所需执行的操作: