原文: http://zetcode.com/springboot/loginpage/

Spring Boot 登录页面教程显示了如何使用默认登录页面。 Spring Security 默认情况下会保护所有 HTTP 端点。 用户必须以默认的 HTTP 形式登录。

为了启用 Spring Boot 安全性,我们将spring-boot-starter-security添加到依赖项中。

Spring Boot 登录页面示例

以下示例显示了如何在 Spring Boot 应用中设置简单的登录页面。

  1. pom.xml
  2. src
  3. ├───main
  4. ├───java
  5. └───com
  6. └───zetcode
  7. Application.java
  8. └───controller
  9. MyController.java
  10. └───resources
  11. application.properties
  12. └───test
  13. └───java

这是 Spring Boot 应用的项目结构。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>springbootloginpage</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>11</maven.compiler.source>
  14. <maven.compiler.target>11</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.5.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-security</artifactId>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. </plugin>
  37. </plugins>
  38. </build>
  39. </project>

这是 Maven 构建文件。 我们有网络和安全性的入门者。

resources/application.properties

  1. spring.main.banner-mode=off
  2. logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n

application.properties文件中,我们关闭 Spring Boot 横幅并配置控制台日志记录模式。

com/zetcode/controller/MyController.java

  1. package com.zetcode.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class MyController {
  6. @GetMapping("/")
  7. public String home() {
  8. return "This is home page";
  9. }
  10. }

我们有一个简单的主页。

我们运行该应用并导航到localhost:8080。 我们被重定向到http://localhost:8080/login页面。

  1. ...
  2. 17-06-2019 17:48:45 [main] INFO org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.getOrDeducePassword -
  3. Using generated security password: df7ce50b-abae-43a1-abe1-0e17fd81a454
  4. ...

在控制台中,我们可以看到为默认用户user生成的密码。 这些凭据将提供给认证表单。

Spring Boot 登录页面教程 - 图1

图:登录表单

Spring 使用 Bootstrap 定义 UI。

  1. spring.security.user.name = admin
  2. spring.security.user.password = s$cret

通过这两个选项,我们可以拥有一个新的用户名和密码。 使用这些设置将自动生成的用户关闭。

在本教程中,我们使用了默认的登录表单。 您可能也对相关教程感兴趣: Spring Boot @Lazy教程Java 教程或列出 Spring Boot 教程