ClassNotFoundException with Spring WebFlux Security - spring

I'm having the following problem:
I am trying to setup a basic Spring Boot (2.0.0.M2) Project containing Spring Webflux and Spring Security.
My pom.xml looks like this (generated via start.spring.io):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
...
</dependencies>
SpringSecurityConfig:
#EnableWebFluxSecurity
public class SecurityConfig extends WebFluxSecurityConfiguration {}
I am not getting any further because I get an exception on startup:
Caused by: java.lang.ClassNotFoundException: org.springframework.security.web.server.context.SecurityContextRepository
So I figure that the whole org.springframework.security.web.server package is not on the classpath, although it should be there, since it is included in the API docs.
Seems like I am doing something wrong, but I don't know what.

Currently Spring Boot's spring-boot-starter-security does not include spring-security-webflux.
Add it as project dependency explicitly.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-webflux</artifactId>
</dependency>
See this issue : Provide auto-configuration for WebFlux-based security.
BTW, check my working codes: https://github.com/hantsy/spring-reactive-sample/tree/master/boot
Updated: In Spring Boot 2.0.0.RELEASE, spring-boot-starter-security includes webflux support.

Related

Error Deploying Spring Boot App using 2.6.2 on IBM Websphere 8.5.5.20

I'm trying to Deploying a spring boot app using 2.6.2 on IBM Websphere 8.5.5.20 (Using java 8)
To test out the Issue I have tried to deploy a simple Hello World restcontroller and facing the issues noted below. I'm able to deploy the application using Spring Boot 1.5 on Websphere 8.5.5.20, but facing difficulty when moving to spring-boot 2.6.2.
Please note I have tried with the javax.servlet and jakarta.servlet (version as per spring boot dependencies). I have followed this Link [https://stackoverflow.com/questions/48156126/websphere-8-5-with-spring-5], Deploy Spring Boot 2.x apps on WebSphere 8.5.5
I get the following error based on how I have the classloader for my app by setting it from
Enterprise Applications > simple-boot > Manage Modules > simple-boot.war
Parent Last
Error:
[1/7/22 16:39:57:407 EST] 00000001 ContainerHelp E WSVR0501E: Error creating component com.ibm.ws.runtime.component.CompositionUnitMgrImpl#c412fa58
com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.webcontainer.exception.WebAppNotLoadedException: Failed to load webapp: Failed to load webapp: javax.servlet.ServletContainerInitializer: Provider ch.qos.logback.classic.servlet.LogbackServletContainerInitializer not a subtype
Parent First
Error:
[1/7/22 16:42:17:173 EST] 00000047 SystemOut O 2022-01-07 16:42:17.173 WARN 31546 --- [ver.startup : 1] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/validation/Configuration.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider; (loaded from file:/xxxxxxx/was/INSTANCE1/plugins/javax.j2ee.validation.jar by org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader#25be5b0d) called from class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean (loaded from file:xxxx/simple-boot.ear/simple-boot.war/WEB-INF/lib/spring-context-5.3.14.jar by
com.ibm.ws.classloader.CompoundClassLoader#24768f5e[war:simple-boot/simple-boot.war]
My pom.xml
Note: In this pom.xml I have javax.servlet using 3.1.0 for testing but as noted above tried with default version as per the spring boot dependencies also with same result.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.my.testApp</groupId>
<artifactId>simple-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>simple-boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>simple-boot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Based on the error message, the parent-last version looks like it's failing because there is a copy of the Servlet API packaged in your application. This is not a usable configuration, because the container will link to its own copy of the API, and as a result it wouldn't be able to successfully cast application artifacts that link to their own API instance. There are some APIs for which you can bring your own version in the app, but Servlet is not among them.
For the parent-first failure, it looks like Spring is expecting a newer version of the javax.validation package than is provided by the server - it's calling a method that doesn't exist in the server's version. WebSphere 8.5.5 implements Java EE 6, which includes Bean Validation 1.0, and that method wasn't added until Bean Validation 1.1. You'll need a version of Spring that works with Java EE 6 or is configurable to avoid higher-version dependencies. It might also be possible to resolve this by running parent-last and including a separate implementation along with the API (obviously, removing the Servlet API as referenced in the previous paragraph), although I'm not familiar enough with the Bean Validation API to know whether that would cause a conflict with the server runtime in the same way as Servlet.

Problem extending Spring WebFlux application with reactive DB client: conflicting Maven dependencies?

I have a small working Spring WebFlux app made basing on this code:
https://github.com/chang-chao/spring-webflux-reactive-jdbc-sample
As far as I've understood this is some kind of mix between purely reactive programming and usual blocking relational databases.
Now I have a task to add reactive DB client to my app. I stared with this guide:
https://spring.io/guides/gs/accessing-data-r2dbc/
But as soon as I added following dependencies to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>0.8.4.RELEASE</version>
<scope>runtime</scope>
</dependency>
my WORKING app failed to start saying it couldn't find autowired repository bean. This error disappeared as soon as I've removed two dependencies above.
Initial full pom.xml of WORKING app:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.freelance</groupId>
<artifactId>studentlist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-list</name>
<description>Spring WebFlux application for managing students</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.4.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I suppose some dependencies are conflicting with each other. Is there a way to actually use these dependencies with all my other dependencies so I'll be able to follow that guide?
I don't know if app's Java code is needed for this question, and if it is, which pieces. For now I'll just add application.properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:~/studentlist
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true
logging.level.org.springframework=warn
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
logging.level.org.hibernate.SQL=warn
logging.level.io.netty=warn
spring.datasource.maximum-pool-size=100
Adding either of these two dependencies, spring-boot-starter-data-r2dbc or r2dbc-h2, without the second one is enough to cause this error:
2020-10-20 15:31:26.008 WARN 11580 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.freelance.studentlist.repository.StudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-10-20 15:31:26.123 ERROR 11580 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentRepository in com.freelance.studentlist.service.StudentServiceImpl required a bean of type 'com.freelance.studentlist.repository.StudentRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.freelance.studentlist.repository.StudentRepository' in your configuration.
Process finished with exit code 1
I removed JPA dependency and I'm currently trying to make things work with just r2dbc.
I suppose my application.properties would no longer be valid. Could you please help me with changing it? I use this code fragment from the guide:
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
#Bean
public H2ConnectionFactory connectionFactory() {
return new H2ConnectionFactory(
H2ConnectionConfiguration.builder()
.url("jdbc:h2:~/studentlist;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4")
.username("sa")
.build());
}
}
I strongly suspect that such url is not valid for r2dbc. What would be valid substitution for jdbc:h2:~/studentlist URL in case of r2dbc H2 (not in-memory, just local DB)?
How should I change this block of code in application.properties ? URL in particular!
**spring.datasource.url=jdbc:h2:~/studentlist**
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
Please help if you know! Can't google an appropriate example for now...
The spring.datasource is for the traditional Jdbc data source, to configure an R2dbc connection, use spring.r2dbc prefix instead in Spring Boot application.properties.
Check my Spring R2dbc example, if you are new to Spring Data R2dbc, the docs in Readme.md.

Problem in configuring spring boot and redis

I have an old spring boot application (1.5.0-FINAL) and I can't change this version.
I want to add redis to my application, that's what I did:
1) added the maven dep:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
2) Added the property to my boot
#EnableCaching
public class MySpringBootApp{
3) Added config properties to check if it starts the connection:
spring.cache.type: redis
spring.redis.host: 192.168.99.100
spring.redis.port: 6379
The host/port above do not exist: I just want to see something like "connection error" on boot to make sure I configured everything but nothing appears! It seems that spring boot just doesn't try to use a cache.
Am I missing something?Maybe my spring boot version is too old?
Spring Boot parent pom already defines the versions of the starters, so remove the version from spring-boot-starter-data-redis dependency.
Your pom.xml would have at least these dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Next, #EnableCaching will look for beans with #Cacheable or #CachePut annotations.

ServerRequest on SpringBoot 2.0.0.RELEASE

I recently moved my app. from Spring Boot 1 to Spring Boot 2 (2.0.0.RELEASE).
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I want to import the class org.springframework.web.reactive.function.server.ServerRequest to get the Error Attributes from the interface
org.springframework.boot.web.reactive.error.ErrorAttributes;
but I got this error:
The import org.springframework.web.reactive.function.server.ServerRequest cannot be resolved
The Spring Boot Reactive Project spring-boot-web-reactive was merged into Spring Webflux with Spring 5.0
Make sure you have webflux Starter on your classpath:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</dependency>

How to use Spring Kafka 2.1.0.RELEASE

It is really great to grab the Spring Kafka 2.1.0.RELEASE to unleash the power of the kafka client 1.0.0. However when I tried to use it spring boot 1.5.9.RELEASE , which is the latest release version of boot it throws an exception .
java.lang.NoSuchMethodError: org.springframework.util.Assert.state(ZLjava/util/function/Supplier;)V
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.determineInferredType(MessagingMessageListenerAdapter.java:396) ~[spring-kafka-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.<init>(MessagingMessageListenerAdapter.java:100) ~[spring-kafka-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.<init>(RecordMessagingMessageListenerAdapter.java:61) ~[spring-kafka-2.1.0.RELEASE.jar:2.1.0.RELEASE]
My POM file is
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.1.0.RELEASE</version>
<scope>test</scope>
</dependency>
.....
How should I be able to use Spring Kafka 2.1.0.RELEASE with a spring boot project .
Is there any release of boot that support spring 5 yet.
Thanks
Joy
For the Answer
Click disqus.com
This is beautifully explained by Francisco , I put up the link as it might help others as well!
I am using Spring boot 1.5.9.RELEASE and spring-kafka:2.1.0.RELEASE have faced similar but not this exception, I would suggest cleaning and building application again to clear maven cache and dependencies.

Resources