希望你们做得很好!
我正在为我的应用程序实现Spring Security 。我给了两个角色,分别是“管理员”和“员工”。如果用户具有管理员或员工角色,则他们在编辑或删除记录时应进行身份验证和授权。但当前,当具有管理员角色的用户尝试删除记录时,它应该请求身份验证,但不是直接请求,并抛出错误消息:找不到此本地主机页:http错误404
注意:它根本不要求管理员和员工角色的身份验证和授权。它只是抛出一个HTTP404错误。
谁能帮我解决这个问题我做错了什么?
我已经尝试过这个解决方案,但没有成功:SpringBootStarter安全性未进行身份验证
这是我的控制器:
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";
}
}
这是我的安全配置
package com.project.ems.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class MyApplicationSecurityCofig extends WebSecurityConfigurerAdapter {
@Autowired
BCryptPasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().
antMatchers("/delete**").
hasAnyRole("ADMIN").
anyRequest().
permitAll().
and().httpBasic().
and().csrf().disable();
}
@Autowired
public void configGlobal(AuthenticationManagerBuilder builder) throws Exception {
builder.inMemoryAuthentication().withUser("Naresh").password(passwordEncoder.encode("p@ssword")).roles("ADMIN")
.and().withUser("Suresh").password(passwordEncoder.encode("123456")).roles("Employee");
}
}
以下是我的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.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>
日志
2021-06-02 22:53:42.378[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.project.ems.EmsApplication [0;39m [2m:[0;39m Starting EmsApplication using Java 15.0.2 on Naresh with PID 12132 (D:\SpringGuy\ems\target\classes started by Naresh in D:\SpringGuy\ems)
[2m2021-06-02 22:53:42.380[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.project.ems.EmsApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-06-02 22:53:42.954[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2021-06-02 22:53:42.997[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 35 ms. Found 1 JPA repository interfaces.
[2m2021-06-02 22:53:43.427[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 2121 (http)
[2m2021-06-02 22:53:43.436[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2021-06-02 22:53:43.436[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.44]
[2m2021-06-02 22:53:43.689[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.jasper.servlet.TldScanner [0;39m [2m:[0;39m At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[2m2021-06-02 22:53:43.698[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/ems] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2021-06-02 22:53:43.698[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1276 ms
[2m2021-06-02 22:53:43.826[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2021-06-02 22:53:44.168[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2021-06-02 22:53:44.207[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2021-06-02 22:53:44.257[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.Version [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 5.4.29.Final
[2m2021-06-02 22:53:44.420[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.hibernate.annotations.common.Version [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
[2m2021-06-02 22:53:44.538[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.hibernate.dialect.Dialect [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
[2m2021-06-02 22:53:45.039[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[2m2021-06-02 22:53:45.046[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2021-06-02 22:53:45.260[0;39m [33m WARN[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[2m2021-06-02 22:53:45.584[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.s.web.DefaultSecurityFilterChain [0;39m [2m:[0;39m Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@204a02a4, org.springframework.security.web.context.SecurityContextPersistenceFilter@b889cb6, org.springframework.security.web.header.HeaderWriterFilter@7aaf6bfd, org.springframework.security.web.authentication.logout.LogoutFilter@5dd2ea0a, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@206b959c, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@54c37dab, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@705d72f0, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4777f71e, org.springframework.security.web.session.SessionManagementFilter@726a5e6a, org.springframework.security.web.access.ExceptionTranslationFilter@1c06f19c, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@54a5799f]
[2m2021-06-02 22:53:45.679[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.s.concurrent.ThreadPoolTaskExecutor [0;39m [2m:[0;39m Initializing ExecutorService 'applicationTaskExecutor'
[2m2021-06-02 22:53:45.760[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.w.s.WelcomePageHandlerMapping [0;39m [2m:[0;39m Adding welcome page template: index
[2m2021-06-02 22:53:45.888[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 2121 (http) with context path '/ems'
[2m2021-06-02 22:53:45.895[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.project.ems.EmsApplication [0;39m [2m:[0;39m Started EmsApplication in 3.844 seconds (JVM running for 4.596)
[2m2021-06-02 22:53:52.524[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[nio-2121-exec-2][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/ems] [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2021-06-02 22:53:52.525[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[nio-2121-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2021-06-02 22:53:52.526[0;39m [32m INFO[0;39m [35m12132[0;39m [2m---[0;39m [2m[nio-2121-exec-2][0;39m [36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 1 ms
暂无答案!
目前还没有任何答案,快来回答吧!