java - 导入 org.springframework.security 无法解决
问题描述
我收到一个错误
import org.springframework.security 无法解决。
我搜索了,也许我缺少一个依赖项并且必须添加一个它,我不知道,请帮助我。我给你我的 pom.xml 文件和我的 java 类如果有人可以帮助我,我将非常感激
这是我的 pom.xml
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Music</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Music</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
和我的java类
package masters.rest;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import masters.beans.CommentBean;
import masters.beans.UserBean;
import masters.repositories.CommentRepo;
@RestController
public class Comment {
CommentRepo commentRepo;
public Comment(CommentRepo commentRepo) {
this.commentRepo = commentRepo;
}
@DeleteMapping(path = "/comment/delete")
@Secured("ROLE_ADMIN")
public ResponseEntity<Boolean> deleteComment(
@RequestParam(value = "id") int id,
HttpSession session
){
UserBean user = (UserBean)session.getAttribute("user");
if(user == null) {
return new ResponseEntity<>(false, HttpStatus.UNAUTHORIZED);
}
Optional<CommentBean> optionalComment = commentRepo.findById(id);
if(optionalComment.isPresent()) {
CommentBean comment = optionalComment.get();
if(comment.getUser().getId() == user.getId()) {
commentRepo.delete(comment);
return new ResponseEntity<>(true, HttpStatus.OK);
}else {
return new ResponseEntity<>(false, HttpStatus.I_AM_A_TEAPOT);
}
}else {
return new ResponseEntity<>(false, HttpStatus.NOT_FOUND);
}
}
}
解决方案
您security
缺少pom.xml
. 将以下内容添加到您的pom.xml
和更新的 Maven 项目中。它应该解决依赖关系。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
推荐阅读
- java - org.springframework.beans.BeanInstantiationException ,无法使用 Oracle 数据源
- pandas - 为什么包含多列的熊猫数据框说 10 的维度为 2?
- c++ - bq.o:server.cpp:(.text+0x20):在 VS Code 2020 中编译 tcp/ip 程序时未定义对 `__imp_socket' 的引用
- angular - Angular - 对文本框的值更改执行过滤器
- c - 尝试从链表中删除时,我丢失了整个列表,而不仅仅是一个 int
- sharepoint - Sharepoint 查找列表
- r - 按字母数字和数字参数聚合列
- google-cloud-platform - 如何使用 cloudarmor 为我的 gke 入口配置 DDoS?
- java - 构建签名APK时android studio中的gradle问题
- html - 我可以在不使用响应式的情况下解决此问题吗?