Grails and Spring Security Plugin: Role Based User Login Redirection

I want to redirect the user after a successful login based on his roles. I follow this example , but the method never executes (printing fails).

My .groovy resources:

beans = {
authenticationSuccessHandler(UrlRedirectEventListener)
{
    def conf = SpringSecurityUtils.securityConfig      
    requestCache = ref('requestCache')
    defaultTargetUrl = conf.successHandler.defaultTargetUrl
    alwaysUseDefaultTargetUrl = conf.successHandler.alwaysUseDefault
    targetUrlParameter = conf.successHandler.targetUrlParameter
    useReferer = conf.successHandler.useReferer
    redirectStrategy = ref('redirectStrategy')
    defaultUrl= "/"
    adminUrl = "/admin/"
    userUrl = "/user/"
}
}

And my UrlRedirectEventListener

class UrlRedirectEventListener 
extends SavedRequestAwareAuthenticationSuccessHandler 
{

@Override
protected String determineTargetUrl(HttpServletRequest request,
                                        HttpServletResponse response) {

    boolean hasBoth = SpringSecurityUtils.ifAllGranted("ROLE_USER,ROLE_ADMIN");
    boolean hasUser = SpringSecurityUtils.ifAnyGranted("ROLE_USER");
    boolean hasAdmin = SpringSecurityUtils.ifAnyGranted("ROLE_ADMIN");

    println("hasBoth:"+hasBoth+ " hasUser:"+hasUser+ " hasAdmin:"+hasAdmin)
    if( hasBoth ){
        return defaultLoginUrl;
    }else if ( hasUser ){
        return userUrl ;
    }else if ( hasAdmin ){
        return adminUrl ;
    }else{
        return super.determineTargetUrl(request, response);
    }
}

private String defaultLoginUrl;
private String mmrLoginUrl;
private String pubApiLoginUrl;

...setters for urls
} 

What am I missing? I am using Grails 2.0.4 and Spring Security Plugin 2 RC2.0

Thank!

Edit:

I also have this controller to check that my bean is created, that it displays correctly.

class TestController {

   AuthenticationSuccessHandler authenticationSuccessHandler

   def index()
   {
      render( authenticationSuccessHandler  )
   }
}
+4
source share
1 answer

, . , SavedRequestAwareAuthenticationSuccessHandler onAuthenticationSuccess determineTargetUrl , ( ). onAuthenticationSuccess .

, :

SavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler

UrlRedirectEventListener, SavedRequestAwareAuthenticationSuccessHandler:

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest == null) {
        super.onAuthenticationSuccess(request, response, authentication);
        return;
    }
    String targetUrlParameter = getTargetUrlParameter();
    if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
        requestCache.removeRequest(request, response);
        super.onAuthenticationSuccess(request, response, authentication);
        return;
    }
    def targetUrl = savedRequest.getRedirectUrl();
    if( targetUrl != null && targetUrl != "" && targetUrl.endsWith("/myApp/") ) // I only want to differently handle "/". If a user hits /user/, or any other url, attempt to send him there. Spring Security will deny him access based on his privileges if necessary.
        targetUrl = this.determineTargetUrl( request, response ) // This is the method defined above. Only thing changed there is I removed the `super` call

    clearAuthenticationAttributes(request);
    logger.info("Redirecting to Url: " + targetUrl);
    getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
+4

All Articles