Abhay
Abhay

Reputation: 347

Redirect to previous page Spring Security

I have a web app which has a log-in option on separate pages. However when I log in, I get sent back to the home page instead of going to back to the page I was on. I am using the default login/log-out pages provided by Spring Security.

Here's my HttpSecurity configuration:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
    .antMatchers("/api/*").hasRole("Admin")
    .antMatchers("/manager").hasAnyRole("Admin", "Manager")
    .antMatchers("/sales").hasAnyRole("Admin", "Manager", "Sales")
    .antMatchers("/checkout").authenticated()
    .and().formLogin().permitAll()
    .and().logout().permitAll()
    .logoutSuccessUrl("/");
}

Upvotes: 1

Views: 550

Answers (1)

dos4dev
dos4dev

Reputation: 533

Main idea how to implement requirements explain in this article, which include examples source of code. You need only change this method

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final 

Authentication authentication) throws IOException {

    if (response.isCommitted()) {
        logger.debug("Response has already been committed. Unable to redirect to " + request.getRequestURI());
        return;
    }
    redirectStrategy.sendRedirect(request, response, request.getRequestURI());
}`

in MySimpleUrlAuthenticationSuccessHandler class

Upvotes: 1

Related Questions