security - Grails,从 Acegi 升级到 Spring 安全插件

标签 security spring grails groovy spring-security

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

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

我们的情况如下:我们使用纯粹的 Grails 应用程序作为其应用程序逻辑。身份验证是通过 Web 服务使用用户名和密码完成的。作为后端,我们使用应用程序的数据库 (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 不知道如何获取我们的域类。

我很高兴阅读任何见解!

最佳答案

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

我的第一次尝试是检查数据库中是否有针对环境的用户:“admin”、密码:“admin”的行。

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

userLookup.passwordPropertyName = "passwd"

更新:第三件事要检查。

active = false

关于security - Grails,从 Acegi 升级到 Spring 安全插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6201492/

相关文章:

python - 如何安全地运行另一个Python文件?

java - Spring data mongodb - 需要 'cursor' 选项

grails - grails 中的通用方法拦截(特别是 Controller )

Grails 资源 : How to properly create environment-aware resources?

java - 将空字符串设置为属性文件中键的值

php - 散列或使用 SSL 将用户名和密码发送到外部站点

ruby-on-rails - 如何使用 Rails 保护数据库中的用户数据?

java - Spring Boot 不从 thymeleaf 返回值

java - Spring beans - 为什么我的私有(private)字段是静态的?

grails - 一对一关系中的Grails域类映射