微信登录

表单登录 - 表单登录配置 - 配置表单登录页面

Java - Web - Spring 《表单登录 - 表单登录配置 - 配置表单登录页面》

一、引言

在 Web 应用开发中,用户登录是一个非常常见且重要的功能。Spring 框架为我们提供了强大的安全支持,使得实现表单登录变得相对简单。本文将详细介绍如何在 Spring 项目中配置表单登录页面,包括必要的依赖、配置步骤以及示例代码。

二、项目准备

2.1 添加依赖

首先,我们需要在 pom.xml 中添加 Spring Security 的依赖。以下是一个基本的 Maven 依赖示例:

  1. <dependencies>
  2. <!-- Spring Web -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!-- Spring Security -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-security</artifactId>
  11. </dependency>
  12. </dependencies>

2.2 创建 Spring Boot 项目

可以使用 Spring Initializr(https://start.spring.io/) 快速创建一个 Spring Boot 项目,选择上述依赖后下载并导入到 IDE 中。

三、配置表单登录

3.1 创建自定义登录页面

src/main/resources/templates 目录下创建一个名为 login.html 的文件,内容如下:

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Login Page</title>
  5. </head>
  6. <body>
  7. <h1>Login</h1>
  8. <form action="#" th:action="@{/login}" th:method="post">
  9. <label for="username">Username:</label>
  10. <input type="text" id="username" name="username" required><br><br>
  11. <label for="password">Password:</label>
  12. <input type="password" id="password" name="password" required><br><br>
  13. <input type="submit" value="Login">
  14. </form>
  15. <p th:if="${param.error}" style="color: red;">Invalid username or password.</p>
  16. <p th:if="${param.logout}" style="color: green;">You have been logged out successfully.</p>
  17. </body>
  18. </html>

这个页面包含一个简单的表单,用于输入用户名和密码,并提交到 /login 路径。同时,它还处理了登录失败和登出成功的提示信息。

3.2 配置 Spring Security

创建一个配置类来配置 Spring Security,以下是示例代码:

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.web.SecurityFilterChain;
  6. @Configuration
  7. @EnableWebSecurity
  8. public class SecurityConfig {
  9. @Bean
  10. public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  11. http
  12. .authorizeRequests()
  13. .antMatchers("/login").permitAll()
  14. .anyRequest().authenticated()
  15. .and()
  16. .formLogin()
  17. .loginPage("/login")
  18. .defaultSuccessUrl("/home")
  19. .permitAll()
  20. .and()
  21. .logout()
  22. .permitAll();
  23. return http.build();
  24. }
  25. }

在这个配置类中,我们做了以下几件事情:

  • 允许所有用户访问 /login 页面。
  • 要求所有其他请求都需要进行身份验证。
  • 指定自定义的登录页面为 /login
  • 登录成功后默认重定向到 /home 页面。
  • 允许所有用户进行登出操作。

3.3 创建主页和控制器

src/main/resources/templates 目录下创建一个名为 home.html 的文件,内容如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Home Page</title>
  5. </head>
  6. <body>
  7. <h1>Welcome to the Home Page!</h1>
  8. <a href="/logout">Logout</a>
  9. </body>
  10. </html>

创建一个控制器来处理 /home 请求:

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. @Controller
  4. public class HomeController {
  5. @GetMapping("/home")
  6. public String home() {
  7. return "home";
  8. }
  9. }

四、运行项目

启动 Spring Boot 应用程序,访问 http://localhost:8080/login,你将看到自定义的登录页面。输入用户名和密码进行登录,如果登录成功,将重定向到主页;如果登录失败,将显示错误信息。

五、总结

步骤 操作 说明
1 添加依赖 pom.xml 中添加 Spring Web 和 Spring Security 依赖
2 创建自定义登录页面 templates 目录下创建 login.html 文件
3 配置 Spring Security 创建 SecurityConfig 类,指定登录页面和访问规则
4 创建主页和控制器 templates 目录下创建 home.html 文件,并创建 HomeController 处理 /home 请求

通过以上步骤,我们成功地在 Spring 项目中配置了自定义的表单登录页面。Spring Security 为我们提供了便捷的方式来实现用户认证和授权,让我们能够专注于业务逻辑的开发。希望本文对你有所帮助!