hibernate - Spring Security Set Role On Registration -


i'm new spring security, i've followed tutorials i'm having trouble understanding how structure of roles works under hood. have 2 tables, 1 user:

        @entity         @table(name = "userprofile", schema = "dbo", catalog = "devtestteam")         public class userprofileentity implements userdetails{              @id             @generatedvalue(strategy = generationtype.identity)             @column(name = "id", nullable = false)             private long id;              @column(name = "enabled", nullable = false)             private boolean enabled;              @notempty(message = "enter password.")             @size(min = 6, max = 15, message = "password must between 6 , 15 characters.")             @column(name = "password", nullable = true, length = 100)             private string password;              @notempty(message = "enter username.")             @size(min = 6, max = 20, message = "username must between 6 , 20 characters.")             @column(name = "username", nullable = true, length = 20, unique = true)             private string username;              @onetoone             @joincolumn(name = "role_id")             private roleentity role;              public roleentity getrole() {                 return role;             }              public void setrole(roleentity role) {                 this.role = role;             }              @override             public collection<? extends grantedauthority> getauthorities() {                 list<grantedauthority> authorities = new arraylist<>();                 authorities.add(new simplegrantedauthority("role_user"));                 return authorities;             } 

and 1 role:

@entity @table(name = "role", schema = "dbo", catalog = "devtestteam") public class roleentity {  @id @generatedvalue(strategy = generationtype.identity) @column(name = "id", nullable = false) private long id;  @column(name = "name", nullable = true, length = 255) private string name;  public long getid() {     return id; }  public void setid(long id) {     this.id = id; }  public string getname() {     return name; }  public void setname(string name) {     this.name = name; } 

my confusion comes when creating new user. have registration form backed userprofileentity object, , populates username , password. it's easy setenabled()=true (i left of getters/setters out of code clarity).

my question how set role when instantiating userprofileentity saved in database. role_id foreign key should take integer , return role role table, i'm not sure how express when instantiating. have role_user in roles table id of 1, , feel pretty simple instantiate can't find answer i'm looking for.

userimpl:

@service public class userprofileserviceimpl implements userprofileservice{ @autowired private userprofiledao userdao;  @override public userprofileentity findbyuser(string username) {     return userdao.findbyusername(username); }  @override public list<userprofileentity> findall() {     list<userprofileentity> list = userdao.findall();     return list; }  @override public userprofileentity save(userprofileentity persisted) {     userdao.save(persisted);     return null; }  @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception {     userprofileentity user = userdao.findbyusername(username);     if (user == null) {         throw new usernamenotfoundexception("user not found.");     }      return user; } 

}

you'll need repository method obtain user role name:

roleentity roleentity = roleentityrepository.findbyname("role_user"); 

then set roleentity userprofileentity before persisting it:

userprofileentity userprofileentity = new userprofileentity(); userprofileentity.setroleentity(roleentity); userservice.save(userprofileentity); 

what want leave userprofileentity unextended. spring security, you'll need userdetailsservice implementation:

@service("userdetailsservice") public class userdetailsserviceimpl implements userdetailsservice {      @autowired     private userrepository userrepository;      @override     public userdetails loaduserbyusername(string username) throws usernamenotfoundexception {          userprofileentity userprofileentity = userrepository.findbyusername(username);          if (userprofileentity == null) {             throw new usernamenotfoundexception("non existing user!");         }          return new org.springframework.security.core.userdetails.user(userprofileentity.getusername(),                 userprofileentity.getpassword(),                 arrays.aslist(new simplegrantedauthority(userbyusername.getroleentity().getname())));     }  } 

however, see requirements quite simple - 1 role per user. therefore, roleentity enum predefined roles:

public enum roleentity {     role_user } 

and in userprofileentity you'd use this:

public class userprofileentity {     @enumerated(enumtype.string)     private roleentity roleentity; } 

to persist user role:

userprofileentity userprofileentity = new userprofileentity(); userprofileentity.setroleentity(roleentity.user); userservice.save(userprofileentity); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -