我正在尝试编写一个弹簧安全测试,如此处所述http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test-mockmvc-setup。我需要导入SecurityMockMvcConfigurers
作为静态导入,但我的IDE找不到该类。
我的pom.xml如下所示:
<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导入Spring Security 4.0.4.RELEASE。我在这个版本中找不到这个类。我需要哪种额外的依赖?或者我没有考虑过其他什么?
答案 0 :(得分:34)
缺少的依赖是:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
答案 1 :(得分:1)
org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers
类所需的依赖关系是org.springframework.security:spring-security-test
依赖关系。
标记的问题Spring Boot和Spring Boot在spring-boot-starter-parent
pom中将依赖关系作为托管依赖项提供。
你的pom很可能从spring-boot-starter-parent
继承
因此,最重要的是不要在没有充分理由的情况下指定版本,并且只使用从父级继承的版本
这就够了:
<dependencies>
...
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
...
<dependencies>
例如,对于Spring Boot 2.0.0.M5,托管版本为
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.0.0.M5</version>
</dependency>
M5
不只是机会
这些设计用于更好地工作&#34;在一起。
请注意,org.springframework.boot:spring-boot-starter-test
依赖关系不会提取spring-security-test
依赖关系。
如果您不使用Spring Boot,您应该尽力指定符合应用程序使用的Core Spring版本的spring-security-test
版本。
答案 2 :(得分:-2)
SecurityMockMvcConfigurers类包含在
中<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
在其他版本中,SecurityMockMvcConfigurers可能不存在。
但是有一个类MockMvcConfigurer,可能你可以用这个来代替吗?
关于:但此依赖关系不包含相应的类。
是的,你是正确的,这个类没有被包含在罐子里。 所以我认为你必须使用那里的东西。 无论如何,这个类很简单,源代码在这里: SecurityMockMvcConfigurers
只需复制您自己班级中的代码即可。