Jasypt加密字段和Criteria API无法获得结果(Grails)

时间:2016-05-21 21:38:40

标签: grails encryption

我刚开始玩Grails并开始使用简单的注册/登录功能。 我有这个简单的域对象:

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [repository.PostRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
        ... 45 common frames omitted

我正在保存Person对象。但是当我尝试使用Criteria API检索它以进行登录时,它却找不到它:

class Person {

    String firstName, lastName, email
    String login, password

    static constraints = {
        firstName nullable: true
        lastName nullable: true
    }
    static mapping = {
        password type: GormEncryptedStringType
    }
}

如果我删除def criteria = Person.createCriteria() def person = criteria.get { and { eq("login", params.login) eq("password", params.password) } } 或将其更改为未加密的内容,则可以正常使用。任何人都知道如何处理它?<​​/ p>

1 个答案:

答案 0 :(得分:0)

你做错了(安全)。你没有加密密码,你可以哈希。散列是单向且有损的,而加密是双向无损的,但如果你可以解密密码,那么你很容易受到攻击者的攻击,这些攻击者可以访问解密密钥,然后他们就可以了解密它们。

如果您使用散列,那么您可以(但当然不应该)让任何人查看散列密码,因为如果您使用良好的散列方案它们将无用。 Bcrypt是最好的之一,易于使用。

您应该对提供的密码进行哈希处理,并根据存储的哈希密码对其进行验证,而不是解密存储的加密密码以验证用户身份验证尝试的明文密码。对于某些算法,这很简单,因为检查值是否相同,但Bcrypt的情况并非如此,因为算法总是为每次调用生成不同的散列。但该算法可以验证两个散列密码,因此这不是问题。

因此,对您的问题的真正解决方法是停止使用加密,并停止自己推出自己的问题。安全。 spring-security-core是一种受欢迎的选择。