那么,如何从任何社交提供商的当前登录用户获取用户的ID? 我知道我可以构建一个自定义的SocialUser,就像我为User做的那样,但是在SocialUserDetails上没有getter的情况下我得到的方法只接受userDetails,而不是普通的" Person"实体。
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()??);
}
}
但是没有" .getUserIdentifier()"对于UserDetails的方法,有一些解决方法吗?
我为用户做的方式:
@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());
}
}
和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;
}
}