简体   繁体   English

Spring Security Core Grails无法登录

[英]spring security core grails not logging in

Installed Spring Security Core as plugin then did quickstart 安装Spring Security Core作为插件,然后快速入门

Here is my User domain class 这是我的用户域类

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)
   }
}

And my boostrap.groovy is 我的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 = {
   }
}

But when I go to login, it keeps saying "Sorry, we were not able to find a user with that username and password." 但是,当我登录时,它总是说:“抱歉,我们找不到具有该用户名和密码的用户。” Any thoughts? 有什么想法吗?

This is because you are using the salt while encoding the password. 这是因为您在编码密码时使用了

password = springSecurityService.encodePassword(password, username)

I have no idea of salting and hence can not guide you to much. 我不知道加盐,因此不能指导您太多。

But if you encode your password without salting then your code works, just remove username when encoding the password , try this 但是,如果您在不加盐的情况下对密码进行编码,则代码可以正常工作, 只需在对密码进行编码时删除用户名 ,请尝试执行此操作

password = springSecurityService.encodePassword(password)

Hope this helps. 希望这可以帮助。

If you create the user in BootStrap.groovy , try changing this: 如果您在BootStrap.groovy创建用户,请尝试更改此设置:

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

to this: 对此:

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

The problem is that you are using the encoding password twice, once in the Domain and once in the constructor's parameters. 问题是您使用了两次编码密码,一次在域中,一次在构造函数的参数中。

Can you validate that the user is actually bootstrapped into the database? 您可以验证用户是否实际上已引导到数据库中吗?

If so, I ran into a similar issue with Tomcat caching some data incorrectly. 如果是这样,我遇到了类似的问题,Tomcat错误地缓存了一些数据。

Here is what I did: 这是我所做的:

  1. Stopped Tomcat 停止的Tomcat
  2. Deleted all the files in Tomcat's Temp directory 删除Tomcat的Temp目录中的所有文件
  3. Restarted Tomcat 重新启动Tomcat

After that, it worked fine. 在那之后,它运行良好。

Let me know if this helps. 让我知道是否有帮助。

Also, its been a while since I've built a Grails site from scratch, but I think I remember there being an issue with some online instructions. 另外,自从我从头开始构建Grails网站以来已经有一段时间了,但是我想我还记得一些在线说明存在问题。 SpringSecurity might be encoding the password for you, so when you do it, it is getting double encoded . SpringSecurity可能正在为您编码密码,所以当您进行密码编码时,它会被双重编码

Try removing the lines that encode the password. 尝试删除编码密码的行。

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

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