-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add filter to force WebSession::save. #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ava/org/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.cloud.gateway.filter.factory; | ||
|
|
||
| import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
| import org.springframework.tuple.Tuple; | ||
| import org.springframework.web.server.WebSession; | ||
|
|
||
| /** | ||
| * Save the current {@link WebSession} before executing the rest of the {@link org.springframework.cloud.gateway.filter.GatewayFilterChain}. | ||
| * | ||
| * Filter is very useful for situation where the WebSession is lazy (e.g. Spring Session MongoDB) and making a remote call requires | ||
| * that {@link WebSession#save()} be called before the remote call is made. | ||
| * | ||
| * @author Greg Turnquist | ||
| */ | ||
| public class SaveSessionGatewayFilterFactory implements GatewayFilterFactory { | ||
|
|
||
| @Override | ||
| public GatewayFilter apply(Tuple args) { | ||
| return (exchange, chain) -> exchange.getSession() | ||
| .map(WebSession::save) | ||
| .then(chain.filter(exchange)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...rg/springframework/cloud/gateway/filter/factory/SaveSessionGatewayFilterFactoryTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.cloud.gateway.filter.factory; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.springframework.boot.SpringBootConfiguration; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.cloud.gateway.test.BaseWebClientTests; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.test.annotation.DirtiesContext; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.junit4.SpringRunner; | ||
| import org.springframework.web.server.WebSession; | ||
| import org.springframework.web.server.session.WebSessionManager; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.test.StepVerifier; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
| import static org.springframework.web.reactive.function.BodyExtractors.toMono; | ||
|
|
||
| /** | ||
| * @author Greg Turnquist | ||
| */ | ||
| @RunWith(SpringRunner.class) | ||
| @SpringBootTest(webEnvironment = RANDOM_PORT) | ||
| @DirtiesContext | ||
| @ActiveProfiles(profiles = "save-session-web-filter") | ||
| public class SaveSessionGatewayFilterFactoryTests extends BaseWebClientTests { | ||
|
|
||
| static WebSession mockWebSession = mock(WebSession.class); | ||
|
|
||
| @Test | ||
| public void webCallShouldTriggerWebSessionSaveAction() { | ||
|
|
||
| when(mockWebSession.getAttributes()).thenReturn(new HashMap<>()); | ||
| when(mockWebSession.save()).thenReturn(Mono.empty()); | ||
|
|
||
| Mono<Map> result = webClient.get() | ||
| .uri("/get") | ||
| .exchange() | ||
| .flatMap(response -> response.body(toMono(Map.class))); | ||
|
|
||
| StepVerifier.create(result) | ||
| .consumeNextWith(response -> {/* Don't care about data, just need to catch signal */}) | ||
| .expectComplete() | ||
| .verify(Duration.ofMinutes(10)); | ||
|
|
||
| verify(mockWebSession).save(); | ||
| } | ||
|
|
||
| @EnableAutoConfiguration | ||
| @SpringBootConfiguration | ||
| @Import(DefaultTestConfig.class) | ||
| static class TestConfig { | ||
|
|
||
| @Bean | ||
| WebSessionManager webSessionManager() { | ||
| return exchange -> Mono.just(mockWebSession); | ||
| } | ||
| } | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
spring-cloud-gateway-core/src/test/resources/application-save-session-web-filter.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| test: | ||
| hostport: httpbin.org:80 | ||
| uri: lb://testservice | ||
|
|
||
| spring: | ||
| cloud: | ||
| gateway: | ||
| routes: | ||
| - id: save_session_test | ||
| uri: ${test.uri} | ||
| predicates: | ||
| - Path=/get | ||
| filters: | ||
| - SaveSession |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gregturn
In this part, does it mean that adding
SaveSessionfilter is very important and it have to be applied, or adding this filter is dangerous and it should be avoided?