Not able to populate the User object when using Spring OAuth2 Jdbc token store -
i updated roy clarkson's spring rest service (https://github.com/royclarkson/spring-rest-service-oauth) jdbc-based token store. original implementation uses in-memory token store. able see user details in user object. on other hand, after switching jdbc-based token store, fields in user object empty. appears somehow spring security not able associate access token user under obtained token when using jdbc-based token store.
the in-memory token store implementation:
@configuration @enableauthorizationserver protected static class authorizationserverconfiguration extends authorizationserverconfigureradapter { @autowired private datasource datasource; private tokenstore tokenstore = new inmemorytokenstore(); @autowired @qualifier("authenticationmanagerbean") private authenticationmanager authenticationmanager; @autowired private customuserdetailsservice userdetailsservice; @autowired private clientdetailsservice clientdetailsservice; @bean public clientdetailsservice clientdetailsservice() { return new jdbcclientdetailsservice(datasource); } @override public void configure(authorizationserverendpointsconfigurer endpoints) throws exception { // @formatter:off endpoints .tokenstore(this.tokenstore) .authenticationmanager(this.authenticationmanager) .userdetailsservice(userdetailsservice); // @formatter:on } @override public void configure(clientdetailsserviceconfigurer clients) throws exception { clients .withclientdetails(clientdetailsservice); } @bean @primary public defaulttokenservices tokenservices() { defaulttokenservices tokenservices = new defaulttokenservices(); tokenservices.setsupportrefreshtoken(true); tokenservices.settokenstore(this.tokenstore); return tokenservices; } }
the rest endpoint:
@requestmapping("/greeting") public greeting greeting(@authenticationprincipal user user) { return new greeting(counter.incrementandget(), string.format(template, user.getname())); }
user.getname() returns name of user under obtained access token.
the jdbc token store implementation:
@configuration @enableauthorizationserver protected static class authorizationserverconfiguration extends authorizationserverconfigureradapter { @autowired private datasource datasource; @autowired private tokenstore tokenstore; @bean public tokenstore tokenstore() { return new jdbctokenstore(datasource); } @autowired @qualifier("authenticationmanagerbean") private authenticationmanager authenticationmanager; @autowired private customuserdetailsservice userdetailsservice; @autowired private clientdetailsservice clientdetailsservice; @bean public clientdetailsservice clientdetailsservice() { return new jdbcclientdetailsservice(datasource); } @override public void configure(authorizationserverendpointsconfigurer endpoints) throws exception { // @formatter:off endpoints .tokenstore(this.tokenstore) .authenticationmanager(this.authenticationmanager) .userdetailsservice(userdetailsservice); // @formatter:on } @override public void configure(clientdetailsserviceconfigurer clients) throws exception { clients .withclientdetails(clientdetailsservice); } @bean @primary public defaulttokenservices tokenservices() { defaulttokenservices tokenservices = new defaulttokenservices(); tokenservices.setsupportrefreshtoken(true); tokenservices.settokenstore(this.tokenstore); return tokenservices; } }
the rest endpoint:
@requestmapping("/greeting") public greeting greeting(@authenticationprincipal user user) { return new greeting(counter.incrementandget(), string.format(template, user.getname())); }
user.getname() returns null.
customuserdetailsservice
@service public class customuserdetailsservice implements userdetailsservice { private final userrepository userrepository; @autowired public customuserdetailsservice(userrepository userrepository) { this.userrepository = userrepository; } @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { user user = userrepository.findbylogin(username); if (user == null) { throw new usernamenotfoundexception(string.format("user %s not exist!", username)); } return new userrepositoryuserdetails(user); } private final static class userrepositoryuserdetails extends user implements userdetails { private static final long serialversionuid = 1l; private userrepositoryuserdetails(user user) { super(user); } @override public collection<? extends grantedauthority> getauthorities() { return getroles(); } @override public string getusername() { return getlogin(); } @override public boolean isaccountnonexpired() { return true; } @override public boolean isaccountnonlocked() { return true; } @override public boolean iscredentialsnonexpired() { return true; } @override public boolean isenabled() { return true; } } }
user
@entity public class user { @id @generatedvalue(strategy = generationtype.auto) private integer id; @notempty private string name; @notempty @column(unique = true, nullable = false) private string login; @notempty private string password; @notempty private string privilege; @jsonignore @manytomany(fetch = fetchtype.eager) @jointable(name = "user_role", joincolumns = { @joincolumn(name = "user_id") }, inversejoincolumns = { @joincolumn(name = "role_id") }) private set<role> roles = new hashset<role>(); public user() { } public user(user user) { super(); this.id = user.getid(); this.name = user.getname(); this.login = user.getlogin(); this.password = user.getpassword(); this.roles = user.getroles(); this.privilege = user.getprivilege(); } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getlogin() { return login; } public void setlogin(string login) { this.login = login; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getprivilege() {return privilege; } public void setprivilege(string privilege) {this.privilege = privilege; } public set<role> getroles() { return roles; } public void setroles(set<role> roles) { this.roles = roles; } }
the issue "userrepositoryuserdetails" creating not serializable.
userrepositoryuserdetails implementing "userdetails" serializable class extending "user" not serializable.
you must getting warning comiler add serialid.
solution
make userrepositoryuserdetails serializable.
Comments
Post a Comment