无法在spring boot中访问主页面

vs91vp4v  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(115)

我在练习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>

这是我的pom的一个简短版本,但我的所有依赖项都在这里
我希望有人能帮助我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题