跳到主要内容
版本:7.0.2

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>

启动 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

...

身份验证

您可以通过访问 localhost:8080/ 来访问应用程序,浏览器将被重定向到默认的登录页面。您可以使用默认用户名 user 以及控制台输出的随机生成密码进行登录。登录后,浏览器将跳转至最初请求的页面。

要退出登录,您可以访问 localhost:8080/logout 并确认您希望退出登录。

Spring Boot 自动配置

Spring Boot 会自动集成 Spring Security,该安全框架要求所有请求都必须经过身份验证。同时,它会生成一个默认用户,其密码是随机生成的并会输出到控制台,您可以使用表单登录或基本认证的方式进行身份验证。