6

Firstly I was working with spring-boot version 1.5.x and My application was working perfect. I upgraded to spring-boot 2.1.6 and having that error with spring-boot security.Here is my logcat

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.technohomes.user.UserApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:596) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:108) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:805) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_212]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at com.technohomes.user.UserApplication.main(UserApplication.java:19) [classes/:na]
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist

Here is what I changed I upgraded the gradle version to 5.5.1, spring-boot version to 2.1.6 and updated all the dependencies to latest as well.

Some resources pointed out that this class isn't available in spring-boot 2 but none of the alternate solution was provided

Here is my build.gradleh

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
apply plugin: 'docker'
buildscript {
project.ext {
    springBootVersion = '2.0.4.RELEASE'
    jarName = 'basic-user-webservice'
    versionName = '0.0.1'
    gradleDockerVersion = '1.2'
    swagger2version = '2.9.2'
}
repositories {
    jcenter()
    mavenCentral()

}

dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-    plugin:${project.springBootVersion}"
    classpath "se.transmode.gradle:gradle-    docker:${project.gradleDockerVersion}"
}
}

task createWrapper(type: Wrapper) {
gradleVersion = '5.1.1'
}
group = 'technohomes'
mainClassName = 'com.technohomes.user.UserApplication'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
jcenter()
}

ext {
springCloudVersion = 'Greenwich.SR2'
}


dependencies {

compile group: 'org.springframework.security', name: 'spring-security-web', version: '5.1.5.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-config', version: '5.1.5.RELEASE'
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5', version: '3.0.4.RELEASE'


compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.0.4.RELEASE'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.0.4.RELEASE'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.1.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.4.7.RELEASE'


compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.0.RELEASE'

compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.0.4.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '2.1.3.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.0.4.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.10.RELEASE'


   compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '2.1.2.RELEASE'

// Swagger for API testing
compile("io.springfox:springfox-swagger2:${swagger2version}")
compile("io.springfox:springfox-swagger-ui:${swagger2version}")
testCompile("org.springframework.security:spring-security-test")

}

jar {
baseName = "${project.jarName}"
version = "${project.versionName}"
}
task buildDocker(type: Docker, dependsOn: build) {
push = false
applicationName = jar.baseName
tagVersion = jar.version
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
    // Copy the built fat jar to the Docker plugin staging directory
    copy {
        from jar
        into stageDir
    }
    // Copy the run.sh file to the Docker plugin staging directory
    copy {
        from "${project.buildDir}/resources/main/run.sh"
        into stageDir
    }
}
}

run {
jvmArgs = ['-Xdebug', '- 
Xrunjdwp:server=y,transport=dt_socket,address=4200,suspend=n', '- 
Dspring.profiles.active=default']
}

MainClass

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import         org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableEurekaClient
@EnableResourceServer
@EnableSwagger2
@EnableOAuth2Client
public class UserApplication {

public static void main(String[] args) {
    SpringApplication.run(UserApplication.class, args);
}
}

Update 1 after updating Eureka client below error was thrown

java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to [Ljava.lang.Object;
at org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelector$ConfigurationPropertiesBeanRegistrar.lambda$collectClasses$1(EnableConfigurationPropertiesImportSelector.java:77) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267) ~[na:1.8.0_212]
at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235) ~[na:1.8.0_212]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_212]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_212]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_212]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_212]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_212]
at org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelector$ConfigurationPropertiesBeanRegistrar.collectClasses(EnableConfigurationPropertiesImportSelector.java:78) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelector$ConfigurationPropertiesBeanRegistrar.getTypes(EnableConfigurationPropertiesImportSelector.java:73) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelector$ConfigurationPropertiesBeanRegistrar.registerBeanDefinitions(EnableConfigurationPropertiesImportSelector.java:67) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:364) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_212]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:145) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at 

org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at com.technohomes.user.UserApplication.main(UserApplication.java:19) [classes/:na]

please help me figure this out. Thanks

Lily
  • 605
  • 3
  • 15
  • 31
  • please copy the build.gradle here, since this question could help other people in the future and the resource might not be available. Other than upgrade the version the gradle, did you apply any other change into your code? – jpganz18 Jul 31 '19 at 11:18
  • I am unable to format build.gradle that' why I psoted it there. – Lily Jul 31 '19 at 11:19
  • I upgraded the gradle version to 5.5.1, spring-boot version to 2.1.6 and updated all the dependencies to latest as well. Some resources pointed out that this class isn't available in spring-boot 2 but none of the alternate solution was provided – Lily Jul 31 '19 at 11:20
  • @jpganz18 I have added gradle and updated question as well. Please have a look – Lily Jul 31 '19 at 11:29
  • My guess is that could be spring-cloud eureka, its an old version and might not be compatible, can you try to implement this? https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client/2.1.2.RELEASE – jpganz18 Jul 31 '19 at 11:39
  • You are mixing jars from different versions of Spring Boot and Spring Cloud. That is trouble waiting to happen. Let Spring Boot manage your dpeendencies (that is one of the things it does) don't try to get your own. – M. Deinum Jul 31 '19 at 11:46
  • @jpganz18 i've tried that and error waws thrown. Logcat has been added to question. please have a look. Thanks – Lily Jul 31 '19 at 11:49
  • 1
    Remove the `version` tags from the dependencies. The `spring-security` dependencies you don;'t need (those are part of the `spring-boot-starter-security`). For Spring Cloud import the `bom` and then remove the versions so yuou have compatible versions. – M. Deinum Jul 31 '19 at 11:51
  • @M.Deinum I'm now having this issue ` org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: An AuthenticationManager is required` – Lily Jul 31 '19 at 12:09

4 Answers4

10
add bellow dependancy
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>
Avinash Khadsan
  • 441
  • 3
  • 6
4

Your dependencies are a bit of a mess. You are mixing jars of different version of both Spring Boot and Spring Cloud and use incompatible Spring Boot / Spring Cloud versions. The Spring Boot plugin (or rather the dependency plugin) will manage the Spring Boot and Spring Cloud dependencies for you.

Your dependencies should look something like this

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

dependencies {

  compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'

  compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'

  compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka'
  compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin'
  compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config'


  // Swagger for API testing
  compile("io.springfox:springfox-swagger2:${swagger2version}")
  compile("io.springfox:springfox-swagger-ui:${swagger2version}")
  testCompile("org.springframework.security:spring-security-test")
  testCompile group:  'org.springframework.boot', name: 'spring-boot-starter-test'

}

and you need the dependency management plugin (formerly part of the spring boot plugin).

apply plugin: 'io.spring.dependency-management'
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • by adding `apply plugin: 'io.spring.dependency-management'` is causes Abstract Method Error:null – Lily Jul 31 '19 at 12:32
  • When? Also not sure if that is compatibe with Gradle 5.5.1 yet. – M. Deinum Jul 31 '19 at 12:35
  • 1
    because that doesn't exists anymore, should be `spring-cloud-starter-netflix-eureka-client` now ( I didn't change your dependencies, just removed the versions). – M. Deinum Jul 31 '19 at 12:37
1

Exception is thrown when OpenFeign dependency is used

<artifactId>spring-cloud-starter-openfeign</artifactId>

Switch to spring-cloud-starter-feign

<dependency>
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-feign</artifactId> 
    <version>1.4.7.RELEASE</version>
</dependency>
0

Use this command find libdir -name "*.jar" -exec zipgrep "CLASS_NAME" '{}' \; to find where this class is used.

The command is taken from https://stackoverflow.com/a/10331145/6204666

After you find the lib where your class is used, try to change version of the lib for proper one.

In my case the class 'ServerPropertiesAutoConfiguration' was used in ZuulServerAutoConfiguration class of spring-cloud-netflix-core lib

ru51an
  • 113
  • 1
  • 1
  • 10