首页 > 解决方案 > Pass userID in @PrePersist (@PreUpdate) method using Spring Boot and JWT

问题描述

I'm using JWT for authentication in my application and I need to pass the userID to the method @PrePersist in the @Entity classes. How can I do it?

This is my @RestController where pass the JwtToken:

@PostMapping(value = "/car")
public ResponseEntity<GenericResponse<String>> addCar(
        @RequestHeader(value = "Authorization") String token,
        @RequestParam(value = "plate") String plate) {
   // some stuff
}

And this is the PrePersist method in Entity classes:

@PrePersist
public void onPrePersist() {
    creationtime = Timestamp.from(Instant.now());
    //TODO creationuser = userID from token
}

Can I get the token info in Entity? What's the best way to do it?

标签: spring-boothibernatejpajwtspring-restcontroller

解决方案


推荐阅读