17

I have login forms in modal windows. After successful login user is redirected to / page. I am trying to find a method to stay on contact page or another page after login. How to do this? My code is:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/css/**","/js/**","/fonts/**","/images/**","/home","/","/kontakt").permitAll()
            .antMatchers("/userlist").hasRole("ADMIN")
            .anyRequest().authenticated();
    http
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/");
}
dur
  • 15,689
  • 25
  • 79
  • 125
set4812
  • 111
  • 1
  • 1
  • 10

8 Answers8

22

You could use custom AuthenticationSuccessHandler and set useReferer to true.

@Bean
public AuthenticationSuccessHandler successHandler() {
    SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
    handler.setUseReferer(true);
    return handler;
}

And in your configure method:

http
    .formLogin()
        .loginPage("/login")
        .successHandler(successHandler())
        .permitAll()
        .and()
Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109
9

Just to provide an alternative solution:

formLogin()
    .loginPage("/login")
    .defaultSuccessUrl("/")

defaultSuccessUrl is a shortcut to adding the custom SuccessHandler.

dur
  • 15,689
  • 25
  • 79
  • 125
Jonas
  • 165
  • 2
  • 3
  • 2
    Your answer is not matching the current version of the question. And your answer is flawed, `defaultSuccessUrl` is a shortcut for `SavedRequestAwareAuthenticationSuccessHandler` not for a custom `SuccessHandler`. – dur Jan 14 '17 at 19:28
7

I had a weird issue that would cause on login to redirect the user to localhost:8080/js/bootstrap.min.js

If anyone else is experiencing an odd redirection on login, which seems to override the .defaultSuccessUrl(), then try adding this code below in SecurityConfig:

@Override
public void configure(WebSecurity security){
    security.ignoring().antMatchers("/css/**","/images/**","/js/**");
}

Add all your Resources/static folders to the antMatchers()

benscabbia
  • 17,592
  • 13
  • 51
  • 62
5

You can as well do it in your AuthenticationSuccessHandler implementation:

@Override
public void onAuthenticationSuccess(HttpServletRequest request, 
HttpServletResponse response, Authentication authentication) throws 
IOException, ServletException 
{
    //Do your logic;
    response.sendRedirect(request.getHeader("referer");
}
Augustin Ghauratto
  • 1,420
  • 1
  • 19
  • 21
4

Config is same as the accepted answer, only luck I had was with extending SavedRequestAwareAuthenticationSuccessHandler.

public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        System.out.println("HEP HEP");
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
  • 1
    This is the only working solution for me, thanks, really. The key was to use ``SavedRequestAwareAuthenticationSuccessHandler ``. – meniluca Jun 16 '20 at 14:06
2

I had absolutely the same issue with inadequate redirect after adding bootstrap to my project tree.

Method .defaultSuccessUrl with flag = true saved me time and lines of code.

.formLogin()
   .loginPage("/login")
   .defaultSuccessUrl("/", true)
   .permitAll()
0

The Spring route, ala extending SavedRequestAwareAuthenticationSuccessHandler or SimpleUrlAuthenticationSuccessHandler can be a bit clunky to implement. In the controller (ex. one that processes logins), you can do the header request yourself; ex:

HttpServletRequest request =null;

String priorUrl = request.getHeader("Referer");

You will notice that you will have the URL prior to either a manual (initiated by user) logout or a session timeout (as handled by Spring session): you'll get an https://iAmPriorUrl.com/.... Then you can do whatever you want with it.

Isaac Riley
  • 290
  • 4
  • 5
-1

@Jonas All you need to do is add .requestCache() at the end

you config will look like this

         .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll()
            .and()
            .requestCache()