Skip to content

Commit 42831fc

Browse files
committed
Add Reactive OTT Sample
Closes gh-326
1 parent 371fe2a commit 42831fc

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

reactive/webflux/java/authentication/one-time-token/magic-link/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ java {
1111
}
1212

1313
repositories {
14-
mavenLocal()
1514
mavenCentral()
1615
maven { url "https://repo.spring.io/milestone" }
1716
maven { url "https://repo.spring.io/snapshot" }

reactive/webflux/java/authentication/one-time-token/magic-link/src/main/java/org/example/magiclink/MagicLinkOneTimeTokenGenerationSuccessHandler.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ public Mono<Void> handle(ServerWebExchange exchange, OneTimeToken oneTimeToken)
4848
.path("/login/ott")
4949
.queryParam("token", oneTimeToken.getTokenValue());
5050
String magicLink = builder.toUriString();
51-
builder
52-
.replacePath(null)
53-
.replaceQuery(null)
54-
.path("/ott/sent");
51+
builder.replacePath(null).replaceQuery(null).path("/ott/sent");
5552
String redirectLink = builder.toUriString();
56-
return this.mailSender.send("[email protected]", "Your Spring Security One Time Token",
57-
"Use the following link to sign in into the application: " + magicLink)
53+
return this.mailSender
54+
.send("[email protected]", "Your Spring Security One Time Token",
55+
"Use the following link to sign in into the application: " + magicLink)
5856
.then(this.redirectStrategy.sendRedirect(exchange, URI.create(redirectLink)));
5957
}
58+
6059
}

reactive/webflux/java/authentication/one-time-token/magic-link/src/main/java/org/example/magiclink/SecurityConfig.java

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.security.config.Customizer;
23-
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2423
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
2524
import org.springframework.security.config.web.server.ServerHttpSecurity;
2625
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;

reactive/webflux/java/authentication/one-time-token/magic-link/src/test/java/org/example/magiclink/MagicLinkApplicationTests.java

+43-19
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,37 @@
2424
import org.junit.jupiter.api.extension.RegisterExtension;
2525

2626
import org.springframework.beans.factory.annotation.Autowired;
27-
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
27+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
2828
import org.springframework.boot.test.context.SpringBootTest;
29-
import org.springframework.test.web.servlet.MockMvc;
29+
import org.springframework.test.web.reactive.server.WebTestClient;
30+
import org.springframework.web.reactive.function.BodyInserters;
3031
import org.springframework.web.util.UriComponents;
3132
import org.springframework.web.util.UriComponentsBuilder;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
34-
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
35-
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
36-
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
37-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
38-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
39-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35+
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
4036

41-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
42-
@AutoConfigureMockMvc
37+
@SpringBootTest
38+
@AutoConfigureWebTestClient(timeout = "30s")
4339
class MagicLinkApplicationTests {
4440

4541
@RegisterExtension
4642
static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.SMTP);
4743

4844
@Autowired
49-
MockMvc mockMvc;
45+
WebTestClient web;
5046

5147
@Test
5248
void ottLoginWhenUserExistsThenSendEmailAndAuthenticate() throws Exception {
53-
this.mockMvc.perform(post("/ott/generate").param("username", "user").with(csrf()))
54-
.andExpectAll(status().isFound(), redirectedUrl("/ott/sent"));
49+
this.web.mutateWith(csrf())
50+
.post()
51+
.uri("/ott/generate")
52+
.body(BodyInserters.fromFormData("username", "user"))
53+
.exchange()
54+
.expectStatus()
55+
.isFound()
56+
.expectHeader()
57+
.location("/ott/sent");
5558

5659
greenMail.waitForIncomingEmail(1);
5760
MimeMessage receivedMessage = greenMail.getReceivedMessages()[0];
@@ -62,19 +65,40 @@ void ottLoginWhenUserExistsThenSendEmailAndAuthenticate() throws Exception {
6265

6366
assertThat(token).isNotEmpty();
6467

65-
this.mockMvc.perform(post("/login/ott").param("token", token).with(csrf()))
66-
.andExpectAll(status().isFound(), redirectedUrl("/"), authenticated());
68+
this.web.mutateWith(csrf())
69+
.post()
70+
.uri("/login/ott")
71+
.body(BodyInserters.fromFormData("token", token))
72+
.exchange()
73+
.expectStatus()
74+
.isFound()
75+
.expectHeader()
76+
.location("/");
6777
}
6878

6979
@Test
7080
void ottLoginWhenInvalidTokenThenFails() throws Exception {
71-
this.mockMvc.perform(post("/ott/generate").param("username", "user").with(csrf()))
72-
.andExpectAll(status().isFound(), redirectedUrl("/ott/sent"));
81+
this.web.mutateWith(csrf())
82+
.post()
83+
.uri("/ott/generate")
84+
.body(BodyInserters.fromFormData("username", "user"))
85+
.exchange()
86+
.expectStatus()
87+
.isFound()
88+
.expectHeader()
89+
.location("/ott/sent");
7390

7491
String token = "1234;";
7592

76-
this.mockMvc.perform(post("/login/ott").param("token", token).with(csrf()))
77-
.andExpectAll(status().isFound(), redirectedUrl("/login?error"), unauthenticated());
93+
this.web.mutateWith(csrf())
94+
.post()
95+
.uri("/login/ott")
96+
.body(BodyInserters.fromFormData("token", token))
97+
.exchange()
98+
.expectStatus()
99+
.isFound()
100+
.expectHeader()
101+
.location("/login?error");
78102
}
79103

80104
}

0 commit comments

Comments
 (0)