繁体   English   中英

Spring Security Core Grails无法登录

[英]spring security core grails not logging in

安装Spring Security Core作为插件,然后快速入门

这是我的用户域类

package auth
class User {
   def springSecurityService
   String username
   String password
   boolean enabled
   boolean accountExpired
   boolean accountLocked
   boolean passwordExpired
   static mapping = {
          // password is a keyword in some sql dialects, so quote with backticks
          // password is stored as 44-char base64 hashed value
       password column: '`password`', length: 64
   }
   static constraints = {
       username blank: false, size: 1..50, unique: true
       password blank: false, size: 8..100
   }


   Set getAuthorities() {
      UserRole.findAllByUser(this).collect { it.role } as Set
   }

   def beforeInsert() {
     encodePassword()
   }

   def beforeUpdate() {
      if (isDirty('password')) {
        encodePassword()
      }
   }

   protected encodePassword() {
        password = springSecurityService.encodePassword(password, username)
   }
}

我的boostrap.groovy是

class BootStrap {

  def init = { servletContext ->

    auth.User james = new auth.User(username: 'test', enabled: true, password: 'password')
    james.save()
    if (james.hasErrors())
    {
        println("he has errors")
    }


    println("we made it! ")
}

   def destroy = {
   }
}

但是,当我登录时,它总是说:“抱歉,我们找不到具有该用户名和密码的用户。” 有什么想法吗?

这是因为您在编码密码时使用了

password = springSecurityService.encodePassword(password, username)

我不知道加盐,因此不能指导您太多。

但是,如果您在不加盐的情况下对密码进行编码,则代码可以正常工作, 只需在对密码进行编码时删除用户名 ,请尝试执行此操作

password = springSecurityService.encodePassword(password)

希望这可以帮助。

如果您在BootStrap.groovy创建用户,请尝试更改此设置:

def adminUser = User.findByUsername('admin') ?: new User(
    username: 'admin',
    password: springSecurityService.encodePassword('admin'),
    enabled: true).save(failOnError: true)

对此:

def adminUser = User.findByUsername('admin') ?: new User(
    username: 'admin',
    password: 'admin',
    enabled: true).save(failOnError: true)

问题是您使用了两次编码密码,一次在域中,一次在构造函数的参数中。

您可以验证用户是否实际上已引导到数据库中吗?

如果是这样,我遇到了类似的问题,Tomcat错误地缓存了一些数据。

这是我所做的:

  1. 停止的Tomcat
  2. 删除Tomcat的Temp目录中的所有文件
  3. 重新启动Tomcat

在那之后,它运行良好。

让我知道是否有帮助。

另外,自从我从头开始构建Grails网站以来已经有一段时间了,但是我想我还记得一些在线说明存在问题。 SpringSecurity可能正在为您编码密码,所以当您进行密码编码时,它会被双重编码

尝试删除编码密码的行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM