问题描述:

So, how can I get the user's id from a current logged user from any social providers?

Well I know I can build a custom SocialUser, the same I do for User, but in the case there is no getter on SocialUserDetails and the method I got just accepts userDetails, instead of a normal "Person" entity.

public class SocialUsersDetailServiceImpl implements SocialUserDetailsService {

private UserDetailsService service;

public SocialUsersDetailServiceImpl(UserDetailsService service) {

this.service = service;

}

@Override

public CSocialUserDetails loadUserByUserId(String username) throws UsernameNotFoundException, DataAccessException {

UserDetails userDetails = (UserDetails) service.loadUserByUsername(username);

return new CustomSocialUser(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities(), userDetails.getUserIdentifier()??);

}

}

But there is no ".getUserIdentifier()" method on UserDetails, there is some workaround for this?

The way I do for User:

@Service

public class UserDetailsServiceImpl implements CUserDetailsService {

@Resource

private PersonRepository respository;

@Override

public CUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

Person p = repository.findByUsername(username);

return new CUser(p.getUsername(), p.getPassword(), p.grantedAuthorities(), p.getIdPerson());

}

}

and the CUser:

public class CUser extends User{

private Number identifier;

public CUser(String username, String password, Collection<? extends GrantedAuthority> authorities, Number identifier) {

super(username, password, authorities);

this.identifier = identifier;

}

public Number getUserIdentifier() {

return identifier;

}

}

相关阅读:
Top