# WebFlux 应用程序入门

本节介绍了如何在反应性应用程序中使用 Spring 安全性和 Spring 引导的最小设置。

可以找到已完成的应用程序在我们的样品库中 (opens new window)
为了你的方便,你可以通过点击这里 (opens new window)下载一个最小的反应式 Spring 启动 + Spring 安全应用程序。

# 更新依赖项

你可以通过添加spring-boot-starter-security将 Spring 安全性添加到 Spring 引导项目中。

Maven

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

Gradle

    implementation 'org.springframework.boot:spring-boot-starter-security'

# 启动 Hello Spring 安全启动

你现在可以通过使用 Maven 插件的run目标run the Spring Boot application (opens new window)。下面的示例展示了如何这样做(以及这样做产生的输出的开始):

例 1。运行 Spring 启动应用程序

Maven

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

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

...

Gradle

$ ./gradlew bootRun
...
INFO 23689 --- [  restartedMain] .s.s.UserDetailsServiceAutoConfiguration :

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

...

# 认证

你可以通过http://localhost:8080/ (opens new window)访问应用程序,该应用程序将把浏览器重定向到默认的登录页面。你可以使用随机生成的密码提供user的默认用户名,该密码已登录到控制台。然后将浏览器带到原始请求的页面。

要注销,你可以访问http://localhost:8080/logout (opens new window),然后确认你希望注销。

# Spring 引导自动配置

Spring 启动会自动添加 Spring 安全性,这需要对所有请求进行身份验证。它还使用随机生成的密码生成用户,该密码被记录到控制台,该控制台可以使用 Form 或 Basic 身份验证来进行身份验证。

反应性应用X.509 认证