Skip to content

Commit b7120ac

Browse files
dkapilpivovarit
authored andcommitted
BAEL-1814 Guide to Spring Webflux (eugenp#4450)
* BAEL-1814 Guide to Spring Webflux -Added files for Employee reactive application -Updated pom.xml for Spring Security * BAEL-1814 Guide to Spring Webflux -Added EmployeeControllerTest -Updated method name in EmployeeController and corrected secured url in EmployeeWebSecurityConfig * BAEL-1814 Guide to spring webflux -Fixed security config, now only specific url prompts for authentication and not all endpoints -Removed @WithMockUser as it is not needed now * BAEL-1814 Guide To Webflux -Feedback incorporation * BAEL-1814 Spring Webflux Guide -Formatted coded for EmployeeWebSocketHandler. * Update and rename EmployeeControllerTest.java to EmployeeControllerUnitTest.java * BAEL-1814 Guide to spring webflux -Fixed EmployeeControllerUnitTest.java
1 parent f7a08a9 commit b7120ac

12 files changed

Lines changed: 387 additions & 1 deletion

‎spring-5-reactive/pom.xml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@
147147
<version>${project-reactor-test}</version>
148148
<scope>test</scope>
149149
</dependency>
150-
150+
<dependency>
151+
<groupId>org.springframework.boot</groupId>
152+
<artifactId>spring-boot-starter-security</artifactId>
153+
</dependency>
154+
<dependency>
155+
<groupId>org.springframework.security</groupId>
156+
<artifactId>spring-security-test</artifactId>
157+
<scope>test</scope>
158+
</dependency>
151159
</dependencies>
152160

153161
<build>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class Employee {
11+
12+
private String id;
13+
private String name;
14+
15+
// standard getters and setters
16+
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.reactive.HandlerMapping;
9+
import org.springframework.web.reactive.config.EnableWebFlux;
10+
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
11+
import org.springframework.web.reactive.socket.WebSocketHandler;
12+
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
13+
14+
@Configuration
15+
@EnableWebFlux
16+
public class EmployeeConfig {
17+
18+
@Bean
19+
public HandlerMapping handlerMapping() {
20+
Map<String, WebSocketHandler> map = new HashMap<>();
21+
map.put("/employee-feed", new EmployeeWebSocketHandler());
22+
23+
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
24+
mapping.setUrlMap(map);
25+
mapping.setOrder(10);
26+
return mapping;
27+
}
28+
29+
@Bean
30+
public WebSocketHandlerAdapter handlerAdapter() {
31+
return new WebSocketHandlerAdapter();
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
@RestController
14+
@RequestMapping("/employees")
15+
public class EmployeeController {
16+
17+
private EmployeeRepository employeeRepository;
18+
19+
public EmployeeController(EmployeeRepository employeeRepository) {
20+
this.employeeRepository = employeeRepository;
21+
}
22+
23+
@GetMapping("/{id}")
24+
private Mono<Employee> getEmployeeById(@PathVariable String id) {
25+
return employeeRepository.findEmployeeById(id);
26+
}
27+
28+
@GetMapping
29+
private Flux<Employee> getAllEmployees() {
30+
return employeeRepository.findAllEmployees();
31+
}
32+
33+
@PostMapping("/update")
34+
private Mono<Employee> updateEmployee(@RequestBody Employee employee) {
35+
return employeeRepository.updateEmployee(employee);
36+
}
37+
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class EmployeeCreationEvent {
9+
private String employeeId;
10+
private String creationTime;
11+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.stereotype.Repository;
7+
8+
import reactor.core.publisher.Flux;
9+
import reactor.core.publisher.Mono;
10+
11+
@Repository
12+
public class EmployeeRepository {
13+
14+
static Map<String,Employee> employeeData;
15+
16+
static Map<String,String> employeeAccessData;
17+
18+
static
19+
{
20+
employeeData = new HashMap<>();
21+
employeeData.put("1",new Employee("1","Employee 1"));
22+
employeeData.put("2",new Employee("2","Employee 2"));
23+
employeeData.put("3",new Employee("3","Employee 3"));
24+
employeeData.put("4",new Employee("4","Employee 4"));
25+
employeeData.put("5",new Employee("5","Employee 5"));
26+
employeeData.put("6",new Employee("6","Employee 6"));
27+
employeeData.put("7",new Employee("7","Employee 7"));
28+
employeeData.put("8",new Employee("8","Employee 8"));
29+
employeeData.put("9",new Employee("9","Employee 9"));
30+
employeeData.put("10",new Employee("10","Employee 10"));
31+
32+
employeeAccessData=new HashMap<>();
33+
employeeAccessData.put("1", "Employee 1 Access Key");
34+
employeeAccessData.put("2", "Employee 2 Access Key");
35+
employeeAccessData.put("3", "Employee 3 Access Key");
36+
employeeAccessData.put("4", "Employee 4 Access Key");
37+
employeeAccessData.put("5", "Employee 5 Access Key");
38+
employeeAccessData.put("6", "Employee 6 Access Key");
39+
employeeAccessData.put("7", "Employee 7 Access Key");
40+
employeeAccessData.put("8", "Employee 8 Access Key");
41+
employeeAccessData.put("9", "Employee 9 Access Key");
42+
employeeAccessData.put("10", "Employee 10 Access Key");
43+
}
44+
45+
public Mono<Employee> findEmployeeById(String id)
46+
{
47+
return Mono.just(employeeData.get(id));
48+
}
49+
50+
public Flux<Employee> findAllEmployees()
51+
{
52+
return Flux.fromIterable(employeeData.values());
53+
}
54+
55+
public Mono<Employee> updateEmployee(Employee employee)
56+
{
57+
Employee existingEmployee=employeeData.get(employee.getId());
58+
if(existingEmployee!=null)
59+
{
60+
existingEmployee.setName(employee.getName());
61+
}
62+
return Mono.just(existingEmployee);
63+
}
64+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class EmployeeSpringApplication {
8+
9+
public static void main(String[] args) {
10+
11+
SpringApplication.run(EmployeeSpringApplication.class, args);
12+
13+
EmployeeWebClient employeeWebClient = new EmployeeWebClient();
14+
employeeWebClient.consume();
15+
}
16+
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import org.springframework.web.reactive.function.client.WebClient;
4+
5+
import reactor.core.publisher.Flux;
6+
import reactor.core.publisher.Mono;
7+
8+
public class EmployeeWebClient {
9+
10+
WebClient client = WebClient.create("http://localhost:8080");
11+
12+
public void consume() {
13+
14+
Mono<Employee> employeeMono = client.get()
15+
.uri("/employees/{id}", "1")
16+
.retrieve()
17+
.bodyToMono(Employee.class);
18+
19+
employeeMono.subscribe(System.out::println);
20+
21+
Flux<Employee> employeeFlux = client.get()
22+
.uri("/employees")
23+
.retrieve()
24+
.bodyToFlux(Employee.class);
25+
26+
employeeFlux.subscribe(System.out::println);
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.http.HttpMethod;
5+
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
6+
import org.springframework.security.config.web.server.ServerHttpSecurity;
7+
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
8+
import org.springframework.security.core.userdetails.User;
9+
import org.springframework.security.core.userdetails.UserDetails;
10+
import org.springframework.security.web.server.SecurityWebFilterChain;
11+
12+
@EnableWebFluxSecurity
13+
public class EmployeeWebSecurityConfig {
14+
15+
@Bean
16+
public MapReactiveUserDetailsService userDetailsService() {
17+
UserDetails user = User.withDefaultPasswordEncoder()
18+
.username("admin")
19+
.password("password")
20+
.roles("ADMIN")
21+
.build();
22+
return new MapReactiveUserDetailsService(user);
23+
}
24+
25+
@Bean
26+
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
27+
http.csrf()
28+
.disable()
29+
.authorizeExchange()
30+
.pathMatchers(HttpMethod.POST, "/employees/update")
31+
.hasRole("ADMIN")
32+
.pathMatchers("/**")
33+
.permitAll()
34+
.and()
35+
.httpBasic();
36+
return http.build();
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.reactive.webflux;
2+
3+
import java.net.URI;
4+
5+
import org.springframework.web.reactive.socket.WebSocketMessage;
6+
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
7+
import org.springframework.web.reactive.socket.client.WebSocketClient;
8+
9+
public class EmployeeWebSocketClient {
10+
11+
public static void main(String[] args) {
12+
13+
WebSocketClient client = new ReactorNettyWebSocketClient();
14+
15+
client.execute(URI.create("ws://localhost:8080/employee-feed"), session -> session.receive()
16+
.map(WebSocketMessage::getPayloadAsText)
17+
.doOnNext(System.out::println)
18+
.then())
19+
.block(); // to subscribe and return the value
20+
}
21+
}

0 commit comments

Comments
 (0)