jpa - @Transient注解,@org.springframework.data.annotation.Transient注解,transient关键字和密码存储

标签 jpa spring-security spring-data spring-annotations transient

目前正在学习Spring框架,主要关注它的Security Module。我看过一些有关注册和登录的指南。我在 User 类中的密码字段上看到了 transient 关键字或 @Transient 注释的常见用法。

我的虚拟应用程序使用 Spring Boot + Spring MVC + Spring Security + MySQL。

我知道

Java's transient keyword is used to denote that a field is not to be serialized.

JPA的@Transient注释...

...specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

以及 org.springframework.data.annotation 的 @Transient 注释..

Marks a field to be transient for the mapping framework. Thus the property will not be persisted and not further inspected by the mapping framework.

在我的 MySQL 数据库中,我有 spring_demo 架构,其中包含 3 个表:

+-----------------------+
| Tables_in_spring_demo |
+-----------------------+
| role                  |
| user                  |
| user_role             |
+-----------------------+

当我在 User 类的密码字段中使用 transient 关键字时,它不会存储在 MySQL 数据库中。 (示例:test01)

mysql> select * from user;
+----+--------+------------------+----------+
| id | active | email            | username |
+----+--------+------------------+----------+
|  1 |      1 | test01@gmail.com | test01   |
+----+--------+------------------+----------+
1 row in set (0,00 sec)

当我在 User 类中的密码字段上使用 javax.persistence @Transient 注释时,它也不会存储在 MySQL 数据库中。 (示例:test02)

但是...当我在 User 类中的密码字段上使用 org.springframework.data.annotation @Transient 注释时,它确实存储在 MySQL 数据库中。 (例如:test03)这是为什么?

mysql> select * from user;
+----+--------+------------------+----------+--------------------------------------------------------------+
| id | active | email            | username | password                                                     |
+----+--------+------------------+----------+--------------------------------------------------------------+
|  1 |      1 | test02@gmail.com | test02   |                                                              |
|  2 |      1 | test03@gmail.com | test03   | $2a$10$UbvmdhfcKxSNr/I4CjOLtOkKGX/j4/xQfFrv3FizxwEVk6D9sAoO  |
+----+--------+------------------+----------+--------------------------------------------------------------+
2 rows in set (0,00 sec)

我的主要问题是,当我使用基于 spring.data 的 @Transient 注释时,密码字段仍然存在。为什么?为什么我应该在密码字段上使用 @Transient 注释?

感谢您提前的指导和帮助!

最佳答案

在 Spring 框架中,您可以使用映射框架从一种形式转换为另一种形式。举例来说,您的 Spring Java 服务器端应用程序需要以 JSON 格式将用户信息发送到客户端(网页、移动应用程序)。

@Entity
public class User {

@Id
private long id;

@Column(name = "username")
private String username;

@Column(name = "email")
private String email;

@Column(name = "password")
private String password;

}

现在要将此 java 实体对象映射为 JSON 格式,您可以使用映射框架(例如 jackson:com.fasterxml.jackson.databind.ObjectMapper)或手动执行。

将用户 2 对象转换为 JSON 时得到的 JSON 格式输出为:

{
   "id": 2,
   "email": "test03@gmail.com",
   "username": "test03",
   "password": "$2a$10$UbvmdhfcKxSNr/I4CjOLtOkKGX/j4/xQfFrv3FizxwEVk6D9sAoO"
}

现在,如果您添加:

@org.springframework.data.annotation.Transient
@Column(name = "password")
private String password;

然后使用映射框架再次为您将获得的用户 2 实体生成 JSON:

{
   "id": 2,
   "email": "test03@gmail.com",
   "username": "test03",
}

请注意,JSON 输出中缺少密码字段。那是因为 @org.springframework.data.annotation.Transient 特别向 spring 框架声明,当从 Java 对象转换为 JSON 时,您使用的对象映射器不应包含此值。

另请注意,如果您尝试将上述实体持久保存到数据库中,它仍然会将其保存到数据库中,因为 @org.springframework.data.annotation.Transient 仅适用于对象映射框架,不适用于JPA。

回顾一下:

transient is for all serializations (over the wire, saving to disk, saving to db)
javax.persistence.Transient is specifically for JPA DB serialization @org.springframework.data.annotation.Transient is for ObjectMapping Framework serializations used within Spring

关于jpa - @Transient注解,@org.springframework.data.annotation.Transient注解,transient关键字和密码存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42750977/

相关文章:

java - Nullable @ManyToOne 不接受 null 值

java - Hibernate EntityManager.remove(entity) 从数据库和关联中删除记录,但不从实体集合中删除记录

java - 为什么不需要通过配置公开 JavaMailSender 即可 Autowiring ,Spring Boot 1.5.8

Spring Boot 安全性,仅对某些路由应用身份验证过滤器

spring - @Cacheable注解的value和cacheName参数的区别

java - 使用 eclipselink jpa 出现未知列错误

java - 系统异常与应用程序异常的清晰解释

java - 如何通过 application.properties 禁用 spring-security?

java - 如何使用 CrudRepository 从数据库获取 'now time'?

java - Spring Boot中如何使用ORDER BY?