Skip to content

Commit 2404312

Browse files
Thoughtscriptpivovarit
authored andcommitted
BAEL-1909 (eugenp#5366)
1 parent 7d7cae7 commit 2404312

9 files changed

Lines changed: 226 additions & 0 deletions

File tree

‎spring-5-reactive/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1414
- [Spring Webflux and CORS](http://www.baeldung.com/spring-webflux-cors)
1515
- [Handling Errors in Spring WebFlux](http://www.baeldung.com/spring-webflux-errors)
1616
- [Server-Sent Events in Spring](https://www.baeldung.com/spring-server-sent-events)
17+
- [A Guide to Spring Session Reactive Support: WebSession](https://www.baeldung.com/a-guide-to-spring-session-reactive-support-websession/)

‎spring-5-reactive/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@
7676
<scope>test</scope>
7777
</dependency>
7878

79+
<!-- Spring WebFlux WebSession -->
80+
<dependency>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-starter-webflux</artifactId>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.springframework.boot</groupId>
86+
<artifactId>spring-boot-starter-data-redis</artifactId>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-starter-security</artifactId>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.session</groupId>
94+
<artifactId>spring-session-core</artifactId>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.springframework.session</groupId>
98+
<artifactId>spring-session-data-redis</artifactId>
99+
</dependency>
100+
79101
<dependency>
80102
<groupId>org.apache.commons</groupId>
81103
<artifactId>commons-collections4</artifactId>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.websession;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
@SpringBootApplication
8+
@ComponentScan(basePackages = {"com.baeldung"})
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.websession.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
6+
import org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession;
7+
8+
@Configuration
9+
//@EnableRedisWebSession
10+
public class RedisConfig {
11+
/**
12+
@Bean
13+
public LettuceConnectionFactory redisConnectionFactory() {
14+
return new LettuceConnectionFactory();
15+
}
16+
*/
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.websession.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.session.ReactiveMapSessionRepository;
6+
import org.springframework.session.ReactiveSessionRepository;
7+
import org.springframework.session.config.annotation.web.server.EnableSpringWebSession;
8+
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
@Configuration
12+
@EnableSpringWebSession
13+
public class SessionConfig {
14+
15+
@Bean
16+
public ReactiveSessionRepository reactiveSessionRepository() {
17+
return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.websession.configuration;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.ApplicationContextAware;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.reactive.config.EnableWebFlux;
7+
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
8+
import org.springframework.web.reactive.config.WebFluxConfigurer;
9+
10+
@Configuration
11+
@EnableWebFlux
12+
public class WebFluxConfig implements ApplicationContextAware, WebFluxConfigurer {
13+
14+
private ApplicationContext context;
15+
16+
@Override
17+
public void setApplicationContext(ApplicationContext context) {
18+
this.context = context;
19+
}
20+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.websession.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
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.crypto.bcrypt.BCryptPasswordEncoder;
11+
import org.springframework.security.crypto.password.PasswordEncoder;
12+
import org.springframework.security.web.server.SecurityWebFilterChain;
13+
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
14+
15+
@Configuration
16+
@EnableWebFluxSecurity
17+
public class WebFluxSecurityConfig {
18+
19+
@Bean
20+
public MapReactiveUserDetailsService userDetailsService() {
21+
UserDetails admin = User
22+
.withUsername("admin")
23+
.password(encoder().encode("password"))
24+
.roles("ADMIN")
25+
.build();
26+
27+
UserDetails user = User
28+
.withUsername("user")
29+
.password(encoder().encode("password"))
30+
.roles("USER")
31+
.build();
32+
33+
return new MapReactiveUserDetailsService(admin, user);
34+
}
35+
36+
@Bean
37+
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
38+
http
39+
.authorizeExchange()
40+
.anyExchange().authenticated()
41+
.and()
42+
.httpBasic()
43+
.securityContextRepository(new WebSessionServerSecurityContextRepository())
44+
.and()
45+
.formLogin();
46+
47+
http
48+
.csrf().disable();
49+
50+
return http.build();
51+
52+
}
53+
54+
@Bean
55+
public PasswordEncoder encoder() {
56+
return new BCryptPasswordEncoder();
57+
}
58+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.websession.controller;
2+
3+
import com.baeldung.websession.transfer.CustomResponse;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.server.WebSession;
8+
import reactor.core.publisher.Mono;
9+
10+
@RestController
11+
public class SessionController {
12+
13+
@GetMapping("/websession/test")
14+
public Mono<CustomResponse> testWebSessionByParam(
15+
@RequestParam(value = "id") int id,
16+
@RequestParam(value = "note") String note,
17+
WebSession session) {
18+
19+
session.getAttributes().put("id", id);
20+
session.getAttributes().put("note", note);
21+
22+
CustomResponse r = new CustomResponse();
23+
r.setId((int) session.getAttributes().get("id"));
24+
r.setNote((String) session.getAttributes().get("note"));
25+
26+
return Mono.just(r);
27+
}
28+
29+
@GetMapping("/websession")
30+
public Mono<CustomResponse> getSession(WebSession session) {
31+
32+
session.getAttributes().putIfAbsent("id", 0);
33+
session.getAttributes().putIfAbsent("note", "Howdy Cosmic Spheroid!");
34+
35+
CustomResponse r = new CustomResponse();
36+
r.setId((int) session.getAttributes().get("id"));
37+
r.setNote((String) session.getAttributes().get("note"));
38+
39+
return Mono.just(r);
40+
}
41+
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.websession.transfer;
2+
3+
public class CustomResponse {
4+
5+
private int id;
6+
private String note;
7+
8+
public CustomResponse() {}
9+
10+
public CustomResponse(int id, String note) {
11+
this.id = id;
12+
this.note = note;
13+
}
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getNote() {
24+
return note;
25+
}
26+
27+
public void setNote(String note) {
28+
this.note = note;
29+
}
30+
31+
}

0 commit comments

Comments
 (0)