【问题标题】:Spring Security - Default Tables not createdSpring Security - 未创建默认表
【发布时间】:2015-05-13 15:03:08
【问题描述】:

我正在尝试在 Spring Boot 应用程序中将 Spring Social 集成到 Spring Security 之上。但似乎 Spring Security 在创建默认表时遇到了问题,例如UserConnection、UserProfile 等,因为在成功建立与 oauth2 提供程序的连接后出现这些 SQL 错误:

PreparedStatementCallback; bad SQL grammar [select userId from UserConnection where providerId = ? and providerUserId = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Tabelle "USERCONNECTION" nicht gefunden Table "USERCONNECTION" not found; SQL statement: select userId from UserConnection where providerId = ? and providerUserId = ? [42102-185]

这是 spring 提供的 JdbcUsersConnectionRepository 中的静态 SQL 调用。我试图切换到避免 SQL 问题的 InMemory 实现,但接下来发生了:

PreparedStatementCallback; bad SQL grammar [INSERT into userProfile(userId, email, firstName, lastName, name, username) values(?,?,?,?,?,?)]; nested exception is org.h2.jdbc.JdbcSQLException: Tabelle "USERPROFILE" nicht gefunden Table "USERPROFILE" not found; SQL statement: INSERT into userProfile(userId, email, firstName, lastName, name, username) values(?,?,?,?,?,?) [42102-185]

USERPROFILE 表也丢失了。

在我发布大量配置 sn-ps 之前,您是否已经知道一些我可能忘记的东西,它告诉 spring 为我创建这些表? :)

目前我正在使用 Spring Boot 标准 H2 内存数据库,它与 JpaRepositories 配合得很好。

谢谢! :)

【问题讨论】:

  • 我按照本教程 goo.gl/qAxitC 得到它的工作 - 但我在将这个 sql 转换为类实体时遇到了问题。 spring 要我创建所有表而不是这两个

标签: spring spring-security spring-social-google


【解决方案1】:

自己找到了解决方案 :)

我最终发现第一件事不是由一些花哨的自动 Spring 机制处理,而是在 src/main/resources 目录中使用一个简单的“schema.sql”。

create table UserConnection (
  userId varchar(255) not null,
  providerId varchar(255) not null,
  providerUserId varchar(255),
  rank int not null,
  displayName varchar(255),
  profileUrl varchar(512),
  imageUrl varchar(512),
  accessToken varchar(1024) not null,
  secret varchar(255),
  refreshToken varchar(255),
  expireTime bigint,
  primary key (userId, providerId, providerUserId));
create unique index UserConnectionRank on UserConnection(userId, providerId, rank);

create table UserProfile (
  userId varchar(255) not null,
  email varchar(255),
  firstName varchar(255),
  lastName varchar(255),
  name  varchar(255),
  username varchar(255),
  primary key (userId));
create unique index UserProfilePK on UserProfile(userId);

【讨论】:

  • 没错。 Spring Social 不会自动为您创建这些表。原因有很多,但主要原因是没有一种万能的方法来创建这些表。尽管我们可以为每个数据库供应商定义供应商特定的 SQL 并自动创建这些表,但这样做从来都不是优先事项。
  • 但问题是,当我尝试使用“@Entity”和“@Table”自己创建这些实体时,spring 会抱怨其他表(用户、users_roles、权限等)和我必须自己创建它们。是不是很奇怪?!而且这些表不是在神奇的 schema.sql 中创建的。它们是即时创建的吗?!为什么 spring 不抱怨这种方法?!
猜你喜欢
  • 2015-08-17
  • 2019-06-04
  • 2012-06-15
  • 2019-07-20
  • 2014-10-08
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多