【发布时间】:2015-10-28 22:11:03
【问题描述】:
当启动我的应用程序时,Spring Security 正在我的 h2 数据库中创建两个表“用户”和“权限”。我的问题是,当我第二次启动应用程序时,数据库已经填充了这两个表,但是 Spring Security 直到想要创建它们。 我如何告诉 Spring Secutiry 仅在表不存在时才创建表?还是我应该改变其他东西来解决这个问题?
Spring Secutiry 正在创建我从博客中获得的两个表,这些表描述了我查看过的一些代码。它说:“关于数据模型的一些注释。没有真正的用户名和密码存储在 USERS 表中。该表由 Spring Security 定义用于一般用途,而不是特定于社交登录用例。”
我得到的错误消息,一些 java 堆栈跟踪,是:
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [org/springframework/security/core/userdetails/jdbc/users.ddl]:
create table users(username varchar_ignorecase(50) not null primary key,password varchar_ignorecase(500) not null,enabled boolean not null); nested exception is org.h2.jdbc.JdbcSQLException: Table "USERS" already exists;
SQL statement: create table users(username varchar_ignorecase(50) not null primary key,password varchar_ignorecase(500) not null,enabled boolean not null) [42101-175]
和
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [org/springframework/security/core/userdetails/jdbc/users.ddl]:
create table users(username varchar_ignorecase(50) not null primary key,password varchar_ignorecase(500) not null,enabled boolean not null); nested exception is org.h2.jdbc.JdbcSQLException: Table "USERS" already exists;
SQL statement: create table users(username varchar_ignorecase(50) not null primary key,password varchar_ignorecase(500) not null,enabled boolean not null) [42101-175]
我不确定这发生在代码中的什么位置。我有一个像这样的 entityManageFactory
@Autowired DataSource dataSource;
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
Properties props = new Properties();
props.put("hibernate.dialect", H2Dialect.class.getName());
props.put("hibernate.format_sql", "true");
props.put("spring.jpa.properties.hibernate.hbm2ddl.auto", "update");
props.put("spring.datasource.dbCreate", "update");
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("mypackage.demo");
factory.setDataSource(dataSource);
factory.setJpaProperties(props);
factory.afterPropertiesSet();
return factory.getObject();
}
【问题讨论】:
标签: spring spring-security h2