跳到主要内容

开始使用 WebFlux 应用程序

DeepSeek V3 中英对照 Getting Started Getting Started with WebFlux Applications

本节介绍了在响应式应用程序中使用 Spring Security 和 Spring Boot 所需的最低配置。

备注

完整的应用程序可以在我们的示例仓库中找到。为了方便起见,你可以通过点击这里下载一个最小化的 Reactive Spring Boot + Spring Security 应用程序。

更新依赖

你可以通过添加 spring-boot-starter-security 来将 Spring Security 添加到你的 Spring Boot 项目中。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
xml

开始 Hello Spring Security Boot

你现在可以使用 Maven 插件的 run 目标来运行 Spring Boot 应用程序。以下示例展示了如何执行此操作(以及执行后的输出开头部分):

$ ./mvnw spring-boot:run
...
INFO 23689 --- [ restartedMain] .s.s.UserDetailsServiceAutoConfiguration :

Using generated security password: 8e557245-73e2-4286-969a-ff57fe326336

...
bash

认证

你可以在 localhost:8080/ 访问该应用程序,浏览器将重定向到默认的登录页面。你可以使用默认的用户名 user 和随机生成的密码(该密码会记录在控制台中)进行登录。登录成功后,浏览器将跳转到最初请求的页面。

要注销登录,你可以访问 localhost:8080/logout,然后确认你希望注销。

Spring Boot 自动配置

Spring Boot 自动添加了 Spring Security,这要求所有请求都必须经过身份验证。它还会生成一个带有随机生成密码的用户,该密码会记录到控制台,可以使用表单或基本身份验证进行身份验证。