Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set redirect url after success login using Social Providers

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());
    }
like image 655
Joao Evangelista Avatar asked Jun 26 '14 20:06

Joao Evangelista


People also ask

How do I redirect a URL after login?

The most common ways to implement redirection logic after login are: using HTTP Referer header. saving the original request in the session. appending original URL to the redirected login URL.

How do I redirect Okta login?

Sign in a userOpen a browser and navigate to http://localhost:8080 . Click the login link and you are redirected to Okta to sign in. When you return, it should display your user information.

What is redirect URL OIDC?

Your redirect URI(s) defines where the user lands after successful login or registration. Each redirect URI must be added to your OIDC login client configuration, otherwise an error will be thrown upon user authentication.

What is redirect URI in Auth0?

During a user's authentication, the redirect_uri request parameter is used as a callback URL. This is where your application receives and processes the response from Auth0, and is often the URL to which users are redirected once the authentication is complete.


2 Answers

Try this:

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

        return config;
}

Then change your configure method:

.apply(getSpringSocialConfigurer());
like image 55
Igor H Avatar answered Oct 22 '22 13:10

Igor H


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

like image 20
Yuci Avatar answered Oct 22 '22 11:10

Yuci