I've got a Grails app that uses Spring Security Core with for authentication and authorization. The app's primary purpose is to provide a rest api that is secured with a token passed in via the Authorization header in each request. This token is generated outside of the grails app and there is no official "login" phase - each request is authenticated and authorized individually.
I've implemented my own authentication filter and provider which seem to work as far as token authentication goes. The problem is that for successful authentications the spring security core always redirects to a login success page (configured via successHandler.defaultTargetUrl) when I just want to silently continue with the original request like this:
- User requests /rest/foo
- Custom spring security filter matches the request and handles it. If the authorization header is good then /rest/foo is invoked (currently redirected to defaultTargetUrl), otherwise the user is forwarded to /error. (this last part works)
Looking at the source of AjaxAwareAuthenticationSuccessHandler which seems to be the culprit and I see that I can set the targetUrl by adding targetUrlParameter (defaults to 'spring-security-redirect) as a request param but this seems both ugly and needless. Is there any way to make this work as desired? I've considered implementing my own SuccessHandler... is that the correct solution? Do I have any other options?
EDIT: I gave the custom success handler a try by extending AjaxAwareAuthenticationSuccessHandler and overriding determineTargetUrl(request, response) and returning request.getServletPath(). This gets me past the initial problem but creates an infinite redirect loop, presumably due to a combination of me needing stateless authentication and DefaultRedirectStrategy redirecting to the destination instead of forwarding.
EDIT #2: Success! Using the code in this SOF thread I was able to get the custom AuthenticationSuccessHandler solution to work. For Grails users you'll also need to update resources.groovy like this:
authenticationSuccessHandler(com.foo.bar.YourSuccessHandler){}