java - How to config Spring Security with JPA? -
i need add spring security project. right way it? have entities user , userrole , dao , services them. use entitymanager access data. read, need write implementation userdetails, don't know how correctly. here code:
user.java
@id @generatedvalue(strategy = generationtype.auto) @jsonproperty private integer id; @column(name = "username", length = 20, nullable = false) @jsonproperty private string username; @column(name = "password", nullable = false) @jsonproperty private string password; @column(name = "enabled", nullable = false) @jsonproperty private boolean enabled; @column(name = "email", nullable = false) @jsonproperty private string email; @onetomany(mappedby = "user", cascade = {cascadetype.all}, fetch = fetchtype.eager) private set<userrole> userroles; //getters , setters
userrole.java
@id @generatedvalue(strategy = generationtype.auto) @jsonproperty private integer id; @manytoone(optional = false) @joincolumn(name = "user_id", referencedcolumnname = "id", foreignkey = @foreignkey(constraintmode.no_constraint)) private user user; @column(name="role") @jsonproperty private string role; //getters , setters
what should do?
i had wrote blog post looking for. see post , pretty sure answer question:
in service layer of userdetails below, pay attention implements userdetailsservice org.springframework.security.core.userdetails.userdetailsservice.
and :
he loaduserbyusername methods return result of assembler.builduserfromuserentity . put, method of assembler to construct org.springframework.security.core.userdetails.user object given userentity dto. code of assembler class given below:
Comments
Post a Comment