|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.session.data.mongo.integration; |
| 17 | + |
| 18 | +import static org.assertj.core.api.AssertionsForClassTypes.*; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URI; |
| 22 | + |
| 23 | +import de.flapdoodle.embed.mongo.MongodExecutable; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import reactor.test.StepVerifier; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.data.mongodb.core.ReactiveMongoOperations; |
| 33 | +import org.springframework.data.mongodb.core.ReactiveMongoTemplate; |
| 34 | +import org.springframework.http.HttpHeaders; |
| 35 | +import org.springframework.http.MediaType; |
| 36 | +import org.springframework.http.ResponseEntity; |
| 37 | +import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; |
| 38 | +import org.springframework.security.config.web.server.ServerHttpSecurity; |
| 39 | +import org.springframework.security.core.userdetails.MapReactiveUserDetailsService; |
| 40 | +import org.springframework.security.core.userdetails.User; |
| 41 | +import org.springframework.security.web.server.SecurityWebFilterChain; |
| 42 | +import org.springframework.session.data.mongo.config.annotation.web.reactive.EnableMongoWebSession; |
| 43 | +import org.springframework.test.context.ContextConfiguration; |
| 44 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 45 | +import org.springframework.test.web.reactive.server.FluxExchangeResult; |
| 46 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 47 | +import org.springframework.util.SocketUtils; |
| 48 | +import org.springframework.web.bind.annotation.GetMapping; |
| 49 | +import org.springframework.web.bind.annotation.RestController; |
| 50 | +import org.springframework.web.reactive.config.EnableWebFlux; |
| 51 | +import org.springframework.web.reactive.function.BodyInserters; |
| 52 | + |
| 53 | +import com.mongodb.reactivestreams.client.MongoClient; |
| 54 | +import com.mongodb.reactivestreams.client.MongoClients; |
| 55 | + |
| 56 | +/** |
| 57 | + * @author Greg Turnquist |
| 58 | + */ |
| 59 | +@ExtendWith(SpringExtension.class) |
| 60 | +@ContextConfiguration |
| 61 | +public class MongoDbLogoutVerificationTest { |
| 62 | + |
| 63 | + @Autowired ApplicationContext ctx; |
| 64 | + |
| 65 | + WebTestClient client; |
| 66 | + |
| 67 | + @BeforeEach |
| 68 | + void setUp() { |
| 69 | + this.client = WebTestClient.bindToApplicationContext(this.ctx).build(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void logoutShouldDeleteOldSessionIdFromMongoDB() { |
| 74 | + |
| 75 | + // 1. `curl -i -v -X POST --data "username=admin&password=password" localhost:8080/login` - Save SESSION cookie and |
| 76 | + // use it it nex step as {cookie-value-1} |
| 77 | + |
| 78 | + FluxExchangeResult<String> loginResult = this.client.post().uri("/login") |
| 79 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) // |
| 80 | + .body(BodyInserters // |
| 81 | + .fromFormData("username", "admin") // |
| 82 | + .with("password", "password")) // |
| 83 | + .exchange() // |
| 84 | + .returnResult(String.class); |
| 85 | + |
| 86 | + assertThat(loginResult.getResponseHeaders().getLocation()).isEqualTo(URI.create("/")); |
| 87 | + |
| 88 | + String originalSessionId = loginResult.getResponseCookies().getFirst("SESSION").getValue(); |
| 89 | + |
| 90 | + // 2. `curl -i -L -v -X GET --cookie "SESSION=48eb6ab2-2c08-43b7-a303-46099bfef231" localhost:8080/hello` - response |
| 91 | + // status will be 200, body will be "HelloWorld" |
| 92 | + |
| 93 | + this.client.get().uri("/hello") // |
| 94 | + .cookie("SESSION", originalSessionId) // |
| 95 | + .exchange() // |
| 96 | + .expectStatus().isOk() // |
| 97 | + .returnResult(String.class).getResponseBody() // |
| 98 | + .as(StepVerifier::create) // |
| 99 | + .expectNext("HelloWorld") // |
| 100 | + .verifyComplete(); |
| 101 | + |
| 102 | + // 3. `curl -i -L -v -X POST --cookie "SESSION=48eb6ab2-2c08-43b7-a303-46099bfef231" localhost:8080/logout` - Save |
| 103 | + // SESSION cookie and use it it nex step as {cookie-value-2} |
| 104 | + |
| 105 | + String newSessionId = this.client.post().uri("/logout") // |
| 106 | + .cookie("SESSION", originalSessionId) // |
| 107 | + .exchange() // |
| 108 | + .expectStatus().isFound() // |
| 109 | + .returnResult(String.class) |
| 110 | + .getResponseCookies().getFirst("SESSION").getValue(); |
| 111 | + |
| 112 | + assertThat(newSessionId).isNotEqualTo(originalSessionId); |
| 113 | + |
| 114 | + // 4. `curl -i -L -v -X GET --cookie "SESSION=3b20200c-cf5e-4529-b3af-3c37ed365f5a" localhost:8080/hello` - response |
| 115 | + // status will be 302, body will be empty |
| 116 | + |
| 117 | + this.client.get().uri("/hello") // |
| 118 | + .cookie("SESSION", newSessionId) // |
| 119 | + .exchange() // |
| 120 | + .expectStatus().isFound() // |
| 121 | + .expectHeader().value(HttpHeaders.LOCATION, value -> assertThat(value).isEqualTo("/login")); |
| 122 | + |
| 123 | + // 5. `curl -i -L -v -X GET --cookie "SESSION=48eb6ab2-2c08-43b7-a303-46099bfef231" localhost:8080/hello` - response |
| 124 | + // status will be 200, body will be "HelloWorld", but it should be the same as step 4 |
| 125 | + |
| 126 | + this.client.get().uri("/hello") // |
| 127 | + .cookie("SESSION", originalSessionId) // |
| 128 | + .exchange() // |
| 129 | + .expectStatus().isFound() // |
| 130 | + .expectHeader().value(HttpHeaders.LOCATION, value -> assertThat(value).isEqualTo("/login")); |
| 131 | + } |
| 132 | + |
| 133 | + @RestController |
| 134 | + static class TestController { |
| 135 | + |
| 136 | + @GetMapping("/hello") |
| 137 | + public ResponseEntity<String> hello() { |
| 138 | + return ResponseEntity.ok("HelloWorld"); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + @EnableWebFluxSecurity |
| 144 | + static class SecurityConfig { |
| 145 | + |
| 146 | + @Bean |
| 147 | + public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { |
| 148 | + |
| 149 | + return http // |
| 150 | + .logout()// |
| 151 | + /**/.and() // |
| 152 | + .formLogin() // |
| 153 | + /**/.and() // |
| 154 | + .csrf().disable() // |
| 155 | + .authorizeExchange() // |
| 156 | + .anyExchange().authenticated() // |
| 157 | + /**/.and() // |
| 158 | + .build(); |
| 159 | + } |
| 160 | + |
| 161 | + @Bean |
| 162 | + public MapReactiveUserDetailsService userDetailsService() { |
| 163 | + |
| 164 | + return new MapReactiveUserDetailsService(User.withDefaultPasswordEncoder() // |
| 165 | + .username("admin") // |
| 166 | + .password("password") // |
| 167 | + .roles("USER,ADMIN") // |
| 168 | + .build()); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Configuration |
| 173 | + @EnableWebFlux |
| 174 | + @EnableMongoWebSession |
| 175 | + static class Config { |
| 176 | + |
| 177 | + private int embeddedMongoPort = SocketUtils.findAvailableTcpPort(); |
| 178 | + |
| 179 | + @Bean(initMethod = "start", destroyMethod = "stop") |
| 180 | + public MongodExecutable embeddedMongoServer() throws IOException { |
| 181 | + return MongoITestUtils.embeddedMongoServer(this.embeddedMongoPort); |
| 182 | + } |
| 183 | + |
| 184 | + @Bean |
| 185 | + public ReactiveMongoOperations mongoOperations(MongodExecutable embeddedMongoServer) { |
| 186 | + |
| 187 | + MongoClient mongo = MongoClients.create("mongodb://localhost:" + this.embeddedMongoPort); |
| 188 | + return new ReactiveMongoTemplate(mongo, "test"); |
| 189 | + } |
| 190 | + |
| 191 | + @Bean |
| 192 | + TestController controller() { |
| 193 | + return new TestController(); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments