繁体   English   中英

自定义登录页面的 403 错误 Spring 安全

[英]403 Error for Custom Login Page Spring Security

每当我尝试使用 Spring Security 登录到我的 spring 引导应用程序的主页时,都会收到 403 错误。 每当我使用 spring 安全的默认登录页面时,登录都不会引发 403 错误。

https://prnt.sc/svxyc8

下面是我的配置方法。

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

下面是我的文件结构的图片:

http://prntscr.com/svy9ly

下面是我的 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";
    }
}

数据库结构: http://prntscr.com/svya5d

登录.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>

您需要设置表单操作并使用正确的输入名称。 这是一个最小的例子:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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