Spring Security创建了一个名为USERS的表

时间:2015-10-28 22:11:03

标签: spring spring-security h2

启动我的应用程序时,Spring Security正在创建两个表"用户"和"当局"在我的h2数据库中。我的问题是,当我第二次启动应用程序时,数据库已经填充了这两个表,但Spring Security直到想要创建它们。 如果不存在,我怎么能告诉Spring Secutiry只创建表?或者我应该改变别的东西来解决这个问题?

Spring Secutiry正在创建我从博客中获得的两个表,描述了我所看到的一些代码。它说:"关于数据模型的一些注释。 USERS表中没有真正的用户名和密码。该表由Spring Security定义,用于通用目的,而不是特定于社交登录用例。"

Link to the blog

我得到的错误消息,一些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();
}

2 个答案:

答案 0 :(得分:2)

在配置Spring Security时,在我的情况下创建了USERS表(以及其他一些表)。这是该代码的一部分:

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.
        jdbcAuthentication()
        .dataSource(dataSource)
        .withDefaultSchema(); 
}

如果我从已配置的现有数据库开始并在其中部署数据,我将删除行:

      .withDefaultSchema(); 

并且Spring Security将使用数据库中的现有表。

答案 1 :(得分:0)

我这样处理:

    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();
    }