Spring Security - 无法将默认登录页面更改为自定义
Posted
技术标签:
【中文标题】Spring Security - 无法将默认登录页面更改为自定义【英文标题】:Spring Security - cannot to change default login page to custom 【发布时间】:2021-12-31 15:58:53 【问题描述】:我正在学习 Spring Security。
现在我想自定义登录页面,但我无法将默认页面更改为自定义。 在安全配置中,我编写了 loginPage("/login"),创建了控制器,但这些步骤对我没有帮助。
我的配置类:
package configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception
http
.authorizeRequests()
.antMatchers("/resources/templates/**", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.disable();
控制器:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AuthController
@GetMapping("/login")
public String getLoginPage()
return "login";
login.html 路径:resources/templates/login.html
login.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" media="all"
href="../static/style.css" th:href="@style.css"/>
</head>
<body>
<header align="center">
<ul>
<a class="logo" href="home.html"><span>L</span><span>O</span><span>G</span><span>O</span></a>
</ul>
</header>
<div class="content_div">
<div class="div_c">
<div class="content_reg">
<form method="post">
<h1 class="form_title">Registration</h1>
<div class="form_group">
<input type="email" class="form_input" placeholder=" ">
<label class="form_label">E-mail</label>
</div>
<div class="form_group">
<input type="text" class="form_input" placeholder=" ">
<label class="form_label">Login</label>
</div>
<div class="form_group">
<input type="password" class="form_input" placeholder=" ">
<label class="form_label">Password</label>
</div>
<button type="submit" class="form_button">Register</button>
</form>
</div>
</div>
<div class="div_c">
<form action="/login" class="login_form" method="post">
<h1 class="form_title">Log in</h1>
<div class="form_group">
<input type="text" class="form_input" placeholder=" ">
<label class="form_label">Login</label>
</div>
<div class="form_group">
<input type="password" class="form_input" placeholder=" ">
<label class="form_label">Password</label>
</div>
<button type="submit" class="form_button">Log in</button>
</form>
</div>
</div>
</body>
</html>
如果您能帮我解决这个问题 - 我将非常感激。
【问题讨论】:
您能否澄清一下您所看到的以及这与预期行为有何不同?直接访问localhost:8080/login
会发生什么?
【参考方案1】:
您的登录页面似乎在路径“/login”上运行良好,如果您希望将未经身份验证的请求重定向到 /login,您应该从 WebSecurityConfig
中删除 disable()
【讨论】:
不,我想将默认登录页面更改为自定义页面,但由于某种原因,默认页面一直在打开。虽然,看起来,我把它改成了自定义的。【参考方案2】:你需要告诉 Spring 在哪里寻找你的静态文件:
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/resources/templates/
您可以在https://www.baeldung.com/spring-mvc-static-resources#2-custom-directories 阅读更多相关信息。
【讨论】:
【参考方案3】:你需要像这样删除 .loginpage 更改你的代码
.formLogin()
.defaultSuccessUrl("/authenticated")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.csrf()
.disable();
【讨论】:
以上是关于Spring Security - 无法将默认登录页面更改为自定义的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Spring Security 中用自定义替换标准登录表单
使用 Spring Security 默认登录将用户放入 HttpSession 并进行身份验证