首页 > 解决方案 > Grails - Spring Security 不适用于 Mysql 8

问题描述

我有一个使用这些技术的应用程序:

问题是当我尝试将应用程序与 Mysql 8(5.6+ 工作正常)连接时,我无法从 Grails-Spring-Security 插件获取与用户相关的信息。该应用程序正在运行,甚至连接到数据库,但无法验证或获取用户的信息,例如 findByUsername,其中用户名是我的用户域类中的属性。

我在 application.properties 文件中定义了用户域类。

grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.aaa.User'

在某些时候,我发现了这个错误,但不确定它是否与此有关。

java.lang.IllegalStateException: 
Either class [com.aaa.User] is not a domain class or GORM has not been initialized correctly or has already been shutdown. 
Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

想了解为什么它无法从数据库中获取信息。我尝试了很多事情,比如将 GORM 版本更改为 6.1.7 和 grails spring-secuirty-core 插件版本,但无法获得任何东西。

任何帮助,将不胜感激。

谢谢,

阿图尔

标签: javamysqlspringgrails

解决方案


我们能够解决问题。当 grails-hibernate 插件查找静态 api 中是否存在的属性(在我的情况下是它的用户名,因为我正在调用 API findByUsername)时,就会发生这种情况。如果它不存在,则会引发异常。

对我有用的是,我必须将该属性放在用户域类的静态映射中:

static mapping = {
table 'userTable'
id column: 'id'
password column: 'userpassword'
username column: 'username'
}

我不确定为什么会这样,当我使用 Mysql5.6+ 运行应用程序时,它的工作原理。(驱动程序根据它)但是当使用 Mysql8 时,它会在静态映射中查找属性。

One more point I would like to mention to fix the issue is, make sure you have tablename, columnname same as defined in DB. Case sensitivity is what expected.

As I mentioned the case sensitivity, if you are using linux with Mysql8, the lower_case_table_names=0, this check with the following as per Mysql official documentation:

Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are case sensitive. You should not set this variable to 0 if you are running MySQL on a system that has case-insensitive file names (such as Windows or macOS). If you force this variable to 0 with --lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption may result.

So if the table name is static is in camel or upper case and in db it is lower case it wont match. and the error occurred.

Hope this helps to someone.


推荐阅读