How to use jdbc-authentication of Spring boot/spring Security with Flyway

Source: Internet
Author: User
Tags auth

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$(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 inclassPath 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 totrueTo 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:60W) 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)    ... 29morecaused by:org.flywaydb.core.api.FlywayException:Found Non-empty schema ' test ' without metadata table! Use baseline () or set Baselineonmigrate totrueTo 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 This 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 DataSourceDataSource; @Override Protected voidConfigure(Httpsecurityhttp) Throws Exception {http.Authorizerequests() .Anyrequest().Authenticated() .and() .Httpbasic() .Realmname("Shipment2rss") .and() .Logout() .Permitall(); } @Autowired public void (authenticationmanagerbuilder auth   throws exception< Span class= "PLN" > { auth . ()  . (datasourcewithdefaultschema (); } }          /span>                

I have read, the problem seams to be, the method is configureGlobal(AuthenticationManagerBuilder ) invoked before the flyway-related code gets executed (SE e How to use Flyway in Spring Boot with JDBC Security?) But found no step-by-step the guide, the 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:https://github.com/smilingj/springboot-authentication-flyway-sample/ tree/e48ce63568776d99e49a9548d8362168cc3a3367

When configuring the and jdbcAuthentication calling, withDefaultSchema directly creates the schema and does so before Flyway have any change to Create the schema.

Flyway now detects it's already there instead of it being allowed to create the schema and it complains on that.

You have 2 possible solutions

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

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

How to use jdbc-authentication of Spring boot/spring Security with Flyway

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.