使用Facebook重定向AfterLogin

时间:2017-11-15 06:23:54

标签: java spring-mvc spring-boot spring-security facebook-login

我使用ConnectionSignUp和SignInAdapter为网站做登录功能。我也使用spring security.But我不知道如何在登录后完成加载当前页面。 登录后,该网站将重新加载表单页面:localhost:9090 /# =

ConnectionSignUp.java

public class FacebookSignInAdapter implements SignInAdapter {
@Autowired
UsersRepository usersService; 

@Override
public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {

    SecurityContextHolder.getContext()
    .setAuthentication(new UsernamePasswordAuthenticationToken(connection.getKey(), 
            null, Arrays.asList(new SimpleGrantedAuthority("ROLE_FACEBOOK"))));

    return null;
}}

FacebookConnectionSignup.java

public class FacebookConnectionSignup implements ConnectionSignUp {

@Autowired
 UsersService usersService;
@Autowired
RolesService rolesService;

@Override
public String execute(Connection<?> connection) {

     Users user = null;
     try {
        user = usersService.findByUserName(connection.getKey().toString());
        if(user == null ) {
            user = new Users();
            user.setUserName(connection.getKey().toString());
            user.setPassword(randomAlphabetic(8));
            user.setEmail(connection.getKey()+"@gmail.com");
            user.setFirstName(connection.getDisplayName());
            user.setAvatar(connection.getImageUrl());
            user.setStatus("active");
            user.setCreatedDate(new Date());
            user.setLoggedInDate(new Date());
            user.setIsOnline((byte) 1);
            HashSet<Roles> roleses = new  HashSet<>();
            roleses.add(rolesService.findByName("ROLE_FACEBOOK"));
            user.setRoleses(roleses);
            usersService.saveorupdate(user);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return user.getUserName();
}}

WebSecurityConfig.java

@Configuration
@EnableWebSecurity

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private  AjaxAuthenticationFailureHandler ajaxAuthenticationFailureHandler;

    @Autowired
    private  AjaxAuthenticationSuccessHandler ajaxAuthenticationSuccessHandler;
    @Autowired
    private  ConnectionFactoryLocator connectionFactoryLocator;

    @Autowired
    private  UsersConnectionRepository usersConnectionRepository;

    @Autowired
    private  FacebookConnectionSignup facebookConnectionSignup;
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/home", "/").permitAll()
                 .antMatchers("/admin/**").hasRole("ADMIN")
    .and().formLogin().loginPage("/403").loginProcessingUrl("/login").usernameParameter("userName").passwordParameter("password")
                .failureHandler(ajaxAuthenticationFailureHandler).successHandler(ajaxAuthenticationSuccessHandler)

                .and().logout().logoutSuccessUrl("/")

                .and().rememberMe().and()
                .exceptionHandling().accessDeniedPage("/403");
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);

    }

    @Bean
    public ProviderSignInController providerSignInController() {
        ((InMemoryUsersConnectionRepository) usersConnectionRepository).setConnectionSignUp(facebookConnectionSignup);
        return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository,
                new FacebookSignInAdapter());
    }}

现在我想在登录后完成加载当前页面。 希望大家都能帮忙。 非常感谢你。

0 个答案:

没有答案