My linux world » Java Spring Security – Social + Database User

Java Spring Security - Social + Database User


Contents

Prerequistes

Please read Java Spring Security – Database User

Configuration

SocialUserDetailsServiceImpl.java

@Service("socialUserDetailsService")
public class SocialUserDetailsServiceImpl implements SocialUserDetailsService{
 
	private UserDetailsService userDetailsService;
 
	@Autowired
	public SocialUserDetailsServiceImpl(UserDetailsService userDetailsService) {
          this.userDetailsService = userDetailsService;
        }	
 
 
	@Override
       public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException, DataAccessException {
		return (SocialUserDetails) userDetailsService.loadUserByUsername(userId);        
    }
 
}

SocialConfig

@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer {
 
	@Autowired
	private DataSource dataSource;
 
	@Autowired
	private MyUserService myUserService;
 
	@Override
	public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
		cfConfig.addConnectionFactory(new GoogleConnectionFactory(
				env.getProperty("google.consumerKey"),
				env.getProperty("google.consumerSecret")));
 
	}
 
	@Override
	public UserIdSource getUserIdSource() {
		return new AuthenticationNameUserIdSource();
	}
 
	@Override
	public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {		
		JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
		repository.setConnectionSignUp(new ImplicitConnectionSignup(myUserService));
		return repository;
	}
 
	@Bean
	public ConnectController connectController(
			ConnectionFactoryLocator connectionFactoryLocator,
			ConnectionRepository connectionRepository) {
		return new ConnectController(connectionFactoryLocator, connectionRepository);
	}
 
	@Bean
	public ProviderSignInController providerSignInController(
			ConnectionFactoryLocator connectionFactoryLocator,
			UsersConnectionRepository usersConnectionRepository) {
		ProviderSignInController controller = new ProviderSignInController(
				connectionFactoryLocator,
				usersConnectionRepository,
				new ImplicitSignInAdapter(new HttpSessionRequestCache()));
		controller.setSignUpUrl("/register");
		return controller;
	}
}

application.properties

In src/main/resources/application.properties, add the following lines (example for Google) :

google.consumerKey=YOUR-CONSUMER-KEY.apps.googleusercontent.com
google.consumerSecret=YOUR-CONSUMER-SECRET

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.