Maybe you should check referer header filed and see if it can fit your needs: use it to redirect back after successful login process. Check this answer for using SimpleUrlAuthenticationSuccessHandler
@Bean
public AuthenticationSuccessHandler successHandler() {
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
handler.setUseReferer(true);
return handler;
}
Or if you are configuring more pieces in spring manually - you can use this answer
for getting referer url in filter phase and save it in session.
One modification of that answer could be: extends OAuth2ClientAuthenticationProcessingFilter and in doFilter get referer value
public class MyOAuth2ClientAuthenticationProcessingFilter extends OAuth2ClientAuthenticationProcessingFilter {
...
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String referrer = request.getHeader("Referer");
if (null != referrer)
request.getSession().setAttribute("url_prior_login", referrer);
super.doFilter(req, res, chain);
}
}
so you can redirect after procesing your '...handle-successful-authentication' - but as I see this redirect is overhead, try to put this logic somewhere else (eg. successHandler() or pricipalExtractor() in UserInfoTokenServices if you need more user details from social oauth provider)
successHandler() could look like this:
@Bean
public AuthenticationSuccessHandler successHandler() {
AuthenticationSuccessHandler rst = new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
...
HttpSession session = request.getSession();
String redirectUrl = null;
if (session != null) {
redirectUrl = (String) session
.getAttribute("url_prior_login");
if (null == redirectUrl || redirectUrl.trim().length() <= 0)
redirectUrl = "http://your_default_redirect_url";
response.sendRedirect(redirectUrl);
};
return rst;
}
Anyway, check spring docs