javaspringspring-bootspring-securityuserdetailsservice

Is it OK to use a domain class to implement UserDetails interface in a Spring Boot project


Is it OK to use a domain class like the one below to implement UserDetails interface in a Spring Boot with Hibernate? Or is it preferable to use a wrapper class that has a private User instance?

@Entity
@Table(name = "users")
public class User
    {

    // All fields have setters and getter that aren't included in the code

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private Integer id;

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

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

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

    }

Solution

  • It is ok. You will have to implement a couple of methods that handle the roles functionnality. Also from my experience username and password must exist as class attributes.

    Here is a good exemple of how to implement JWT with springboot : https://auth0.com/blog/implementing-jwt-authentication-on-spring-boot/

    It contains a User class that implements UserDetails.