BeanCreationException spring boot and keycloak - java

While trying to follow the article https://sandor-nemeth.github.io/java/spring/2017/06/15/spring-boot-with-keycloak.html
I set up my keycloak oauth in spring boot application exactly the same apart form the fact that I am using maven pom.xml instead of gradle.
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-2-starter</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-adapter</artifactId>
<version>9.0.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>9.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The Java part is the same as in the article:
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(securedEnabled = true)
public class KeycloakSecurityConfigurer extends KeycloakWebSecurityConfigurerAdapter {
#Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) {
final SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
mapper.setConvertToUpperCase(true);
final KeycloakAuthenticationProvider provider = keycloakAuthenticationProvider();
provider.setGrantedAuthoritiesMapper(mapper);
auth.authenticationProvider(provider);
}
#Bean
public KeycloakSpringBootConfigResolver keycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
#Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new NullAuthenticatedSessionStrategy();
}
#Override
protected void configure(final HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests();
}
While trying to start the spring boot application
java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar "${HOME}/app.war" "$#"
I am getting BeanInCreationException:
rg.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'undertowServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedUndertow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.keycloak.adapters.springboot.KeycloakAutoConfiguration': Unsatisfied dependency expressed through method 'setKeycloakSpringBootProperties' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'keycloakSecurityConfigurer': Unsatisfied dependency expressed through field 'keycloakConfigResolver'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'keycloakConfigResolver': Requested bean is currently in creation: Is there an unresolvable circular reference?","logger":"org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext","thread":"main","level":"WARN"}
{"time":"2020-03-10T12:53:22.667+00:00","msg":"Application run failed","logger":"org.springframework.boot.SpringApplication","thread":"main","level":"ERROR","stack_trace":"org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'undertowServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedUndertow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.keycloak.adapters.springboot.KeycloakAutoConfiguration': Unsatisfied dependency expressed through method 'setKeycloakSpringBootProperties' parameter 1

Related

Error creating bean with name 'springSecurityFilterChain API endpoint

I am trying to implement oauth2 on some endpoints within my API. here is my config class:
#Configuration
#Order(102)
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.mvcMatchers(HttpMethod.GET, "/").permitAll()
.mvcMatchers(HttpMethod.GET, "/endpoint1").hasAuthority("SCOPE_READ")
.mvcMatchers(HttpMethod.GET, "/").hasAuthority("SCOPE_WRITE")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt();
}
There are other config files within my project hence I added the order annotation but I keep receiving the Error creating bean with name 'springSecurityFilterChain error.
Here are the related dependencies in my pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>com.okta.spring</groupId>
<artifactId>okta-spring-boot-starter</artifactId>
<version>1.5.1</version>
</dependency>
I tried explicitly using the last version (5.5.3) of spring security web but I'm still receiving the error. Here's the full stack trace:
"timestamp":"2021-11-02T15:16:59.774Z","message":"Application run failed","logger":"o.s.b.SpringApplication","thread":"restartedMain","level":"ERROR","stack_trace":"<#20be4d11> o.s.b.f.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/log/LogMessage\r\n\tat o.s.b.f.s.ConstructorResolver.instantiate(ConstructorResolver.java:627)\r\n\tat o.s.b.f.s.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)\r\n\tat o.s.b.f.s.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)\r\n\tat o.s.b.f.s.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
This is my first time working with the Spring security features and would like more insight on resolving this issue.

Spring bootb absctractmethoderror after hibernate upgrade

My project use spring boot 1.3.8 and now I have to upgrade hibernate from 4.* to 5.*. When I add dependencies and start project I have error "java.lang.AbstractMethodError" after initialization Datasource bean. What can I do for resolve this problem ?
#Bean
public LocalContainerEntityManagerFactoryBean ussdDBEntityManagerFactory(final EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean emf = builder
.dataSource(ussdDBDataSource())
.persistenceUnit("ussdDBPersistenceUnit")
.build();
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
emf.setJpaProperties(properties);
return emf;
}
Error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ussdDBEntityManagerFactory' defined in class path resource [com/intech/kievstar/config/DBUssdConfiguration.class]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError
Solution of my problem was dependency:
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>5.0.0.GA</version>
</dependency>
When I up verson to 5.0.0.GA, problem was resolved

Swagger Error with version upgrading version > 2.6.1 - NoSuchMethodException: BasePathAwareServicesProvider.<init>()

I have upgraded springfox to version 2.7.0(and 2.9.2):
<springfox.version>2.7.0</springfox.version>
<spring-boot.version>1.3.3.RELEASE</spring-boot.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${springfox.version}</version>
</dependency>
and trying to start the application I get the next error:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'documentationPluginsBootstrapper'
defined in URL
[jar:file:.....jar!/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]:
Unsatisfied dependency expressed through constructor argument with
index 1 of type [java.util.List]: Error creating bean with name
'basePathAwareServicesProvider' defined in URL
[jar:file:/..............!/lib/springfox-data-rest-2.7.0.jar!/springfox/documentation/spring/data/rest/BasePathAwareServicesProvider.class]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[springfox.documentation.spring.data.rest.BasePathAwareServicesProvider]:
No default constructor found; nested exception is
java.lang.NoSuchMethodException:
springfox.documentation.spring.data.rest.BasePathAwareServicesProvider.();
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'basePathAwareServicesProvider' defined in URL
[jar:file:............!/lib/springfox-data-rest-2.7.0.jar!/springfox/documentation/spring/data/rest/BasePathAwareServicesProvider.class]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[springfox.documentation.spring.data.rest.BasePathAwareServicesProvider]:
No default constructor found; nested exception is
java.lang.NoSuchMethodException:
springfox.documentation.spring.data.rest.
BasePathAwareServicesProvider. init ()
No default constructor found; BasePathAwareServicesProvider. init ()
I have annotated the classes like this in order to avoid some errors with swaggers-ui.html:
#Configuration
#Profile("swaggerEnabled")
#EnableSwagger2
#EnableWebMvc
#Import(SpringDataRestConfiguration.class)
public class SwaggerConfiguration {
The Springboot application:
#EnableSwagger2
#EnableWebMvc
#SpringBootApplication
#Import(value = {
......class,})
#Order(Ordered.HIGHEST_PRECEDENCE)
#ComponentScan(basePackages = {"....."})
public class PlatformApiApplication {
I've tried some solutions from github but nothing works. Can somebody help me? thanks.
Removing the line:
#Import(SpringDataRestConfiguration.class)
The error dissapears

Error creating bean with name '...RedisHttpSessionConfiguration': Initialization of bean failed

I'm using Redis and created HttpSessionConfig file. Here is my code in
HttpSessionConfig.java:
#EnableRedisHttpSession
public class HttpSessionConfig {
#Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
#Bean
public HttpSessionIdResolver httpSessionStrategy() {
return HeaderHttpSessionIdResolver.authenticationInfo();
}
}
And here is my pom.xml file:
<!-- Redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>4.3.1.Final</version>
</dependency>
But I got error:
Error creating bean with name 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration':
Initialization of bean failed
More trace:
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'sessionRepositoryFilterRegistration'
defined in class path resource
[org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]:
Unsatisfied dependency expressed through method
'sessionRepositoryFilterRegistration' parameter 1; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration':
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'connectionFactory' defined in class path
resource [bookstore/config/HttpSessionConfig.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]:
Factory method 'connectionFactory' threw exception; nested exception
is java.lang.NoClassDefFoundError: io/lettuce/core/KeyValue
Caused by: org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration':
Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'connectionFactory' defined in class path
resource [bookstore/config/HttpSessionConfig.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]:
Factory method 'connectionFactory' threw exception; nested exception
is java.lang.NoClassDefFoundError: io/lettuce/core/KeyValue
Anyone know how to fix it ???
In the stack trace the error tells that
java.lang.NoClassDefFoundError: io/lettuce/core/KeyValue
So add manually the dependency io.lettuce in your pom.xml
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>

java.lang.NullPointerException at org.apache.tiles.access.TilesAccess.getContainer(TilesAccess.java:124)

I do have all jars of spring 4.1.7 and tiles 3.0.5 version jars,
Please find below error .
SCHWERWIEGEND: Servlet.service() for servlet [mccstore] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'standard_welcome': Invocation of init method failed; nested exceptio
n is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.apache.tiles.access.TilesAccess.getContainer(TilesAccess.java:124)
at org.apache.tiles.access.TilesAccess.getContainer(TilesAccess.java:107)
at org.springframework.web.servlet.view.tiles3.TilesView.afterPropertiesSet(TilesView.java:97)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
Make sure you have defined the following beans:
#Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tiles = new TilesConfigurer();
tiles.setDefinitions("/WEB-INF/definitions.xml");
return tiles;
}
#Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver tilesViewResolver = new UrlBasedViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
and have the following dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Hope it helps.

Categories