0

So i am trying to implement custom userdetails in spring using kotlin data class. but when i implemented it and my ide started throwing declartion class errors so to get rid of it i made all the fields private.i can implement custom getters and setters but i really like to know if their is a way around it.



@Entity
@Table(name="user_table")
data class UserModel(

    @Column(unique = true)
    private var email: String,
    @Column(unique = true)
    private var username: String,
    private var password: String,
    private var date: Date,
    private var authority: GrantedAuthority,
    private var enabled: Boolean,
    @Column(name="user_uuid",unique=true)
    private var uuid: UUID,
    @ManyToMany

    var roles:List<RoleModel>,
    @Id
    @GeneratedValue(generator = "UUID")
    var id: Long?=null
):UserDetails{
    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
        TODO("Not yet implemented")
    }

    override fun getPassword(): String {
        TODO("Not yet implemented")
    }

    override fun getUsername(): String {
        TODO("Not yet implemented")
    }

    override fun isAccountNonExpired(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isAccountNonLocked(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isCredentialsNonExpired(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isEnabled(): Boolean {
        TODO("Not yet implemented")
    }

}

and what concerns me the most is the thing mentioned at bottom of this question

klaus
  • 43
  • 1
  • 5
  • data classes allow read access to fields by default, no need for getters – DCTID Mar 20 '21 at 18:54
  • @DCTID but not for private data members – klaus Mar 21 '21 at 06:15
  • Aside from that. He is implementing UserDetails interface. That needs to be implemented to satisfy the contract. – Daniel Jacob Mar 22 '21 at 15:25
  • 2
    What I was getting at is the fields should not be private. The entity should be a representation of the db table. I would then create an extension function that converts the entity to a `UserDetails` implementation. I believe kotlin supports this very well, you can declare all the classes, extension function and even repo in the same file. This really follows the single responsibility of SOLID well and is easier to understand than having your entity implement Spring Security stuff. Then if the table or Spring Security changes, you only modify the converter nothing else needs to know. – DCTID Mar 23 '21 at 03:29

0 Answers0