【问题标题】:Extending AjaxAwareAuthenticationSuccessHandler, Remembering destination URL扩展 AjaxAwareAuthenticationSuccessHandler,记住目标 URL
【发布时间】:2011-10-23 19:40:24
【问题描述】:

Grails 1.3.7 Spring-Security-Core 1.1.2

我已经实现了一个扩展 AjaxAwareAuthenticationSuccessHandler 的自定义类,以便在登录后可以将特定角色带到特定的 URL,效果很好。但是,如果会话过期,我需要能够在会话过期时将用户带到请求的 URL,覆盖基于角色的 URL。

这是我的代码的简化版本

class MyAuthSuccessHandler extends AjaxAwareAuthenticationSuccessHandler {

  @Override
  public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response,
                                      final Authentication authentication) throws ServletException, IOException {

    def goAdmin = false
    authentication.authorities.each { ga ->
      if (ga.authority.equals('ROLE_ADMIN')) {
        goAdmin = true
      }
    }

    if (goAdmin) {
      response.sendRedirect(request.contextPath + '/admin/index')
    }else{
      super.onAuthenticationSuccess(request, response, authentication)
    }
  }
}

我尝试添加一个对 determineTargetUrl(request, response) 的调用,但它总是返回“/”,即使我请求了一个受保护的 /admin/foo 之类的资源。

谢谢。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    请求

    super.determineTargetUrl(request, response);
    

    如果您使用SavedRequestAwareAuthenticationSuccessHandler 作为超类,应该可以。我不确定您是否可以在您的场景中切换到此类。也许这会有所帮助,但我想你已经充分意识到了这一点:http://omarello.com/2011/09/grails-custom-target-urls-after-login/

    【讨论】:

    • 然而,事实并非如此。 SavedRequestAwareAuthenticationSuccessHandler 是 AjaxAware 的超类......该方法实际上隐藏在抽象超类中,无论如何它大约是链上的 4 级。因此,我扩展哪个类并不重要,只要它是其中之一即可。
    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 2011-06-16
    • 2015-11-07
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多