Spring Boot Security redirect after successful login - undefined
You can add a successHandler to redirect like this:
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
...
.formLogin()
.loginPage("/login")
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
redirectStrategy.sendRedirect(request, response, "/");
}
})
I had the same issue and this is a workaround that I used. First have a mapping for your root "/" that is unprotected
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public ModelAndView projectBase() {
return new ModelAndView("redirect:/home");
}
Have it redirect to where you want the user to go initially like home for example
@RequestMapping(value = { "/home" }, method = RequestMethod.GET)
public ModelAndView getHome() {
ModelAndView model = new ModelAndView("account/home");
model.addObject("user", userFacade.getJsonForUser(userFacade.getUserForClient()));
return model;
}
Make sure the root url is open in your security configuration like...
http.
authorizeRequests()
.antMatchers("/").permitAll()
What will happen is now it will hit the root /, and redirect to home which is restricted and send them to the loginpage with a return url of home. it will then write correctly as /home when they first login
For some reason spring security is not respecting the default success url, and it could be a configuration issue with your web server causing it. On my local machine I don't have this issue, but on some other machines I do. The workaround works in both places, since you always end up with a returnUrl.