我使用 H2 数据库作为测试应用程序,使用 Spring boot。每次,当我重新启动 Spring Boot 应用程序时,H2 中的数据都会被清除。我正在使用文件而不是内存。我spring.jpa.hibernate.ddl-auto=update也在 application.properties 中设置了。这是我的 application.properties 文件
spring.datasource.url=jdbc:h2:file:./data/demo
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么。它可以很好地保存数据。但是一旦我关闭应用程序,所有数据都会被清除。
这是 Spring Boot 3.0.1 版本的问题。这对于 2.7.5 版本来说工作得非常好。
我的应用程序使用 Spring Security 和 JDBC 会话。所以我被X-Auth-Token保存在spring_session我的数据库(PostgreSQL)的表中。当我调用登录控制器方法时,我已经实现了UserDetailsService从数据库中获取用户的接口。
当迁移到新的 Spring Boot 3 版本时,主要的变化发生在WebSecurityConfig类中。以前它是从WebSecurityConfigurerAdapter班级延伸出来的。所以我知道问题出在这堂课的某个地方。
在这里,我复制并粘贴该类的旧版本 2.7.5(工作正常)和新的 3.0.1 版本WebSecurityConfig:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
@Autowired
UserDetailsService userDetailsService;
@Autowired
private AuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected …Run Code Online (Sandbox Code Playgroud)