对于最新的稳定版本,请使用 Spring Security 6.5.3! |
WebFlux 应用程序入门
本节介绍如何在响应式应用程序中将 Spring Security 与 Spring Boot 一起使用的最低设置。
完整的应用程序可以在我们的示例存储库中找到。 为方便起见,您可以单击此处下载最小的响应式 Spring Boot + Spring Security 应用程序。 |
更新依赖项
您可以通过添加spring-boot-starter-security
.
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
implementation 'org.springframework.boot:spring-boot-starter-security'
启动 Hello Spring Security Boot
您现在可以使用 Maven 插件的run
目标。
以下示例显示了如何执行此作(以及执行此作的输出的开头):
运行 Spring Boot 应用程序
-
Maven
-
Gradle
$ ./mvnw spring-boot:run
...
INFO 23689 --- [ restartedMain] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: 8e557245-73e2-4286-969a-ff57fe326336
...
$ ./gradlew bootRun
...
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,这需要对所有请求进行身份验证。它还生成一个用户,该密码将随机生成,该密码记录到控制台,可用于使用表单或基本身份验证进行身份验证。