【问题标题】:Spring Security - Grails 3.2.2 - ERR_TOO_MANY_REDIRECTSSpring Security - Grails 3.2.2 - ERR_TOO_MANY_REDIRECTS
【发布时间】:2017-10-04 08:05:31
【问题描述】:

我的application.yml上有这个:

grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.successHandler.defaultTargetUrl = '/l/loggedIn'

l/loggedIn 看起来像:

def loggedIn() {
    User user = springSecurityService.currentUser

    def roleDefault = Role.findByAuthority("ROLE_DEFAULT")
    if(user.authorities.contains(roleDefault))
        redirect(controller: 'foo', action:'index')

    def roleAdmin = Role.findByAuthority("ROLE_ADMIN")
    if(user.authorities.contains(roleAdmin))
        redirect(view: 'index')

}

当我在我的应用程序中以admin 登录时,我收到ERR_TOO_MANY_REDIRECTS 错误。

有什么办法解决这个问题?


更新

@Secured(["ROLE_ADMIN", "ROLE_DEFAULT"])
class FooController {

    def index() {}
}

【问题讨论】:

  • 它只发生在管理员用户@AdeelAnsari

标签: redirect grails spring-security controller


【解决方案1】:

redirect 不考虑 view,render 考虑。

允许的选项是 action、controller、uri、url、params、fragment 和/或域实例。

所以,你可以这样做,

def loggedIn() {
    User user = springSecurityService.currentUser

    def roleDefault = Role.findByAuthority("ROLE_DEFAULT")
    if(user.authorities.contains(roleDefault))
        redirect(controller: 'foo', action:'index')

    def roleAdmin = Role.findByAuthority("ROLE_ADMIN")
    if(user.authorities.contains(roleAdmin))
        redirect(controller: 'foo', action:'adminIndex')
}

然后在您的控制器中定义actions,并提供适当的视图。

@Secured(["ROLE_ADMIN", "ROLE_DEFAULT"])
class FooController {

    def index() {}

    def adminIndex() {}

}

阅读docs。

【讨论】:

  • 使用redirect(uri: "/") 解决了这个问题,谢谢。
  • 很高兴它有帮助。
猜你喜欢
  • 2014-09-17
  • 1970-01-01
  • 2013-05-23
  • 2013-04-19
  • 2019-05-09
  • 2012-02-19
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多