Spring-data-jpa is encrypted with @ ColumnTransformer annotation, but the data returned after decryption is null

problem description

The password field is encrypted with @ ColumnTransformer annotation in

Springboot + Spring-data-jpa, but the data returned after decryption is null

related codes

entity class:
package top.inger.JpaDemo.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;

@ Data
@ AllArgsConstructor
@ NoArgsConstructor
@ Entity
@ EntityListeners (AuditingEntityListener.class)
@ JsonIgnoreProperties (value = {"adminRegTime", "adminModTime"}, allowGetters = true)
@ Table (name = "WINES_ADMIN")
public class Admin {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int adminId;

@Column(name = "username")
@NotBlank(message = "!")
@Length(min = 4, max = 8)
private String adminUsName;

@Column(name = "password")
@NotBlank(message = "!")
@Length(min = 3, max = 6)
@ColumnTransformer(
        write = "HEX(AES_ENCRYPT(?, "password"))",
        read = "AES_DECRYPT(UNHEX(password),"password")"
)
private String adminPassword;

@Column(name = "name")
@NotBlank(message = "!")
@Length(min = 2, max = 20)
private String adminName;

@Column(name = "phone")
@NotBlank(message = "!")
@Length(min = 11,max = 11)
private String adminPhone;

@Column(name = "status")
private Byte adminStatus=0;

@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false, updatable = false,name = "regTime")
private Date adminRegTime;

@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false,name = "lastModTime")
private Date adminLastModTime;

}

how to use specific fields:

@Column(name = "password")
@NotBlank(message = "!")
@Length(min = 3, max = 6)
@ColumnTransformer(
        write = "HEX(AES_ENCRYPT(?, "password"))",
        read = "AES_DECRYPT(UNHEX(password),"password")"
)
private String adminPassword;

repository:
package top.inger.JpaDemo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import top.inger.JpaDemo.domain.Admin;

@ Repository
public interface AdminRepository extends JpaRepository < Admin, Integer >, JpaSpecificationExecutor < Admin > {
}

controller:
@ RestController
@ RequestMapping ("/ admin")
public class AdminController {

private final AdminRepository adminRepository;

@Autowired
public AdminController(AdminRepository adminRepository) {
    this.adminRepository = adminRepository;
}

/**
 *   >  POST:  /admin/create
 */
@PostMapping("/create")
public Admin createAdmin(@RequestBody @Valid Admin admin) {
    return adminRepository.saveAndFlush(admin);
}

/**
 * id  >  GET:  /admin/findById/{adminId}
 */
@GetMapping("/findById/{adminId}")
public Optional<Admin> findAdminById(@PathVariable(value = "adminId") int id) {
    return adminRepository.findById(id);
}


what result do you expect? What is the error message actually seen?

create an administrator:

id3:

idea log printing:
Hibernate: select admin0_.id as id1_0_0_, admin0_.lastModTime as lastModT2_0_0_, admin0_.name as name3_0_0_, AES_DECRYPT (UNHEX (admin0_.password), "admin0_.password") as password4_0_0_, admin0_.phone as phone5_0_0_, admin0_.regTime as regTime6_0_0_, admin0_.status as status7_0_0_ Admin0_.username as username8_0_0_ from WINES_ADMIN admin0_ where admin0_.id=?

should return the password of sdfsd, but the result is null.

look forward to God"s help!


Try to add the forcolumn value to the

@ ColumnTransformer note

@Column(name = "password")
@NotBlank(message = "!")
@Length(min = 3, max = 6)
@ColumnTransformer(
        forColumn = "password",
        write = "HEX(AES_ENCRYPT(?, 'password'))",
        read = "AES_DECRYPT(UNHEX(password),'password')"
)
private String adminPassword;

current data operations are based on hibernate

HEX(AES_ENCRYPT(?, 'password'))

password in this code is encrypted salt bar.

from a security point of view, set the encryption salt to the environment variable.

Edit application.yml file

  

I just want to ask, have you solved the boss

?
Menu