Spring 安全创建了一个名为 table 的 USERS
Spring Security creates a table named USERS
启动我的应用程序时 Spring 安全性正在我的 h2 数据库中创建两个表 "users" 和 "authorities"。我的问题是,当我第二次启动应用程序时,数据库中已经填充了这两个表,但是 Spring Security till 想要创建它们。
我如何告诉 Spring Secutiry 仅在表不存在时才创建表?或者我应该改变其他东西来解决这个问题?
Spring Secutiry 正在创建我从博客中获得的两个表,这些表描述了我看过的一些代码。它说:"Some notes on the data model. No real username nor password is stored in the USERS-table. The table is defined by Spring Security for general purpose usage and not specific for social login use-cases."
我得到的错误消息,一些 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();
}
USERS 表(和其他一些表)是在配置 Spring 安全性时创建的。这是该代码的一部分:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.
jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema();
}
如果我从一个已经存在的数据库开始,该数据库已配置并在其中部署了数据,我会删除该行:
.withDefaultSchema();
和Spring安全将使用数据库中的现有表。
我是这样处理的:
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer =
auth.jdbcAuthentication().dataSource(dataSource);
// Check if the auth schema has been populated (from a persistent source); if not, set it to populate
if (!dataSource.getConnection().getMetaData().getTables(null, "", "USERS", null).first()) {
configurer = configurer.withDefaultSchema();
}
启动我的应用程序时 Spring 安全性正在我的 h2 数据库中创建两个表 "users" 和 "authorities"。我的问题是,当我第二次启动应用程序时,数据库中已经填充了这两个表,但是 Spring Security till 想要创建它们。 我如何告诉 Spring Secutiry 仅在表不存在时才创建表?或者我应该改变其他东西来解决这个问题?
Spring Secutiry 正在创建我从博客中获得的两个表,这些表描述了我看过的一些代码。它说:"Some notes on the data model. No real username nor password is stored in the USERS-table. The table is defined by Spring Security for general purpose usage and not specific for social login use-cases."
我得到的错误消息,一些 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();
}
USERS 表(和其他一些表)是在配置 Spring 安全性时创建的。这是该代码的一部分:
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.
jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema();
}
如果我从一个已经存在的数据库开始,该数据库已配置并在其中部署了数据,我会删除该行:
.withDefaultSchema();
和Spring安全将使用数据库中的现有表。
我是这样处理的:
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer =
auth.jdbcAuthentication().dataSource(dataSource);
// Check if the auth schema has been populated (from a persistent source); if not, set it to populate
if (!dataSource.getConnection().getMetaData().getTables(null, "", "USERS", null).first()) {
configurer = configurer.withDefaultSchema();
}