2020-03-19 14:56:31 +01:00
|
|
|
package com.baeldung.config;
|
2019-10-31 20:43:47 -05:00
|
|
|
|
2020-03-19 14:56:31 +01:00
|
|
|
import com.baeldung.security.FacebookSignInAdapter;
|
|
|
|
import com.baeldung.security.FacebookConnectionSignup;
|
2019-10-31 20:43:47 -05:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
|
import org.springframework.social.connect.ConnectionFactoryLocator;
|
|
|
|
import org.springframework.social.connect.UsersConnectionRepository;
|
|
|
|
import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository;
|
|
|
|
import org.springframework.social.connect.web.ProviderSignInController;
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@EnableWebSecurity
|
2020-03-19 14:56:31 +01:00
|
|
|
@ComponentScan(basePackages = { "com.baeldung.security" })
|
2019-10-31 20:43:47 -05:00
|
|
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserDetailsService userDetailsService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private ConnectionFactoryLocator connectionFactoryLocator;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UsersConnectionRepository usersConnectionRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private FacebookConnectionSignup facebookConnectionSignup;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
|
|
|
auth.userDetailsService(userDetailsService);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void configure(final HttpSecurity http) throws Exception {
|
|
|
|
// @formatter:off
|
|
|
|
http
|
|
|
|
.csrf().disable()
|
|
|
|
.authorizeRequests()
|
|
|
|
.antMatchers("/login*","/signin/**","/signup/**").permitAll()
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
.and()
|
|
|
|
.formLogin().loginPage("/login").permitAll()
|
|
|
|
.and()
|
|
|
|
.logout();
|
|
|
|
} // @formatter:on
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
// @Primary
|
|
|
|
public ProviderSignInController providerSignInController() {
|
|
|
|
((InMemoryUsersConnectionRepository) usersConnectionRepository).setConnectionSignUp(facebookConnectionSignup);
|
|
|
|
return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new FacebookSignInAdapter());
|
|
|
|
}
|
|
|
|
}
|