gpt4 book ai didi

java - 自定义登录页面 Spring Security 的 403 错误

转载 作者:行者123 更新时间:2023-12-01 16:21:24 24 4
gpt4 key购买 nike

每当我尝试使用 Spring Security 登录 Spring Boot 应用程序的主页时,都会收到 403 错误。每当我使用 Spring Security 的默认登录页面时,登录都不会抛出 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>

关于java - 自定义登录页面 Spring Security 的 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62264324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com