Java Spring引导安全身份验证[无法找到此本地主机页:HTTP错误404]错误
希望你们做得很好 我正在为我的应用程序实现spring安全性。我给了两个角色,分别是“管理员”和“员工”。如果用户具有管理员或员工角色,则他们在编辑或删除记录时应进行身份验证和授权。但当前,当具有管理员角色的用户尝试删除记录时,它应该请求身份验证,但不是直接请求,并抛出错误消息:找不到此本地主机页:HTTP错误404 注意:它根本不要求管理员和员工角色的身份验证和授权。它只是抛出一个HTTP404错误 谁能帮我解决这个问题我做错了什么 我尝试过此解决方案,但对我无效: 这是我的控制器:Java Spring引导安全身份验证[无法找到此本地主机页:HTTP错误404]错误,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,希望你们做得很好 我正在为我的应用程序实现spring安全性。我给了两个角色,分别是“管理员”和“员工”。如果用户具有管理员或员工角色,则他们在编辑或删除记录时应进行身份验证和授权。但当前,当具有管理员角色的用户尝试删除记录时,它应该请求身份验证,但不是直接请求,并抛出错误消息:找不到此本地主机页:HTTP错误404 注意:它根本不要求管理员和员工角色的身份验证和授权。它只是抛出一个HTTP404错误 谁能帮我解决这个问题我做错了什么 我尝试过此解决方案,但对我无效: 这是我的控制器: pack
package com.project.ems.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.project.ems.entity.Employee;
import com.project.ems.model.EmployeeModel;
import com.project.ems.repository.EmployeeRepository;
@Controller
public class EmployeeController {
@Autowired
EmployeeRepository repository;
@GetMapping("/index")
public String getIndexPage() {
return "index";
}
@GetMapping("/addEmployee")
public String getAddEmployeePage(Model model) {
EmployeeModel empModel=new EmployeeModel();
model.addAttribute("empModel", empModel);
return "addEmployee";
}
@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("empModel")EmployeeModel empModel, Model model) {
Employee e = new Employee();
BeanUtils.copyProperties(empModel, e);
boolean flag=repository.existsById(e.getEmpno());
if(flag==true) {
model.addAttribute("message", "Employee with given empno already exist");
}
else {
repository.save(e);
model.addAttribute("message", "Employee is added to Database");
}
return "index";
}
@GetMapping("/listEmployees")
public String listEmployees(Model model) {
List<Employee> empList= repository.findAll();
List<EmployeeModel> empModelList=new ArrayList<>();
empList.forEach(e -> {
EmployeeModel emodel=new EmployeeModel();
BeanUtils.copyProperties(e, emodel);
empModelList.add(emodel);
});
model.addAttribute("empModelList", empModelList);
return "employeesList";
}
@GetMapping("/editEmployee")
public String editEmployeePage(@RequestParam("id") int empno, Model model) {
Employee e = repository.findById(empno).get();
EmployeeModel emodel=new EmployeeModel();
BeanUtils.copyProperties(e, emodel);
model.addAttribute("emodel",emodel);
return "editEmployee";
}
@PostMapping("/updateEmployee")
public String updateEmployee(@ModelAttribute("emodel") EmployeeModel emodel) {
Employee e=new Employee();
BeanUtils.copyProperties(emodel, e);
repository.saveAndFlush(e);
return "redirect:listEmployees";
}
@GetMapping("/deleteEmployee")
public String deleteEmployee(@RequestParam("id") int empno) {
repository.deleteById(empno);
return "redirect:listEmployees";
}
}
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>ems</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ems</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的POM.XML:
package com.project.ems.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.project.ems.entity.Employee;
import com.project.ems.model.EmployeeModel;
import com.project.ems.repository.EmployeeRepository;
@Controller
public class EmployeeController {
@Autowired
EmployeeRepository repository;
@GetMapping("/index")
public String getIndexPage() {
return "index";
}
@GetMapping("/addEmployee")
public String getAddEmployeePage(Model model) {
EmployeeModel empModel=new EmployeeModel();
model.addAttribute("empModel", empModel);
return "addEmployee";
}
@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("empModel")EmployeeModel empModel, Model model) {
Employee e = new Employee();
BeanUtils.copyProperties(empModel, e);
boolean flag=repository.existsById(e.getEmpno());
if(flag==true) {
model.addAttribute("message", "Employee with given empno already exist");
}
else {
repository.save(e);
model.addAttribute("message", "Employee is added to Database");
}
return "index";
}
@GetMapping("/listEmployees")
public String listEmployees(Model model) {
List<Employee> empList= repository.findAll();
List<EmployeeModel> empModelList=new ArrayList<>();
empList.forEach(e -> {
EmployeeModel emodel=new EmployeeModel();
BeanUtils.copyProperties(e, emodel);
empModelList.add(emodel);
});
model.addAttribute("empModelList", empModelList);
return "employeesList";
}
@GetMapping("/editEmployee")
public String editEmployeePage(@RequestParam("id") int empno, Model model) {
Employee e = repository.findById(empno).get();
EmployeeModel emodel=new EmployeeModel();
BeanUtils.copyProperties(e, emodel);
model.addAttribute("emodel",emodel);
return "editEmployee";
}
@PostMapping("/updateEmployee")
public String updateEmployee(@ModelAttribute("emodel") EmployeeModel emodel) {
Employee e=new Employee();
BeanUtils.copyProperties(emodel, e);
repository.saveAndFlush(e);
return "redirect:listEmployees";
}
@GetMapping("/deleteEmployee")
public String deleteEmployee(@RequestParam("id") int empno) {
repository.deleteById(empno);
return "redirect:listEmployees";
}
}
<?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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>ems</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ems</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
你能出示日志吗?我已经添加了查询,请看一下。