我正在练习Spring Boot。我在我的项目中添加了一个安全类,但从那时起,我就不能到达主页面"/"了,在我添加安全类之前,它还能工作,但现在我得到了一个白标错误页面。
下面是我的配置类。
package com.teszt.thymeleaftest.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/event**").authenticated()
.antMatchers("/").anonymous()
.and()
.formLogin()
.loginPage("/login/login")
.loginProcessingUrl("/logIn")
.permitAll()
.and()
.csrf().disable();
}
}
这里是我的控制器。
package com.teszt.thymeleaftest.controller;
import com.teszt.thymeleaftest.dao.MemberRepository;
import com.teszt.thymeleaftest.entity.Members;
import com.teszt.thymeleaftest.entity.Users;
import com.teszt.thymeleaftest.service.MembersService;
import com.teszt.thymeleaftest.service.UsersService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.transaction.Transactional;
import java.util.List;
@Controller
@RequestMapping("/login")
@AllArgsConstructor
public class MemberController {
MembersService membersService;
UsersService usersService;
@GetMapping("/members-list")
public String showList(Model theModel){
//get employees from db
List<Members> theMembers = membersService.findAll();
//add the spring model
theModel.addAttribute("members", theMembers);
return "/login/members-list";
}
@GetMapping("/login")
public String showLoginPage(){
return "/login/login";
}
@GetMapping("/signUp")
public String showSignUpPage(Model theModel){
Members theMember = new Members();
theModel.addAttribute("member", theMember);
return "/login/signUp";
}
@PostMapping("/save")
public String saveMember(@ModelAttribute("member") Members theMember, ModelMap modelMap){
Members tempMember = membersService.findByEmail(theMember.getEmail());
if(tempMember != null){
modelMap.addAttribute("error", "Email is already exist!");
return "/login/signUp";
}else{
membersService.save(theMember);
//prevent duplication
return "redirect:/login/login";
}
}
@PostMapping("/logIn")
public String logInUser(@RequestParam("email") String email, @RequestParam("password") String password, ModelMap modelMap){
Members theMember = membersService.findByEmail(email);
if(theMember.getPassword().equals(password)){
modelMap.addAttribute("firstName", theMember.getFirstName());
modelMap.addAttribute("lastName", theMember.getLastName());
return "/login/welcomePage";
}else{
modelMap.addAttribute("msg", "Invalid email or password!");
}
return "/login/login";
}
@GetMapping("/member")
public String member(@RequestParam("userId") int theId, Model theModel) {
//get the employee from the service
Users theUser = usersService.findById(theId);
//set employee as a model attribute to pre-populate the form
theModel.addAttribute("member", theUser);
return "login/member";
}
@PostMapping("/saveMember")
@Transactional
public String saveNewMember(@ModelAttribute("member") Users theUser, ModelMap modelMap, @RequestParam("password")String password){
Members theMember = new Members(theUser.getFirstName(), theUser.getLastName(), theUser.getEmail(), password);
Members tempMember = membersService.findByEmail(theMember.getEmail());
if(tempMember != null){
modelMap.addAttribute("error", "Email is already exist");
return "/login/member";
}else{
//save the member
membersService.save(theMember);
//delete the user because he/she is a member now
usersService.deleteByEmail(theUser.getEmail());
//prevent duplication
return "redirect:/";
}
}
@GetMapping("/showMemberFormForUpdate")
public String updateMember(@RequestParam("memberId") int theId, Model theModel){
//get the employee from the service
Members theMember = membersService.findById(theId);
//set employee as a model attribute to pre-populate the form
theModel.addAttribute("member", theMember);
return "login/updateMember";
}
@PostMapping("/update")
public String updateMember(@ModelAttribute("member") Members theMember, ModelMap modelMap){
Members tempMember = membersService.findByEmail(theMember.getEmail());
/*if(tempMember != null){
modelMap.addAttribute("error", "Email is already exist!");
return "/login/signUp";
}else{
membersService.save(theMember);
//prevent duplication
return "redirect:/login/login";
}*/
membersService.save(theMember);
//prevent duplication
return "redirect:/login/login";
}
@GetMapping("/delete")
public String deleteMember(@RequestParam("memberId") int theId){
//delete the employee
membersService.deleteById(theId);
//redirect to index
return "redirect:/";
}
}
这里是我的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.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.teszt</groupId>
<artifactId>thymeleaftest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>thymeleaftest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>16</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.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
</dependencies>
</project>
这是一个简短的版本,但我所有的依赖都在这里。
我希望有人能帮助我。
StackOverflow:java - Cannot reach the main page in Spring Boot - Stack Overflow