【发布时间】:2014-08-02 07:21:13
【问题描述】:
我有一个使用 Spring-Security spring-security-web:4.0.0.M1 的 spring-boot 1.1.0.BUILD-SNAPSHOT 项目。我想在我的 H2 表中植入数据库表,但在启动时出现异常(包括生产和集成测试代码)。
这是我相关的 schema.sql 文件内容:
CREATE TABLE IF NOT EXISTS Users (
username VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY,
password VARCHAR_IGNORECASE(500) NOT NULL,
enabled BOOLEAN NOT NULL
);
CREATE TABLE IF NOT EXISTS Authorities (
username VARCHAR_IGNORECASE(50) NOT NULL,
authority VARCHAR_IGNORECASE(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username) REFERENCES users (username)
);
CREATE UNIQUE INDEX IF NOT EXISTS ix_auth_username ON authorities (username, authority);
INSERT INTO users(username,password,enabled) VALUES ('admin','$2a$10$GVPAMYRozI08Mcll5too6.Q4M2jTO0iJVoiaVDv9pMxRqTpxNj9vO', TRUE);
INSERT INTO Authorities (username, authority) VALUES ('admin', 'ADMIN');
INSERT INTO users(username,password,enabled) VALUES ('user','$2a$10$GVPAMYRozI08Mcll5too6.Q4M2jTO0iJVoiaVDv9pMxRqTpxNj9vO', TRUE);
INSERT INTO Authorities (username, authority) VALUES ('user', 'USER');
我的 application.properties 文件中有以下内容:
spring.jpa.database=H2
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
当我启动或运行测试时,我收到以下错误:
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource URL [file:/Users/David/projects/cnet/OFAC/out/test/OFAC/data.sql]: INSERT INTO users (username, password, enabled) VALUES ('admin', '$2a$10$GVPAMYRozI08Mcll5too6.Q4M2jTO0iJVoiaVDv9pMxRqTpxNj9vO', TRUE); nested exception is org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_4 ON PUBLIC.USERS(USERNAME)"; SQL statement:
INSERT INTO users (username, password, enabled) VALUES ('admin', '$2a$10$GVPAMYRozI08Mcll5too6.Q4M2jTO0iJVoiaVDv9pMxRqTpxNj9vO', TRUE) [23505-172]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:474)
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:208)
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:135)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runDataScripts(DataSourceInitializer.java:95)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.onApplicationEvent(DataSourceInitializer.java:88)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.onApplicationEvent(DataSourceInitializer.java:46)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:78)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initialize(DataSourceInitializer.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
用这些用户帐户播种安全数据库表的最佳方法是什么?我尝试了代码,但无法让加载程序以正确的顺序工作。
更新: 当我通过 ResourceDatabasePopulator.populate 时,我看到我有两个正在使用的 schema.sql 实例。他们是:
file:.../out/test/OFAC/schema.sql
file:.../out/production/OFAC/schema.sql
我也有:
file:.../out/test/OFAC/data.sql
file:.../out/production/OFAC/data.sql
显然没有重复 schema.sql 的危害。但是在 ScriptUtils.executeSqlScript() 中,我看到两个 data.sql 文件也被调用了。所以,我不明白为什么要执行测试和生产 sql 文件。
【问题讨论】:
-
看起来您的构建工具正在创建重复的类路径?我想你需要拨回一点,然后尝试一个非常简单的构建配置。
标签: spring-security spring-data spring-boot