Spring Load redirect to current page after successful login

I have modal login forms. After the user successfully logs in, the user is redirected to the / page. I am trying to find a way to stay on the contact page or another page after logging in. How to do it? 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("/"); } 
+11
spring spring-boot spring-security
source share
6 answers

You can 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() 
+17
source share

Just to provide an alternative solution:

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

defaultSuccessUrl is a shortcut for adding a custom SuccessHandler .

+10
source share

You can also do this 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"); } 
+4
source share

I had a strange problem that would force the login to redirect the user to localhost:8080/js/bootstrap.min.js

If someone else encounters an odd login redirect that seems to override .defaultSuccessUrl() , try adding this code below to SecurityConfig :

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

Add all your Resources/static folders to antMatchers()

+2
source share

Spring's path, namely the SavedRequestAwareAuthenticationSuccessHandler or SimpleUrlAuthenticationSuccessHandler may be a little awkward to implement. In the controller (for example, the one that processes the logins) you can make a header request yourself; eg:

HttpServletRequest request =null;

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

You will notice that you will have a URL before either manually (user-initiated) logging out or the timeout for the session (as processed by the Spring session): you will get https://iAmPriorUrl.com/... Then you can do everything with it what you want.

0
source share

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

you will look like this:

  .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll() .and() .requestCache() 
-one
source share

All Articles