博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway
阅读量:6713 次
发布时间:2019-06-25

本文共 8393 字,大约阅读时间需要 27 分钟。

 

java.lang.IllegalStateException: Failed to load ApplicationContext    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:497)    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema `test` without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111)    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)    ... 29 moreCaused by: org.flywaydb.core.api.FlywayException: Found non-empty schema `test` without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1035)    at org.flywaydb.core.Flyway$1.execute(Flyway.java:1006)    at org.flywaydb.core.Flyway.execute(Flyway.java:1418)    at org.flywaydb.core.Flyway.migrate(Flyway.java:1006)    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)

 

 

I am trying to set up my spring boot application that authenticates its users using the jdbcAuthentication and the default database scheme provided in the appendix of the spring security documentation. But i am stuck getting this exception during the database initialization:

org.flywaydb.core.api.FlywayException: Found non-empty schema "PUBLIC" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.

The configuration of the authentication manager looks like this:

@Configuration    @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .httpBasic() .realmName("shipment2rss") .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSource) .withDefaultSchema(); } }

I have read that the problem seams to be that the method configureGlobal(AuthenticationManagerBuilder ) is invoked before the Flyway-related code gets executed (see ) but found no step-by-step guide how to work around this specific problem.

Can anyone give me such a guide or point me to a website that does?

EDIT I uploaded a project to show the problem at github: 

When configuring the jdbcAuthentication and calling withDefaultSchema that directly creates the schema and does so before Flyway has any change to create the schema.

Flyway now detects it is already there instead of it being allowed to create the schema and it complains about that.

You have 2 possible solutions

  1. Extend the FlywayMigrationStrategy and set the baselineOnMigrate property to true.
  2. Better is to let Flyway do all the database migrations. To enable that remove the call to withDefaultSchema and simply add the sql to create the Spring Security tables to Flyway. The SQL files are part of the Spring Security distribution.

http://stackoverflow.com/questions/30013953/how-to-use-jdbc-authentication-of-spring-boot-spring-security-with-flyway

 

转载地址:http://bhhlo.baihongyu.com/

你可能感兴趣的文章
数据恢复软件哪个好
查看>>
『火车进出栈问题 卡特兰数』
查看>>
第四天:HTTP&Tomcat
查看>>
python 文件和路径操作函数小结
查看>>
条件+努力=?
查看>>
HBase分布式安装
查看>>
随笔-文件的读写
查看>>
tcp 状态以及三次握手
查看>>
我的友情链接
查看>>
WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK!的分析
查看>>
nginx禁止ip直接访问
查看>>
hadoop常用服务管理命令
查看>>
10.28 rsync工具10.29-10.30 rsync选项10.31 rsync通过ssh同步
查看>>
Fault,Error and Failure
查看>>
Go语言的通道(1)-无缓冲通道
查看>>
spring oauth从请求中获取token
查看>>
6.18docker(一)Compose 模板文件
查看>>
每天学点GDB 9
查看>>
为什么要用 /dev/null 2>&1 这样的写法
查看>>
AngularJs创建省,市,区的3级列表
查看>>