前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring5之WebFlux

Spring5之WebFlux

作者头像
叔牙
发布2020-11-19 17:13:31
2.5K0
发布2020-11-19 17:13:31
举报

Spring5之WebFlux

1.介绍

Spring WebFlux框架是Spring5的一部分,为Web应用程序提供响应式(反应式)编程支持。

在本篇文章中,我们将使用响应式Web组件RestController和WebClient创建一个小型的响应式REST应用程序,并且研究如何使用Spring Security保护我们的响应式端点。

2.Spring WebFlux框架

Spring WebFlux内部使用Reactor及其具体实现-Flux和Mono:

  • 基于注解的响应式组件
  • 功能路由和处理

在这里我们将重点介绍基于注解的响应式组件。

3.依赖管理

我们直接从spring-boot-starter-webflux依赖开始,实际上它引入了所有其他必需的依赖:

  • 用于基本的Spring Boot应用程序设置的spring-boot和spring-boot-starter
  • spring-webflux框架
  • 响应式流和reactor-netty等核心依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.0.5.RELEASE</version> </dependency>

最新的spring-boot-starter-webflux可以从Maven中心仓库下载。

4.响应式REST应用程序

我们现在使用Spring WebFlux构建一个非常简单的Reactive REST EmployeeManagement应用程序:

  • 使用一个简单的实体类-具有id和name字段的Employee
  • 使用RestController和WebClient构建REST API,以便发布和检索单个以及列表Employee资源
  • 使用WebFlux和Spring Security创建安全的响应式端点

5.响应式RestController

Spring WebFlux和Spring Web MVC框架一样支持基于注解的配置。

首先,在服务器端,我们创建一个带注解的控制器,用于发布我们的Employee响应流。

创建带注解的EmployeeController:

@RestController @RequestMapping("/employees") public class EmployeeReactiveController { private final EmployeeRepository employeeRepository; // constructor... }

EmployeeRepository可以是支持非阻塞响应流的任何数据库调用。

5.1:单一资源

在我们的控制器中创建一个发布单个Employee资源的端点:

@GetMapping("/{id}") private Mono<Employee> getEmployeeById(@PathVariable String id) { return employeeRepository.findEmployeeById(id); }

对于单个Employee资源,我们使用了Employee类型的Mono,因为它最多会提供1个元素。

5.2:集合资源

在我们的控制器中再添加一个端点,用于发布所有Employees的集合资源:

@GetMapping private Flux<Employee> getAllEmployees() { return employeeRepository.findAllEmployees(); }

6.响应式Web客户端

在Spring5中引入的WebClient是一个支持响应式流的非阻塞客户端。

在客户端,我们使用WebClient从EmployeeController中创建的端点检索数据。

创建一个简单的EmployeeWebClient:

public class EmployeeWebClient { WebClient client = WebClient.create("http://localhost:8080"); // ... }

这里我们使用其工厂方法create创建了一个WebClient,它将指向响应式url路径localhost:8080。

6.1:检索单个资源

从端点/employee/{id}获取Mono类型的单个资源:

Mono<Employee> employeeMono = client.get() .uri("/employees/{id}", "1") .retrieve() .bodyToMono(Employee.class); employeeMono.subscribe(System.out::println);

6.2:检索集合资源

类似的,从/employees端点检索Flux类型的集合资源:

Flux<Employee> employeeFlux = client.get() .uri("/employees") .retrieve() .bodyToFlux(Employee.class); employeeFlux.subscribe(System.out::println);

7.Spring WebFlux安全

我们可以使用Spring Security来保护我们的响应式端点。

假设EmployeeController中有一个新的端点,此端点更新Employee详细信息并返回更新的Employee。

由于这个接口允许用户修改现有员工信息,因此我们希望仅将此端点做权限管控,限制ADMIN角色用户才能修改。

为EmployeeController添加一个新方法:

@PostMapping("/update") private Mono<Employee> updateEmployee(@RequestBody Employee employee) { return employeeRepository.updateEmployee(employee); }

接着,为了限制对此方法的访问,让我们创建SecurityConfig并定义一些基于请求路径的规则以仅允许ADMIN用户:

@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角色的用户才能访问此端点并更新现有Employee信息。

最后,注解@EnableWebFluxSecurity添加了一些默认配置的Spring Security WebFlux支持。

总结

在本文中,我们通过创建一个小型的Reactive REST应用程序,研究了如何创建和使用Spring WebFlux框架支持的响应式Web组件。

我们学习了如何使用RestController和WebClient分别发布和使用响应式流,还研究了如何在Spring Security的帮助下创建安全的响应式端点。

除了响应式RestController和WebClient之外,WebFlux框架还支持响应式WebSocket和相应的WebSocketClient,用于响应式流的套接字样式流。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档