简体   繁体   中英

403 Error for Custom Login Page Spring Security

I am getting a 403 error whenever I try to login in to the home page of my spring boot application using Spring Security. The login does not throw an 403 error whenever I use spring security's default login page.

https://prnt.sc/svxyc8

Below is my configure method.

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests()
    .antMatchers("/css/**").permitAll()
    .antMatchers().hasAnyRole("ADMIN","USER")
    .antMatchers("/").permitAll().and().formLogin().loginPage("/login");
}

Below is a picture of my file structure:

http://prntscr.com/svy9ly

Below is my Controller.

@Controller
public class AuthenticationController 
{
    /*
      Get request to allow us to see the login page. This page is
      stores in the static folder.
     */
    @GetMapping("/login")
    public String login()
    {
        return "login";
    }

    @GetMapping("/")
    public String index()
    {
        return "index";
    }
}

Database Structure: http://prntscr.com/svya5d

login.html

<!DOCTYPE html>
<html xmlns:th="www.thymeleaf.org">
    <!-- We need ThymeLeaf. This provides a rendering engine to allow Spring boot to know that there are templates for it to render -->
    <head>
    </head>

    <body>
        <form method="POST">
                <div class="form-row">
                        <div class="name">Username</div>
                        <div class="value">
                                <div class="input-group">
                                        <input class="input--style-5" type="text" name="userID">
                                </div>
                        </div>
                </div>
                <div class="form-row">
                        <div class="name">Password</div>
                        <div class="value">
                                <div class="input-group">
                                        <input class="input--style-5" type="password" name="userPW">
                                </div>
                        </div>
                </div>
                <div>
                        <button class="btn btn--radius-2 btn--red" type="submit">Login</button>
                </div>
                <a class="myLinkLog" href="register">Create an account</a>
        </form>
</html>

You need to set a form action and use the proper input names. This is a minimal example:

        <form th:action="@{/login}" method="post">

            <input type="text" name="username" />
            <input type="password" name="password" />

            <button type="submit" th:text="#{login}"></button>
        </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM