Can't find SecurityMockMvcConfigurers - java

I'am trying to write a spring security test as described here http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test-mockmvc-setup. I need to import SecurityMockMvcConfigurers as a static import, but my IDE does not find the class.
My pom.xml looks as follows:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring Boot 1.4.0.M2 imports Spring Security 4.0.4.RELEASE. I' can't find the class in this release. Which additional dependency do I need? Or what else I haven't considered?

The missing dependency was:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>

The required dependency for the org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers class is the org.springframework.security:spring-security-test dependency.
The question is tagged Spring Boot and Spring Boot provides the dependency as a managed dependency in the spring-boot-starter-parent pom.
Your pom inherits very probably from spring-boot-starter-parent.
So above all don't specify a version without a very good reason and use simply the version inherited from the parent.
That is enough :
<dependencies>
...
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
...
<dependencies>
For example with Spring Boot 2.0.0.M5, the managed version is
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.0.0.M5</version>
</dependency>
The M5 is not just chance.
These are designed to work "better" together.
Note that org.springframework.boot:spring-boot-starter-test dependency doesn't pull the spring-security-test dependency.
In case you don't use Spring Boot, you should endeavor to specify a spring-security-test version conform to the Core Spring version used by the application.

The class SecurityMockMvcConfigurers is included in
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
In other releases SecurityMockMvcConfigurers may not exist.
However there is a class MockMvcConfigurer, may be you can work with this instead ?
regarding : but this dependency does not contain the corresponding class.
Yes you are right that class is no inluded in the jar.
So I think you have to use what is there.
Anyway, the class is rather simple and the sourcecode is here :
SecurityMockMvcConfigurers
Just copy the code in your own class and you are done.

Related

Spring dependency injection in maven multi modules project

I'm building a multi modules maven project with 4 modules. For two of them (the one with the rest controller and the one with the core business logic) I need the power of the dependency injection. But is not working at all. Here my parent pom:
....
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
....
<modules>
<module>taskexecutor-service</module>
<module>taskexecutor-core</module>
<module>taskexecutor-common</module>
<module>taskexecutor-rules</module>
</modules>
.....
Child pom service:
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-service</artifactId>
.....
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Child pom core (the one with the class that i can't inject into the service project)
....
<parent>
<groupId>com.example.taskexecutor</groupId>
<artifactId>taskexecutor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>taskexecutor-core</artifactId>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The package inside the two project (service and core) are the same: com.example.application (where is the main class with #SpringBootApplication in the service project), com.example.application.restcontroller (where is the controller in the service project), com.example.application.core (where is the #component that I can't inject in the core project).
Inside the core project I have, under the com.example.application.core a #Component that I would inject inside the class into the service project under the com.example.application.restcontroller package.
Within the same project and under the package of the main class, the dependency injection works absolutely fine. Between two different modules I can't achieve that (nullPointerException).
Any advice will be appreciated
Your description about the packages is hard to understand. But, it most likely is a Component scanning issue. Make sure that you are scanning all the packages. For instance, In the taskexecutor-service, you can do:
#ComponentScan(basePackages = {"com.example.taskexecutor", "com.example.application"})

The dependency spring-boot-starter-security is not found in a simple Maven project

I am trying to inject the spring-boot-starter-security dependency in a simple spring boot application (Maven). I tried to copy this dependency from the "Securing a Web Application" tutorial from the actual Spring website, I also tried to implement it together with the spring security test dependency and thymeleaf spring security dependency.
So here are the dependencies that get the error "not found":
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
And here it is the full pom file:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.homehero
homehero
0.0.1
war
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I feel the need to thank y'all in advance and sorry if I'm missing something really stupid, I really hope I'm not!
I solved the problem.
Long story short, a friend of mine suggested doing a new project with Spring Initializer and this time add from the beginning the "Spring Boot Security" dependency. After that, I just needed to compare the pom files. The only difference was that the new project had a:
<scope>test</scope>
line. I added that to my initial project and the first dependency did not get the error anymore. The second dependency was the Thymeleaf spring security one, in order to fix this one I added:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
To my pom file.
So this is how the dependencies look together now:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
That's it, thank you for all the answers you guys also provided.
Take a look in the version of parent POM.xml file. When I've changed the version to 2.0.0 my project founds the dependency.
<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 had the same problem. follow these steps:
1- Delete the dependencies that are not found.
2- Sync your project.
3- Add the dependencies.
4- sync the project again.
it worked for me hope works for everyone else:)
Not sure how you are creating project.I can tell you simple solution,which is to go to Spring Initializer ,link for which is https://start.spring.io/
There you can add all your required dependencies and then just click on Generate,which will download your project.You can just import the project in your IDE and run maven build.I tried with Spring boot version 2.3.1 only as you mentioned and it worked for me and should work for you.
Also,I would suggest you to user Intellij IDE,which comes with maven support by default,so it will make things easy for you.You can download Intellij free edition from here:
https://www.jetbrains.com/idea/download/#section=windows
Hope it will help you.

maven dependency hell: java.lang.NoSuchMethodError

I am trying to convert one of our existing maven projects to spring boot. I dont have any compile errors in my project but when I try to run it I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.util.MultiValueMap.addAll(Ljava/lang/Object;Ljava/util/List;)V
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:140)
at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:119)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:266)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
at com.myorg.MyMainClass ...
My POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>couple of our other internal projects</groupId>
**One of these projects is using spring-context and spring-orm** dependencies.
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Since the spring dependencies in my own project are declared before the other projects I would have thought it would override any of those transitive dependencies.
I have wasted an entire day on this. Can some one please help?
Since the spring dependencies in my own project are declared before the other projects I would have thought it would override any of those transitive dependencies.
That is in fact the issue. Your project is dictating what is included, but some of the dependencies require a different version. They are still trying to call the method that exists in their version, but not in yours, and can't find it because you've forcibly excluded their version. That should not be done unless you're confident that the two versions are compatible!
Solutions:
Align on a single version, and use that
Use more compatible versions
Include both versions via shading (see this post) or a similar mechanism

Maven: Transitive Dependencies

I am working on a library for some projects which relies on Spark and HBase.
So the POM of the library looks something link this:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
</dependency>
...
</dependencies>
And in the specific project that uses the central library (which is published on an internal Maven repository) I have this:
<dependencies>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>myartifact</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
This, however does not then automatically include the dependencies that the library itself has. Therefore I would need to copy the dependency section of the library POM into the application POM.
Do you have any advice what might be missing/wrong?
Thanks and regards!

Using Spring boot starters in regular Spring project

There is any chance to use spring-boot-starters in regular spring project? I have, big trouble with configuration Spring Social project. I've already configured many others things and I don't want to move my project to Spring Boot just for one library.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-facebook</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-linkedin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-twitter</artifactId>
</dependency>
There is any possibility to use Spring Boot goods in regular Spring project? I'm quite confused about Spring Boot, as I read that project helps to quick start project on Spring without making many configs, there are some other pros against regular Spring Project?
But my main question in this thread is: "how to use spring-boot-starters in regular spring project?"
There is independent project (I can't put more than 2 links, so, others you can find in comment):
http://projects.spring.io/spring-social-facebook/
And there is a spring social quick start, which will help you to start with it:
https://github.com/spring-projects/spring-social-samples/tree/master/spring-social-quickstart
spring-social-starter have only maven dependencies. For example, spring-boot-starter-social-facebook contains this in pom.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
</dependency>
</dependencies>
You can add this without dependencies from org.springframework.boot package in your pom for using it without spring-boot functionality.

Categories