Spring WebFlux的使用指南
Spring WebFlux是spring5的一部分,它為web應用程序提供反應式編程支持。
在本教程中,我們將使用RestController和WebClient創(chuàng)建一個小型響應式REST應用程序。
我們還將研究如何使用Spring安全保護我們的反應端點。
Spring-WebFlux框架Spring WebFlux在內部使用Project Reactor及其發(fā)布者實現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>響應式應用
我們現在將使用Spring WebFlux構建一個非常簡單的REST EmployeeManagement應用程序:
我們將使用一個簡單的域模型-帶有id和name字段的Employee 我們將使用RestController構建restapi,以將員工資源作為單個資源和集合發(fā)布 我們將使用WebClient構建一個客戶端來檢索相同的資源 我們將使用WebFlux和Spring Security創(chuàng)建一個安全的被動端點 響應式RestControllerspringwebflux支持基于注釋的配置,方式與springwebmvc框架相同。
首先,在服務器上,我們創(chuàng)建一個帶注釋的控制器,它發(fā)布員工資源的反應流。
讓我們創(chuàng)建帶注釋的EmployeeController:
@RestController@RequestMapping('/employees')public class EmployeeController { private final EmployeeRepository employeeRepository;// constructor...}
EmployeeRepository可以是任何支持非阻塞反應流的數據存儲庫。
單一資源讓我們在控制器中創(chuàng)建一個端點,用于發(fā)布單個員工資源:
@GetMapping('/{id}')private Mono<Employee> getEmployeeById(@PathVariable String id) { return employeeRepository.findEmployeeById(id);}
我們在Mono中包裝一個Employee資源,因為我們最多返回一個Employee。
集合資源我們還要添加一個端點來發(fā)布所有雇員的集合資源:
@GetMappingprivate Flux<Employee> getAllEmployees() { return employeeRepository.findAllEmployees();}
對于集合資源,我們使用類型為Employee的流量,因為它是0..n元素的發(fā)布者。
反應式Web客戶端Spring5中引入的WebClient是一個支持反應流的非阻塞客戶端。
我們可以使用WebClient創(chuàng)建一個客戶端,從EmployeeController提供的端點檢索數據。
讓我們創(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ā)回更新的員工。
由于這允許用戶更改現有員工,因此我們希望僅將此端點限制為管理員角色用戶。
讓我們?yōu)镋mployeeController添加一個新方法:
@PostMapping('/update')private Mono<Employee> updateEmployee(@RequestBody Employee employee) { return employeeRepository.updateEmployee(employee);}
現在,為了限制對該方法的訪問,讓我們創(chuàng)建SecurityConfig并定義一些基于路徑的規(guī)則以僅允許管理員用戶:
@EnableWebFluxSecuritypublic 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角色的用戶才能訪問此端點并更新現有員工。
最后,注解@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的使用的資料請關注好吧啦網其它相關文章!
相關文章: