How to set redirect url after success login using Social Providers

混江龙づ霸主 提交于 2019-11-30 05:27:03

问题


I need change the redirect url when my user is succefull logged in using some of Spring Social Providers, like Twitter in this case.

I'm getting in every set***Url("") a null pointer exception Some times setting this don't work too

I tried so far setting:

public ProviderSignInController signInController(ConnectionFactoryLocator connectionFactoryLocator,
                                                     UsersConnectionRepository usersConnectionRepository) {
        ProviderSignInController providerSignInController = new ProviderSignInController(connectionFactoryLocator,
                usersConnectionRepository,
                new CSignInAdapter(requestCache()));
        providerSignInController.setPostSignInUrl("/home");
        providerSignInController.setApplicationUrl("localhost:8080/home");
        return  providerSignInController;
    }

I tried each one of setPostSignInUrl and setApplicationUrl, separately.

Also tried:

@Bean
    public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator,
                                               ConnectionRepository connectionRepository) {
        ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository);
        connectController.addInterceptor(new TweetAfterConnectInterceptor());
        connectController.setApplicationUrl("/home");
        return connectController;
    }

I'm using Spring Social showcase with Security as base to do this. In case of need I'm posting the HttpSecurity configuration:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/signin")
                .loginProcessingUrl("/signin/authenticate")
                .failureUrl("/signin?param.error=bad_credentials")
                .defaultSuccessUrl("/home")
                .and()
                .csrf()
                .and()
                .logout()
                .logoutUrl("/signout")
                .deleteCookies("JSESSIONID")
                .and()
                .authorizeRequests()
                .antMatchers("/admin/**", "/favicon.ico", "/resources/**", "/auth/**", "/signin/**", "/signup/**",
                        "/disconnect/facebook").permitAll()
                .antMatchers("/**").authenticated()
                .and()
                .rememberMe()
                .and()
                .apply(new SpringSocialConfigurer());
    }

回答1:


Try this:

private SpringSocialConfigurer getSpringSocialConfigurer() {
        SpringSocialConfigurer config = new SpringSocialConfigurer();
        config.alwaysUsePostLoginUrl(true);
        config.postLoginUrl("/home");

        return config;
}

Then change your configure method:

.apply(getSpringSocialConfigurer());



回答2:


For Spring Social, you can configure the post login URL to a default URL, such as "/home".

But under certain circumstances, you would like to direct the user to a different URL. In order to dynamically change the redirect URL after successful login, you can simply return a String representing any URL you desire in the signIn method of your SignInAdapter implementation class:

import org.springframework.social.connect.web.SignInAdapter;

public class SocialSignInAdapter implements SignInAdapter {

    public String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {
        boolean flag = true;
        if (flag) {        
            return "/a_different_url";
        }
        return null; // Default, which means using the default post login URL
    }
}

I verified this using Spring Social version 1.1.0.RELEASE



来源:https://stackoverflow.com/questions/24439777/how-to-set-redirect-url-after-success-login-using-social-providers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!