【问题标题】:Grails, upgrading from Acegi to Spring security pluginGrails,从 Acegi 升级到 Spring 安全插件
【发布时间】:2011-09-06 06:53:37
【问题描述】:

昨天我开始将我们的 Grails 应用程序从 Acegi 插件 0.5.2 升级到 Spring 安全插件。我遇到了一些问题,也许有人可以在这里提供帮助?

进行必要的更改后(如 Burt Beckwith 在 Migrating from the Acegi Plugin 上记录的那样),我能够再次启动 Grails 应用程序(哇哦!)。但是,登录不再起作用。

我们的情况如下:我们使用我们的 Grails 应用程序 pure 作为其应用程序逻辑。身份验证是通过网络服务使用用户名和密码完成的。作为后端,我们使用应用程序 (dao) 的数据库和 LDAP 后端。目前,我们已禁用 LDAP,以简化测试。

这是进行身份验证的代码:

def authenticate(String username, String password) {
try {
      println "Trying authentication with user " + username + " and password " + password + "."
      def tempToken = new UsernamePasswordAuthenticationToken(username, password)
      println "Temptoken is " + tempToken
      def token = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password))
      println "Authentication token received was: " + token
    } catch (AuthenticationException authenticationException) {
        return false
    }
    return true     
}

这会打印到日志中:

Trying authentication with user admin and password admin.
Temptoken is org.springframework.security.providers.UsernamePasswordAuthenticationToken@1f: Principal: admin; Password: [PROTECTED]; Authenticated: false; Details: null; Not granted any authorities

然后一切都停止了。

我们使用的领域类相当简单。我们不使用诸如用户角色之类的类来连接人员及其权限。相反,我们使用多对多映射,因为这对我们一直有效且易于维护。

我们的权威域类:

class Authority {
static hasMany = [people: Person]

/** description */
String description
/** ROLE String */
String authority = ''

String authorityType

static constraints = {
    authority(help:'x',class:'wide',blank: false,unique:true)
    description(help:'x',class:'extrawide')
    authorityType(help:'x',class:'wide')
    people(help:'x',selectSort:'username',display:false)
}   

String toString() {
      return authority;
}   
}

还有我们的 Person 域类:

class Person  {

static hasMany = [authorities: Authority]
static belongsTo = Authority

//Authority primaryGroup

/** Username */
String username
/** User Real Name*/
String userRealName
String familyName
String givenName

/** MD5 Password */
String passwd
/** enabled */
boolean enabled

String email
boolean emailShow

/** description */
String description = ''



static constraints = {
    username(blank: false, unique: true,help:'x',class:'wide')
    userRealName(blank: false,help:'x',class:'wide')
    familyName(blank: false,help:'x',class:'wide')
    givenName(blank: false,help:'x',class:'wide')
    email(help:'x',class:'wide')
    emailShow(help:'x')
    enabled(help:'x')
    passwd(blank: false,password:true,show:false,help:'x',class:'wide')
    authorities(nullable:true,help:'x',sortable:true,selectSort:'authority')        
}

String toString() {
      return username;
  }
}

在 Config.Groovy 中,我们定义了:

security {
active = false
cacheUsers = false

grails.plugins.springsecurity.providerNames = ['daoAuthenticationProvider', 'anonymousAuthenticationProvider', 'rememberMeAuthenticationProvider']

grails.plugins.springsecurity.userLookUp.userDomainClassName = "Person"
grails.plugins.springsecurity.authority.className = "Authority"

就文档而言,这应该可以正常工作(对于“旧”Acegi 设置也是如此)。

为了收集更多信息,我短暂激活了 LDAP 并发现了同样的问题。 WireShark 告诉我在登录过程中没有进行任何 LDAP 调用。我的猜测是 Authenticate 函数中的代码有问题,或者 SpringSecurity 不知道如何获取我们的域类。

我很乐意阅读任何见解!

【问题讨论】:

  • 约束中的 help、class 和 selectSort 是什么?谢谢?

标签: security spring grails groovy spring-security


【解决方案1】:

似乎令牌无效,因此 authenticate() 调用失败并生成异常(IIRC 仅在调用成功时返回一个值)。

我的第一次尝试是检查数据库是否有一行用于用户:'admin',密码:'admin' 用于环境。

经过一番思考,我会仔细检查 Person 类中的密码属性。通过域类中的映射,或者更好地通过

userLookup.passwordPropertyName = "passwd"

更新:要检查的第三件事。

active = false

【讨论】:

  • 您好,我刚刚测试了您的建议,但没有效果。但是,我确实认为错误命名的密码字段是一个问题,但不是我的主要问题。用户名/密码正确,算法也正确(双重检查)。感谢您的帮助!
  • 谢谢,活动设置没有被选中,因为它没有以 grails.plugins.springsecurity 为前缀。 (自 Spring 安全插件以来的要求),但我将其更改为活动状态,甚至对其进行了测试。可悲的是,我要猎杀的不是幽灵……
  • 好的。您是否尝试在捕获 AuthenticationException 时打印一些日志以查看它正在发生的确切问题?
猜你喜欢
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2017-03-14
  • 2016-07-15
  • 2015-09-24
  • 1970-01-01
相关资源
最近更新 更多