java - how to get user info from jdbc user service -
i using spring security authorization secure user login application using following code:
<jdbc-user-service data-source-ref="datasource" users-by-username-query="select username, password password,1 enabled users username=?" authorities-by-username-query="select username, authority,1 enabled users username =?" />
is there way add other fields query ex:id,email ... ?
if want load complete custom user object database, , set in securitycontextholder
/session
, better approach use custom userdetailservice
create custom user entity:
first create simple custom user entity according database table
@entity @table(name = "users") public class securityuser implements serializable{ /** * */ private static final long serialversionuid = 1l; @id @column(name = "username", nullable = false, unique = true) private string username; @column(name = "password", nullable = false) @notnull private string password; @column(name = "type", nullable = false) @notnull private string type; @column(name = "full_name", nullable = false) @notnull private string fullname; @column(name = "email", nullable = false) @notnull private string email; //gettters & setters plus default constructor }
create repository/dao accessing above user database
@repository public interface userrepository extends jparepository<securityuser, long> { securityuser findbyusername(string username); }
create custom user object logged in user extending spring's user
public class currentuser extends user { /** * */ private static final long serialversionuid = 1l; private securityuser securityuser; public currentuser(securityuser securityuser) { super(securityuser.getusername(), securityuser.getpassword(), authorityutils.createauthoritylist(securityuser.getrole().tostring())); this.securityuser = securityuser; } public securityuser getsecurityuser() { return securityuser; } public string getrole() { return securityuser.getrole(); } }
create custom user detail service
@service public class currentuserdetailsservice implements userdetailsservice { @autowired private userrepository userservice; @autowired public currentuserdetailsservice(userrepository userservice) { this.userservice = userservice; } public currentuserdetailsservice() { } @override public currentuser loaduserbyusername(string username) throws usernamenotfoundexception, dataaccessexception { securityuser user = userservice.findbyusername(username); return new currentuser(user); } }
set userdetailservice in configuration
as using xml based configuration, in config file:
<bean id="myuserdetailsservice" class="complete-path-to-serviceclasss.myuserdetailsservice"/> <security:authentication-manager> <security:authentication-provider user-service-ref="myuserdetailsservice" > <security:password-encoder ref="passwordencoder"> </security:password-encoder> </security:authentication-provider> </security:authentication-manager>
now anywhere, if execute below lines when user loggedin, give currentuser
object data
currentuser user = securitycontextholder.getcontext().getauthentication().getprincipal()
Comments
Post a Comment