Spring WebFlux的使用指南
Spring WebFlux是spring5的一部分,它為web應用程序提供反應式編程支持。
在本教程中,我們將使用RestController和WebClient創(chuàng)建一個小型響應式REST應用程序。
我們還將研究如何使用Spring安全保護我們的反應端點。
Spring-WebFlux框架
Spring WebFlux在內部使用Project Reactor及其發(fā)布者實現(xiàn)Flux和Mono。
新框架支持兩種編程模型:
- 基于注釋的反應元件
- 功能路由和處理
依賴項
讓我們從spring boot starter webflux依賴項開始,它包含所有其他必需的依賴項:
- spring boot和spring boot starter,用于基本的spring boot應用程序設置
- spring-webflux框架
- reactor-core我們需要的反應流,也需要reactor-netty
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.2.6.RELEASE</version> </dependency>
響應式應用
我們現(xiàn)在將使用Spring WebFlux構建一個非常簡單的REST EmployeeManagement應用程序:
- 我們將使用一個簡單的域模型-帶有id和name字段的Employee
- 我們將使用RestController構建restapi,以將員工資源作為單個資源和集合發(fā)布
- 我們將使用WebClient構建一個客戶端來檢索相同的資源
- 我們將使用WebFlux和Spring Security創(chuàng)建一個安全的被動端點
響應式RestController
springwebflux支持基于注釋的配置,方式與springwebmvc框架相同。
首先,在服務器上,我們創(chuàng)建一個帶注釋的控制器,它發(fā)布員工資源的反應流。
讓我們創(chuàng)建帶注釋的EmployeeController
:
@RestController @RequestMapping("/employees") public class EmployeeController { private final EmployeeRepository employeeRepository; // constructor... }
EmployeeRepository可以是任何支持非阻塞反應流的數(shù)據(jù)存儲庫。
單一資源
讓我們在控制器中創(chuàng)建一個端點,用于發(fā)布單個員工資源:
@GetMapping("/{id}") private Mono<Employee> getEmployeeById(@PathVariable String id) { return employeeRepository.findEmployeeById(id); }
我們在Mono中包裝一個Employee資源,因為我們最多返回一個Employee。
集合資源
我們還要添加一個端點來發(fā)布所有雇員的集合資源:
@GetMapping private Flux<Employee> getAllEmployees() { return employeeRepository.findAllEmployees(); }
對于集合資源,我們使用類型為Employee的流量,因為它是0..n元素的發(fā)布者。
反應式Web客戶端
Spring5中引入的WebClient是一個支持反應流的非阻塞客戶端。
我們可以使用WebClient創(chuàng)建一個客戶端,從EmployeeController提供的端點檢索數(shù)據(jù)。
讓我們創(chuàng)建一個簡單的EmployeeWebClient:
public class EmployeeWebClient { WebClient client = WebClient.create("http://localhost:8080"); // ... }
在這里,我們使用工廠方法create創(chuàng)建了一個WebClient。它會指向localhost:8080,所以我們可以使用或相對的URL來調用這個客戶端實例。
檢索單個資源
要從endpoint/employee/{id}檢索Mono類型的單個資源,請執(zhí)行以下操作:
Mono<Employee> employeeMono = client.get() .uri("/employees/{id}", "1") .retrieve() .bodyToMono(Employee.class); employeeMono.subscribe(System.out::println);
檢索集合資源
類似地,要從endpoint/employees檢索Flux類型的集合資源,請執(zhí)行以下操作:
Flux<Employee> employeeFlux = client.get() .uri("/employees") .retrieve() .bodyToFlux(Employee.class); employeeFlux.subscribe(System.out::println);
Spring WebFlux安全性
我們可以使用Spring Security來保護我們的反應端點。
假設我們在EmployeeController中有一個新的端點。此端點更新員工詳細信息并發(fā)回更新的員工。
由于這允許用戶更改現(xiàn)有員工,因此我們希望僅將此端點限制為管理員角色用戶。
讓我們?yōu)镋mployeeController添加一個新方法:
@PostMapping("/update") private Mono<Employee> updateEmployee(@RequestBody Employee employee) { return employeeRepository.updateEmployee(employee); }
現(xiàn)在,為了限制對該方法的訪問,讓我們創(chuàng)建SecurityConfig并定義一些基于路徑的規(guī)則以僅允許管理員用戶:
@EnableWebFluxSecurity public class EmployeeWebSecurityConfig { // ... @Bean public SecurityWebFilterChain springSecurityFilterChain( ServerHttpSecurity http) { http.csrf().disable() .authorizeExchange() .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN") .pathMatchers("/**").permitAll() .and() .httpBasic(); return http.build(); } }
此配置將限制對/employees/update的訪問。因此,只有具有ADMIN角色的用戶才能訪問此端點并更新現(xiàn)有員工。
最后,注解@EnableWebFluxSecurity添加了一些默認配置的Spring-Security-WebFlux支持。
結論
在本文中,我們探討了如何創(chuàng)建和使用springwebflux框架支持的反應式web組件。例如,我們構建了一個小型的REST應用程序。
除了Reactive RestController和WebClient之外,WebFlux框架還支持Reactive WebSocket和對應的WebSocketClient,以進行套接字樣式的Reactive流。
最后,在Github上提供了本文中使用的完整源代碼:https://github.com/eugenp/tutorials/tree/master/spring-5-reactive-security
以上就是Spring WebFlux的使用指南的詳細內容,更多關于Spring WebFlux的使用的資料請關注腳本之家其它相關文章!
相關文章
Scheduled如何會在上次任務執(zhí)行完才會執(zhí)行下次任務
這篇文章主要介紹了Scheduled如何會在上次任務執(zhí)行完才會執(zhí)行下次任務問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
數(shù)據(jù)庫是編程中重要的一部分,它囊括了數(shù)據(jù)操作,數(shù)據(jù)持久化等各方面。在每一門編程語言中都占有相當大的比例。本次,小編以MySQL為例,使用mvc編程思想,給大家講解下javaweb對數(shù)據(jù)庫的操作2017-02-02Java實現(xiàn)將容器 Map中的內容保存到數(shù)組
這篇文章主要介紹了Java實現(xiàn)將容器 Map中的內容保存到數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09