Grails 和 Spring 安全插件 : Redirecting user upon login based on roles

标签 grails groovy spring-security

我想根据用户的角色在成功登录后重定向用户。我正在关注 this example但该方法永远不会执行(打印不会发生)。

我的资源.groovy:

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/"
}
}

还有我的 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
} 

我错过了什么?
我正在使用 Grails 2.0.4 和 Spring Security Plugin 2 RC2.0

谢谢!

编辑:

我也有这个 Controller 来测试我的 bean 是否已创建,它显示正确。
class TestController {

   AuthenticationSuccessHandler authenticationSuccessHandler

   def index()
   {
      render( authenticationSuccessHandler  )
   }
}

最佳答案

看了源码后,我明白了。问题是 SavedRequestAwareAuthenticationSuccessHandler的方法 onAuthenticationSuccess永远不会到达覆盖的方法 determineTargetUrl使用我使用的简单登录方案(插件生成的默认值)。我实际上必须覆盖 onAuthenticationSuccess而是把我的逻辑放在那里。

有问题的来源:

SavedRequestAwareAuthenticationSuccessHandler 扩展 SimpleUrlAuthenticationSuccessHandler扩展 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);
}

关于Grails 和 Spring 安全插件 : Redirecting user upon login based on roles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006300/

相关文章:

grails - grails从 Controller 返回xml字符串

grails - Grails-创建只读API

groovy - 如何从 groovy 列表中删除重复值

groovy - 是否可以创建一个无需编译即可在脚本中使用的 Groovy 包?

spring-security - 使用 CSRF 进行放心和基本身份验证的集成测试

spring-security - 具有数据库和多个角色的 Spring 安全性?

spring - 如何让 SpringSecurity/Grails 与终止 SSL 的负载均衡器很好地配合

csv - 带有大表的 Grails 应用程序

unit-testing - Groovy 测试 groovy.sql.Sql

groovy - groovy 中的方法重载