简体   繁体   English

JWT 令牌的 UniqueConstraint - 如何?

[英]UniqueConstraint for JWT token - how to?

My enviroment:我的环境:

lexik_jwt_authentication:
   user_identity_field: phoneNumber
security:
   providers:
        chain_provider:
            chain:
                providers: ['fos_userbundle', 'app_user_provider']
        fos_userbundle:
            id: fos_user.user_provider.username
        app_user_provider:
            entity:
                class: App\Entity\User
                property: phoneNumber

in App\Entity\User在应用\实体\用户

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(
 *     name="`user`",
 *     uniqueConstraints={
 *        @ORM\UniqueConstraint(name="project_phoneNumber", columns={"project_id", "phone_number"})
 *     }
 * )
 * @ORM\HasLifecycleCallbacks()
 * @JMS\ExclusionPolicy("all")
 * @Vich\Uploadable
 */

So the problem is that phoneNumber is not unique for user any more.所以问题是 phoneNumber 对于用户来说不再是唯一的。 Is it possible to use project_phoneNumber for user identity in jwt?是否可以在 jwt 中使用 project_phoneNumber 作为用户身份?

What you trying to do is a compound primary key.您尝试做的是复合主键。

This is a very good way to proceed at a database level.这是在数据库级别进行的非常好的方法。 So the way you want to do it is the good way.所以你想做的就是好方法。

Problem is that even though doctrine say that they:问题是即使 doctrine 说他们:

...took good care to make sure Doctrine ORM supports as many of the composite primary key use-cases https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/tutorials/composite-primary-keys.html ...took good care to make sure Doctrine ORM supports as many of the composite primary key use-cases https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/tutorials/composite-primary-键.html

This is in fact a real pain when you face one unsupported feature for composite primary key (example: when you want to perform a manyToMany on two entity with composed primary key...).当您面对复合主键的一个不受支持的功能时,这实际上是一个真正的痛苦(例如:当您想对具有复合主键的两个实体执行 manyToMany 时......)。

So what I recommend to do is to always use id as primary key even though it'll violate the 1NF .所以我建议做的是始终使用 id 作为主键,即使它会违反1NF

This is an acceptable violation... I mean not for me but there is no other way to handle it for complex entity relation.这是可以接受的违规行为……我的意思不是对我来说,但对于复杂的实体关系,没有其他方法可以处理它。

So if you want to try your luck you may implement a composite primary key and so remove you unique constraint which is handle by the primary key itself.因此,如果您想试试运气,您可以实现一个复合主键,从而删除由主键本身处理的唯一约束。

Else, just use a simple auto-incremented id and you'll be good (and so keep your unique constraint).否则,只需使用一个简单的自动递增 id 就可以了(并保持您的唯一约束)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM