Spring Security(Third Edition)
上QQ阅读APP看书,第一时间看更新

Configuring the H2 embedded database

To configure the H2 embedded database, we need to create a DataSource and run SQL to create the Spring Security table structure. We will need to update the SQL that is loaded at startup to include Spring Security's basic schema definition, Spring Security user definitions, and the authority mappings for users. You can find the DataSource definition and the relevant updates in the following code snippet:

    //src/main/java/com/packtpub/springsecurity/configuration/DataSourceConfig.java

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setName("dataSource")
.setType(EmbeddedDatabaseType.H2)
.addScript("/database/h2/calendar-schema.sql")
.addScript("/database/h2/calendar-data.sql")
.addScript("/database/h2/security-schema.sql")
.addScript("/database/h2/security-users.sql")
.addScript("/database/h2/security-user-authorities.sql")
.build();
}

Remember that the EmbeddedDatabaseBuilder() method creates this database only in memory, so you won't see anything on the disk, and you won't be able to use standard tools to query it. However, you can use the H2 console that is embedded in the application to interact with the database. See the instructions on the Welcome page of our application to learn how to use it.