Home configuring the refer url redirect in spring security
Reply: 0

configuring the refer url redirect in spring security

mmaceachran
1#
mmaceachran Published in 2018-01-10 19:18:02Z

I am using Spring Security, and I am trying to redirect to a page after login. If i am logged out, and go to /myurl/home, I am correctly redirected to the login page. but when I login, I am redirected to a default URL, not /myurl/home.

I have created a subclass of SavedRequestAwareAuthenticationSuccessHandler, and what I am trying to do is if there is a refer in the header, go there, otherwise go to a default url that I can set. here is the code:

public class MySuccessRedirectHandler extends SavedRequestAwareAuthenticationSuccessHandler {

static final Logger logger = LoggerFactory.getLogger(MySuccessRedirectHandler.class);

private String defaultRedirectUrl;

public MySuccessRedirectHandler(String defaultRedirectUrl) {
    this.defaultRedirectUrl = defaultRedirectUrl;
    this.setUseReferer(true);
}

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
    String  url = (String)request.getSession().getAttribute("referer");
    logger.debug("Referer URL from session:"+url);
    if (url == null) {
        response.sendRedirect(defaultRedirectUrl);
    } else {
        response.sendRedirect(url); 
    }
}
}

and my config looks like this:

        http.authorizeRequests().anyRequest()
         // now start login info
        .authenticated().and().csrf().disable().formLogin()
        .successHandler(new MySuccessRedirectHandler("/home"))
        .loginProcessingUrl("/j_spring_security_check")
        .usernameParameter("j_username")
        .passwordParameter("j_password")
        .loginPage("/login").failureUrl("/login?error=true").and()
        // now start logoff info
        .logout()
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .logoutSuccessUrl("/login").and().exceptionHandling();

However, each time I log in, I get a null for the refer. How do I get the refer URL so I can get this to work?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300727 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO