Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
springsecurity创建一个名为USERS的表_Spring_Spring Security_H2 - Fatal编程技术网

springsecurity创建一个名为USERS的表

springsecurity创建一个名为USERS的表,spring,spring-security,h2,Spring,Spring Security,H2,启动我的应用程序时,Spring安全性在我的h2数据库中创建两个表“users”和“authorities”。我的问题是,当我第二次启动应用程序时,数据库中已经填充了这两个表,但是SpringSecurityTill想要创建它们。 我如何告诉Spring Secutiry仅在表不存在时创建它们?或者我应该换些别的东西来解决这个问题 Spring Secutiry正在创建我从博客中获得的两个表,其中描述了我所看到的一些代码。它说:“关于数据模型的一些注释。用户表中没有存储真实的用户名和密码。该表由

启动我的应用程序时,Spring安全性在我的h2数据库中创建两个表“users”和“authorities”。我的问题是,当我第二次启动应用程序时,数据库中已经填充了这两个表,但是SpringSecurityTill想要创建它们。 我如何告诉Spring Secutiry仅在表不存在时创建它们?或者我应该换些别的东西来解决这个问题

Spring Secutiry正在创建我从博客中获得的两个表,其中描述了我所看到的一些代码。它说:“关于数据模型的一些注释。用户表中没有存储真实的用户名和密码。该表由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]

我不确定这在代码中的什么地方发生。我有一个像这样的工厂

@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安全性时创建的。这是该代码的一部分:

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将使用数据库中的现有表。

我使用以下方法处理此问题:

    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();
    }
JDBCUserDetailsManager配置器配置器=
auth.jdbccauthentication().dataSource(dataSource);
//检查验证模式是否已填充(来自持久性源);如果不是,则将其设置为“填充”
if(!dataSource.getConnection().getMetaData().getTables(null,“,“USERS”,null).first()){
configurer=configurer.withDefaultSchema();
}
    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();
    }