This may be an old question, but during my recent research on this topic, I found that the problem is common and still exists (especially in the case of modern AngularJS front-end applications with integrated security). I would like to share my decision with you.
On the login page, for example, /login.html, place the following code before the </body> :
<script type="text/javascript"> var hash = window.location.hash; document.cookie="hashPart=" + window.btoa(hash); </script>
Note (1): The btoa () function works in IE> = 10 ( http://www.w3schools.com/jsref/met_win_btoa.asp ), the equivalent of jQuery is used for older browsers.
Note (2): encryption of the # part of the URL is required, as it may contain special characters that cannot be stored in the cookie value string.
On the server side, you need to change the onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) method for a class that implements the AuthenticationSuccessHandler interface.
In my case, I just extend the SavedRequestAwareAuthenticationSuccessHandler class and override the onAuthenticationSuccess method using its source code. Then I get the hashPart cookie value from the request, decrypt it and add it to the allowed redirect URL. My code snippet is below:
@Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { // ... copy/paste original implementation here, until ... // Use the DefaultSavedRequest URL String targetUrl = savedRequest.getRedirectUrl(); for (Cookie cookie : req.getCookies()) { if (cookie.getName().equals("hashPart")) { targetUrl += new String(Base64Utils.decodeFromString(cookie.getValue())); cookie.setMaxAge(0); // clear cookie as no longer needed response.addCookie(cookie); break; } } getRedirectStrategy().sendRedirect(request, response, targetUrl); }
Finally, just enter your success handler class in the Spring security configuration, as described in: https://stackoverflow.com/a/330576/ ...
I look forward to your comments or other solutions to this problem.