Getting Started with WebFlux Applications
此部分涵盖了有关如何在响应式应用程序中将 Spring Security 与 Spring Boot 配合使用的最低设置。
This section covers the minimum setup for how to use Spring Security with Spring Boot in a reactive application.
可在https://github.com/spring-projects/spring-security-samples/tree/{gh-tag}/reactive/webflux/java/hello-security[我们的样例库]中找到已完成的应用程序。您可通过 clicking here下载一个最小的Reactive Spring Boot + Spring Security应用程序以供方便。 The completed application can be found in our samples repository. For your convenience, you can download a minimal Reactive Spring Boot + Spring Security application by clicking here. |
Updating Dependencies
你可以通过添加 spring-boot-starter-security
将 Spring Security 添加到你的 Spring Boot 项目。
You can add Spring Security to your Spring Boot project by adding 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'
Starting Hello Spring Security Boot
您现在可以使用Maven插件的`run`目标 run the Spring Boot application 。以下示例演示如何执行此操作(以及执行此操作后输出的开头):
You can now run the Spring Boot application by using the Maven Plugin’s run
goal.
The following example shows how to do so (and the beginning of the output from doing so):
-
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
...
Authenticating
您可以访问[role="bare"][role="bare"]http://localhost:8080/应用程序,该应用程序将把浏览器重定向到默认登录页面。您可以提供`user`默认用户名和记录到控制台的随机生成的密码。然后浏览器将转到最初请求的页面。
You can access the application at [role="bare"]http://localhost:8080/ which will redirect the browser to the default log in page. You can provide the default username of user
with the randomly generated password that is logged to the console. The browser is then taken to the orginally requested page.
要注销,您可以访问[role="bare"][role="bare"]http://localhost:8080/logout,然后确认您希望注销。
To log out you can visit [role="bare"]http://localhost:8080/logout and then confirming you wish to log out.
Spring Boot Auto Configuration
Spring Boot 自动添加了 Spring Security,它要求所有请求都经过认证。它还会生成一个带有随机生成密码的用户,该密码已记录到可用于使用表单或基本认证来进行认证的控制台。
Spring Boot automatically adds Spring Security which requires all requests be authenticated. It also generates a user with a randomly generated password that is logged to the console which can be used to authenticate using form or basic authentication.