Spring WebFlux的使用指南
Spring WebFlux是spring5的一部分,它為web應(yīng)用程序提供反應(yīng)式編程支持。
在本教程中,我們將使用RestController和WebClient創(chuàng)建一個(gè)小型響應(yīng)式REST應(yīng)用程序。
我們還將研究如何使用Spring安全保護(hù)我們的反應(yīng)端點(diǎn)。
Spring-WebFlux框架Spring WebFlux在內(nèi)部使用Project Reactor及其發(fā)布者實(shí)現(xiàn)Flux和Mono。
新框架支持兩種編程模型:
基于注釋的反應(yīng)元件 功能路由和處理 依賴項(xiàng)讓我們從spring boot starter webflux依賴項(xiàng)開始,它包含所有其他必需的依賴項(xiàng):
spring boot和spring boot starter,用于基本的spring boot應(yīng)用程序設(shè)置 spring-webflux框架 reactor-core我們需要的反應(yīng)流,也需要reactor-netty<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.2.6.RELEASE</version></dependency>響應(yīng)式應(yīng)用
我們現(xiàn)在將使用Spring WebFlux構(gòu)建一個(gè)非常簡(jiǎn)單的REST EmployeeManagement應(yīng)用程序:
我們將使用一個(gè)簡(jiǎn)單的域模型-帶有id和name字段的Employee 我們將使用RestController構(gòu)建restapi,以將員工資源作為單個(gè)資源和集合發(fā)布 我們將使用WebClient構(gòu)建一個(gè)客戶端來檢索相同的資源 我們將使用WebFlux和Spring Security創(chuàng)建一個(gè)安全的被動(dòng)端點(diǎn) 響應(yīng)式RestControllerspringwebflux支持基于注釋的配置,方式與springwebmvc框架相同。
首先,在服務(wù)器上,我們創(chuàng)建一個(gè)帶注釋的控制器,它發(fā)布員工資源的反應(yīng)流。
讓我們創(chuàng)建帶注釋的EmployeeController:
@RestController@RequestMapping('/employees')public class EmployeeController { private final EmployeeRepository employeeRepository;// constructor...}
EmployeeRepository可以是任何支持非阻塞反應(yīng)流的數(shù)據(jù)存儲(chǔ)庫。
單一資源讓我們?cè)诳刂破髦袆?chuàng)建一個(gè)端點(diǎn),用于發(fā)布單個(gè)員工資源:
@GetMapping('/{id}')private Mono<Employee> getEmployeeById(@PathVariable String id) { return employeeRepository.findEmployeeById(id);}
我們?cè)贛ono中包裝一個(gè)Employee資源,因?yàn)槲覀冏疃喾祷匾粋€(gè)Employee。
集合資源我們還要添加一個(gè)端點(diǎn)來發(fā)布所有雇員的集合資源:
@GetMappingprivate Flux<Employee> getAllEmployees() { return employeeRepository.findAllEmployees();}
對(duì)于集合資源,我們使用類型為Employee的流量,因?yàn)樗?..n元素的發(fā)布者。
反應(yīng)式Web客戶端Spring5中引入的WebClient是一個(gè)支持反應(yīng)流的非阻塞客戶端。
我們可以使用WebClient創(chuàng)建一個(gè)客戶端,從EmployeeController提供的端點(diǎn)檢索數(shù)據(jù)。
讓我們創(chuàng)建一個(gè)簡(jiǎn)單的EmployeeWebClient:
public class EmployeeWebClient { WebClient client = WebClient.create('http://localhost:8080'); // ...}
在這里,我們使用工廠方法create創(chuàng)建了一個(gè)WebClient。它會(huì)指向localhost:8080,所以我們可以使用或相對(duì)的URL來調(diào)用這個(gè)客戶端實(shí)例。
檢索單個(gè)資源要從endpoint/employee/{id}檢索Mono類型的單個(gè)資源,請(qǐng)執(zhí)行以下操作:
Mono<Employee> employeeMono = client.get() .uri('/employees/{id}', '1') .retrieve() .bodyToMono(Employee.class);employeeMono.subscribe(System.out::println);檢索集合資源
類似地,要從endpoint/employees檢索Flux類型的集合資源,請(qǐng)執(zhí)行以下操作:
Flux<Employee> employeeFlux = client.get() .uri('/employees') .retrieve() .bodyToFlux(Employee.class);employeeFlux.subscribe(System.out::println);Spring WebFlux安全性
我們可以使用Spring Security來保護(hù)我們的反應(yīng)端點(diǎn)。
假設(shè)我們?cè)贓mployeeController中有一個(gè)新的端點(diǎn)。此端點(diǎn)更新員工詳細(xì)信息并發(fā)回更新的員工。
由于這允許用戶更改現(xiàn)有員工,因此我們希望僅將此端點(diǎn)限制為管理員角色用戶。
讓我們?yōu)镋mployeeController添加一個(gè)新方法:
@PostMapping('/update')private Mono<Employee> updateEmployee(@RequestBody Employee employee) { return employeeRepository.updateEmployee(employee);}
現(xiàn)在,為了限制對(duì)該方法的訪問,讓我們創(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(); }}
此配置將限制對(duì)/employees/update的訪問。因此,只有具有ADMIN角色的用戶才能訪問此端點(diǎn)并更新現(xiàn)有員工。
最后,注解@EnableWebFluxSecurity添加了一些默認(rèn)配置的Spring-Security-WebFlux支持。
結(jié)論在本文中,我們探討了如何創(chuàng)建和使用springwebflux框架支持的反應(yīng)式web組件。例如,我們構(gòu)建了一個(gè)小型的REST應(yīng)用程序。
除了Reactive RestController和WebClient之外,WebFlux框架還支持Reactive WebSocket和對(duì)應(yīng)的WebSocketClient,以進(jìn)行套接字樣式的Reactive流。
最后,在Github上提供了本文中使用的完整源代碼:https://github.com/eugenp/tutorials/tree/master/spring-5-reactive-security
以上就是Spring WebFlux的使用指南的詳細(xì)內(nèi)容,更多關(guān)于Spring WebFlux的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. SSM框架整合JSP中集成easyui前端ui項(xiàng)目開發(fā)示例詳解2. asp文件用什么軟件編輯3. 每日六道java新手入門面試題,通往自由的道路第二天4. 微信開發(fā) 網(wǎng)頁授權(quán)獲取用戶基本信息5. React中使用TS完成父組件調(diào)用子組件的操作方法6. android Web跳轉(zhuǎn)到app指定頁面并傳遞參數(shù)實(shí)例7. python中的class_static的@classmethod的巧妙用法8. PHP session反序列化漏洞超詳細(xì)講解9. Python中g(shù)lob庫實(shí)現(xiàn)文件名的匹配10. React基礎(chǔ)-JSX的本質(zhì)-虛擬DOM的創(chuàng)建過程實(shí)例分析
