WebSecurityConfigurerAdapter cannot be resolved to a type 找不到
时间: 2023-10-12 15:09:21 浏览: 69
WebSecurityConfigurerAdapter 是 Spring Security 中的一个类,需要引入 Spring Security 的依赖才能使用。如果出现了 WebSecurityConfigurerAdapter cannot be resolved to a type 的错误,可能是以下几个原因:
1. 未引入 Spring Security 的依赖,需要在 pom.xml 文件中添加如下代码:
```
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.4.2</version>
</dependency>
```
2. 在代码中没有正确导入 WebSecurityConfigurerAdapter 类,需要在代码中添加如下代码:
```
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
```
3. 如果使用的是 Spring Boot,还需要在启动类上添加 @EnableWebSecurity 注解,例如:
```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@SpringBootApplication
@EnableWebSecurity
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```