spring-attic / spring-cloud-security

Security concerns for distributed applications implemented in Spring
Apache License 2.0
531 stars 244 forks source link

Can't initialize a database schema with Spring Cloud #143

Closed waincent closed 5 years ago

waincent commented 6 years ago

Question

I want add Spring Cloud Security with OAuth2, an exception occurs when initialize the script .

Bug report

Spring Boot Version : 2.0.0.RELEASE Spring Cloud Version: Finchley.M8

Just Spring Boot, the server can initialize a database with schema

the build.gradle:

dependencies {

compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-data-jpa' compile 'mysql:mysql-connector-java'

testCompile group: 'junit', name: 'junit', version: '4.12' }

the application.properties:

spring.datasource.initialization-mode=always spring.datasource.name=test spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/auth?useSSL=false&characterEncoding=utf-8 spring.datasource.username= spring.datasource.password= spring.datasource.schema=classpath:database/schema-mysql.sql

the log:

2018-03-13 20:26:44.721 INFO 90228 --- [ main] com.btzh.test.TestApplication : Starting TestApplication on wanimal with PID 90228 (/Users/waincent/workspace/project/AnalysisPlatform/analysis-service-auth/out/production/classes started by waincent in /Users/waincent/workspace/project/AnalysisPlatform) 2018-03-13 20:26:44.729 INFO 90228 --- [ main] com.btzh.test.TestApplication : No active profile set, falling back to default profiles: default 2018-03-13 20:26:44.826 INFO 90228 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@16aa0a0a: startup date [Tue Mar 13 20:26:44 CST 2018]; root of context hierarchy 2018-03-13 20:26:49.479 INFO 90228 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$67bc7cb5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-03-13 20:26:51.127 INFO 90228 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8889 (http) 2018-03-13 20:26:51.226 INFO 90228 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-03-13 20:26:51.226 INFO 90228 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2018-03-13 20:26:51.249 INFO 90228 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/waincent/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2018-03-13 20:26:51.495 INFO 90228 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-03-13 20:26:51.496 INFO 90228 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6675 ms 2018-03-13 20:26:52.283 INFO 90228 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-03-13 20:26:52.292 INFO 90228 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2018-03-13 20:26:52.293 INFO 90228 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2018-03-13 20:26:52.293 INFO 90228 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2018-03-13 20:26:52.293 INFO 90228 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/] 2018-03-13 20:26:52.496 INFO 90228 --- [ main] com.zaxxer.hikari.HikariDataSource : test - Starting... 2018-03-13 20:26:53.191 INFO 90228 --- [ main] com.zaxxer.hikari.HikariDataSource : test - Start completed. 2018-03-13 20:26:53.201 INFO 90228 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [database/schema-mysql.sql] 2018-03-13 20:26:53.616 INFO 90228 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [database/schema-mysql.sql] in 415 ms. 2018-03-13 20:26:53.830 INFO 90228 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 2018-03-13 20:26:53.877 INFO 90228 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2018-03-13 20:26:54.255 INFO 90228 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.14.Final} 2018-03-13 20:26:54.257 INFO 90228 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found 2018-03-13 20:26:54.353 INFO 90228 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-03-13 20:26:54.729 INFO 90228 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2018-03-13 20:26:55.395 INFO 90228 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2018-03-13 20:26:57.041 INFO 90228 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@16aa0a0a: startup date [Tue Mar 13 20:26:44 CST 2018]; root of context hierarchy 2018-03-13 20:26:57.168 WARN 90228 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2018-03-13 20:26:57.309 INFO 90228 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-03-13 20:26:57.312 INFO 90228 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-03-13 20:26:57.417 INFO 90228 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-13 20:26:57.417 INFO 90228 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-13 20:26:57.512 INFO 90228 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-13 20:26:58.468 INFO 90228 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-03-13 20:26:58.472 INFO 90228 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure 2018-03-13 20:26:58.480 INFO 90228 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-03-13 20:26:58.584 INFO 90228 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8889 (http) with context path '' 2018-03-13 20:26:58.593 INFO 90228 --- [ main] com.btzh.test.TestApplication : Started TestApplication in 15.774 seconds (JVM running for 17.686)

When I add the dependencies with spring cloud

dependencies {

compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-data-jpa' compile 'org.springframework.cloud:spring-cloud-starter-security' compile 'org.springframework.cloud:spring-cloud-starter-oauth2'

compile 'mysql:mysql-connector-java' testCompile group: 'junit', name: 'junit', version: '4.12' }

the problem:

2018-03-13 20:30:21.044 INFO 94609 --- [ main] com.btzh.test.TestApplication : No active profile set, falling back to default profiles: default 2018-03-13 20:30:21.082 INFO 94609 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@102cec62: startup date [Tue Mar 13 20:30:21 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@36c88a32 2018-03-13 20:30:23.251 INFO 94609 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=refresh; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] 2018-03-13 20:30:23.565 INFO 94609 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=e1c12ad4-ed75-30c4-97c0-951f8d831ab4 2018-03-13 20:30:23.853 INFO 94609 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$435d5c9b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-03-13 20:30:23.958 INFO 94609 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$5f775f98] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-03-13 20:30:24.769 INFO 94609 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8889 (http) 2018-03-13 20:30:24.858 INFO 94609 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-03-13 20:30:24.859 INFO 94609 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2018-03-13 20:30:24.908 INFO 94609 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/waincent/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2018-03-13 20:30:25.146 INFO 94609 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-03-13 20:30:25.147 INFO 94609 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4065 ms 2018-03-13 20:30:25.756 INFO 94609 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-03-13 20:30:25.773 INFO 94609 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2018-03-13 20:30:25.774 INFO 94609 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2018-03-13 20:30:25.775 INFO 94609 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/] 2018-03-13 20:30:25.775 INFO 94609 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/] 2018-03-13 20:30:26.219 WARN 94609 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'scopedTarget.dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference? 2018-03-13 20:30:26.220 INFO 94609 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2018-03-13 20:30:26.355 INFO 94609 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-13 20:30:26.361 ERROR 94609 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

The dependencies of some of the beans in the application context form a cycle:

entityManagerFactory defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] ↓ entityManagerFactoryBuilder defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] ↓ jpaVendorAdapter defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] ┌─────┐ | scopedTarget.dataSource defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] ↑ ↓ | org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker └─────┘

Process finished with exit code 1

Enhancement

I don't know why.

digvijayS commented 6 years ago

I was facing exact same issue and after excluding HikariCP it disappeared. Try following it may work for you too

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>
djgraff209 commented 6 years ago

I ran into the same issue recently. I think the datasource initializer needs to be refactored to work like the higher level tools like liquibase or flyway.

The current implementation in spring boot is very greedy in that it runs the moment it comes onto scope. Under a cloud environment with cloud config, if the datasource is in refreshscope it will not be ready.

I got around this using liquibase

cherrishccl commented 6 years ago

The same issue in zipkin: `

io.zipkin.java
        <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
        <version>2.9.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>`
charle819 commented 6 years ago

@digvijayS Great answer , but can you please tell what will be used for Default Connection Pool if HikariCP is excluded.

hqthai05 commented 6 years ago

You can run testing successfully If you excluded HikariCP on pom file but will fail when try to running application. You can fix this issue by given exactly which data source type via property "spring.datasource.type = [type of datasource]" Hopefully this solution is useful.

devpreview commented 5 years ago

After upgrade to Spring Boot v2.1 I have same error.

After exclusion HikariCP Spring Boot can not autowire JPA repositories.

melloware commented 5 years ago

Having the exact same issue Spring Boot 2.1 and Spring Cloud Finchley.SR2

spencergibb commented 5 years ago

Valid versions are boot 2.0.6 and spring cloud Finchley.SR2 boot 2.1.0 and spring cloud Greenwich.M2

melloware commented 5 years ago

Sorry @spencergibb I was using Finchley and not Greenwich. Problem is solved.