在我目前的春季项目中,我最近使用Spring-Social添加了对社交登录的支持。它的主要配置类是:
@Configuration
@EnableSocial
@ComponentScan(value="com.webapp.loja")
public class SocialContext implements SocialConfigurer {
@Autowired
private DataSource dataSource;
@Autowired
private SignInAdapter signInAdapter;
@Autowired
private ConnectionSignUp connectionSignUp;
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
cfConfig.addConnectionFactory(new TwitterConnectionFactory("...", "..."));
cfConfig.addConnectionFactory(new FacebookConnectionFactory("...", "..."));
}
@Override
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
repository.setConnectionSignUp(connectionSignUp);
return repository;
}
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
@Bean
public ProviderSignInController providerSignInController(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository connectionRepository) {
return new ProviderSignInController(connectionFactoryLocator, connectionRepository, signInAdapter);
}
}
我的问题是,当我尝试使用任何社交媒体服务登录应用程序时,应用程序执行connectionSignUp
类,然后执行signInAdapter
。
我想要oposite行为:首先应用程序应该执行signInAdapter
,我尝试查找用户是否已经存在:
@Service
public class SimpleSignInAdapter implements SignInAdapter {
@Autowired
private SocialUserDetailsService socialUserDetailsService;
@Override
public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
SocialUserDetails user = socialUserDetailsService.loadUserByUserId(userId);
if(user != null) {
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), user.getAuthorities()));
return user.getUsername();
} else {
return null;
}
}
}
仅当未找到用户(返回null)时,应用程序才应执行connectionSignUp
类以创建新帐户:
@Component
public class AccountConnectionSignUp implements ConnectionSignUp {
@Autowired
private UsuarioHome accountRepository;
@Override
public String execute(Connection<?> connection) {
UserProfile profile = connection.fetchUserProfile();
Usuario account = new Usuario(profile.getUsername(), profile.getFirstName(), profile.getLastName(), profile.getEmail());
accountRepository.persist(account);
return profile.getUsername();
}
}
有人知道这是否可行?
答案 0 :(得分:0)