java - Spring Security Authentication on Spring Boot using Spring Data JPA and Custom User/Group DB table -
i have spring boot project using mysql
, hibernate
, spring security
dependencies. security pretty simple. each of different users belongs particular user group(eg. admin, customer, helpdesk etc
).
codes below:
users model
@entity @table(name = "user") public class user extends baseentity implements serializable { private static final long serialversionuid = -999620920601692072l; @id @generatedvalue(strategy = identity) @column(name = "user_id", unique = true, nullable = false, insertable = false, updatable = false) private long id; @column(name="user") private string name; @column(name="user_password") private string password; @manytoone( fetch=fetchtype.lazy ) @joincolumn(name = "group_id", nullable = false) private usergroup usergroup; @manytoone( fetch=fetchtype.lazy ) @joincolumn(name = "status_id", nullable = false) private statususers status; public user() { } public user(user user) { this.name = user.name; this.password = user.password; this.usergroup = user.usergroup; this.status = user.status; } //getters , setters
usergroup model
@entity @table(name="user_group") public class usergroup extends baseentity implements serializable { private static final long serialversionuid = 511110846736323808l; @id @generatedvalue(strategy = identity) @column(name = "group_id", unique = true, nullable = false, insertable = false, updatable = false) private long id; @column(name = "group") private string name; public usergroup() { }//getters , setters
websecurityconfig
@enablewebsecurity public class websecurityconfig extends websecurityconfigureradapter { @autowired private userdetailsservice userdetailsservice; //@formatter:off @autowired public void configauthentication(authenticationmanagerbuilder authenticationmanagerbuilder) throws exception { //todo1: not sure line also! authenticationmanagerbuilder.userdetailsservice(userdetailsservice).passwordencoder(passwordencoder()); //authenticationmanagerbuilder.getdefaultuserdetailsservice(); } //@formatter:on //@formatter:off @override protected void configure(httpsecurity http) throws exception{ http .authorizerequests() .antmatchers("/build/**","/vendor/**","/about-us","/contact","/faq","/","/index","list-property","lost-password","rent","sale") .permitall() .and() .formlogin() .loginpage(appmappings.mapping_login) > //todo 2: want "username" , "password" database not sure > line .usernameparameter("username").passwordparameter("password") .and() .logout() .logouturl(appmappings.mapping_logout) .logoutsuccessurl(appmappings.mapping_login) .and() .exceptionhandling().accessdeniedpage(appmappings.mapping_accessdenied) .and(); } //@formatter:on @bean(name="passwordencoder") public passwordencoder passwordencoder(){ return new bcryptpasswordencoder(); } }
customuserdetails class
public class customuserdetails extends user implements userdetails { //todo 3: how implement getauthorities() method , constructor customuserdetails private static final long serialversionuid = -4038292874466984069l; //private string usergroups; public customuserdetails(user user){//, string usergroups) { super(user); //this.usergroups = usergroups; } public long getuserid(){ return super.getid(); } @override public collection<? extends grantedauthority> getauthorities() { //string roles= stringutils.collectiontocommadelimitedstring(usergroups); return authorityutils.commaseparatedstringtoauthoritylist(usergroups); } @override public string getpassword() { return null; } @override public string getusername() { return super.getname(); } @override public boolean isaccountnonexpired() { return !(super.getstatus().getid() == 2 | super.getstatus().getid() == 3); } @override public boolean isaccountnonlocked() { return isaccountnonexpired(); } @override public boolean iscredentialsnonexpired() { return isaccountnonexpired(); } @override public boolean isenabled() { return isaccountnonexpired(); } }
customuserdetailsservice class
public class customuserdetailsservice implements userdetailsservice { private final baseentityrepo baseentityrepo; public customuserdetailsservice(baseentityrepo baseentityrepo) { this.baseentityrepo = baseentityrepo; } @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { baseentity baseentity = baseentityrepo.findbyname(username); user user = (user)baseentity; if(null == user){ throw new usernamenotfoundexception("no user present username: "+username); }else{ //todo 4: not sure return type here! return new customuserdetails(user); } } }
baseentity model
@mappedsuperclass public class baseentity implements serializable{ private static final long serialversionuid = 930174351002422055l; public baseentity(){} @id @generatedvalue(strategy = identity) @column(name = "id", unique = true, nullable = false, insertable = false, updatable = false) private long id; @column(name="name") protected string name; @column(name = "created_at", nullable = false) @temporal( temporaltype.timestamp ) protected date createddate; @column(name = "last_modified" , nullable = false ) @temporal( temporaltype.timestamp) protected date lastmodified; //getter , setters!}
question: can modify codes implement authentication using spring data jpa have tried do? , if so, how? (or have use jdbc based authentication , how like) note: special references todos (1-4)
Comments
Post a Comment