Skip to content

Commit b9521b6

Browse files
quaffrwinch
authored andcommitted
Introduce CompositeHttpSessionIdResolver
1 parent 6fd52ca commit b9521b6

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2014-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+
17+
package org.springframework.session.web.http;
18+
19+
import java.util.List;
20+
import java.util.stream.Stream;
21+
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import jakarta.servlet.http.HttpServletResponse;
24+
25+
/**
26+
* Composite {@link HttpSessionIdResolver} implementation that iterates over a given
27+
* collection of delegate {@link HttpSessionIdResolver} instances.
28+
*
29+
* @author Yanming Zhou
30+
*/
31+
public class CompositeHttpSessionIdResolver implements HttpSessionIdResolver {
32+
33+
private final HttpSessionIdResolver[] resolvers;
34+
35+
public CompositeHttpSessionIdResolver(HttpSessionIdResolver... resolvers) {
36+
this.resolvers = resolvers;
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
@Override
43+
public List<String> resolveSessionIds(HttpServletRequest request) {
44+
return Stream.of(this.resolvers)
45+
.flatMap((resolver) -> resolver.resolveSessionIds(request).stream())
46+
.distinct()
47+
.toList();
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
@Override
54+
public void setSessionId(HttpServletRequest request, HttpServletResponse response, String sessionId) {
55+
for (HttpSessionIdResolver resolver : this.resolvers) {
56+
resolver.setSessionId(request, response, sessionId);
57+
}
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
@Override
64+
public void expireSession(HttpServletRequest request, HttpServletResponse response) {
65+
for (HttpSessionIdResolver resolver : this.resolvers) {
66+
resolver.expireSession(request, response);
67+
}
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2014-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+
17+
package org.springframework.session.web.http;
18+
19+
import java.util.Collections;
20+
import java.util.UUID;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.mock.web.MockHttpServletRequest;
26+
import org.springframework.mock.web.MockHttpServletResponse;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link CompositeHttpSessionIdResolver}.
32+
*
33+
* @author Yanming Zhou
34+
*/
35+
class CompositeHttpSessionIdResolverTests {
36+
37+
private static final String HEADER_X_AUTH_TOKEN = "X-Auth-Token";
38+
39+
private static final String HEADER_AUTHENTICATION_INFO = "Authentication-Info";
40+
41+
private MockHttpServletRequest request;
42+
43+
private MockHttpServletResponse response;
44+
45+
private CompositeHttpSessionIdResolver resolver;
46+
47+
@BeforeEach
48+
void setup() {
49+
this.request = new MockHttpServletRequest();
50+
this.response = new MockHttpServletResponse();
51+
this.resolver = new CompositeHttpSessionIdResolver(HeaderHttpSessionIdResolver.xAuthToken(),
52+
HeaderHttpSessionIdResolver.authenticationInfo());
53+
}
54+
55+
@Test
56+
void getRequestedSessionIdNull() {
57+
assertThat(this.resolver.resolveSessionIds(this.request)).isEmpty();
58+
}
59+
60+
@Test
61+
void resolveSessionIdsByXAuthToken() {
62+
String sessionId = UUID.randomUUID().toString();
63+
this.request.addHeader(HEADER_X_AUTH_TOKEN, sessionId);
64+
assertThat(this.resolver.resolveSessionIds(this.request)).isEqualTo(Collections.singletonList(sessionId));
65+
}
66+
67+
@Test
68+
void resolveSessionIdsByAuthenticationInfo() {
69+
String sessionId = UUID.randomUUID().toString();
70+
this.request.addHeader(HEADER_AUTHENTICATION_INFO, sessionId);
71+
assertThat(this.resolver.resolveSessionIds(this.request)).isEqualTo(Collections.singletonList(sessionId));
72+
}
73+
74+
@Test
75+
void onNewSession() {
76+
String sessionId = UUID.randomUUID().toString();
77+
this.resolver.setSessionId(this.request, this.response, sessionId);
78+
assertThat(this.response.getHeader(HEADER_X_AUTH_TOKEN)).isEqualTo(sessionId);
79+
assertThat(this.response.getHeader(HEADER_AUTHENTICATION_INFO)).isEqualTo(sessionId);
80+
}
81+
82+
@Test
83+
void onDeleteSession() {
84+
this.resolver.expireSession(this.request, this.response);
85+
assertThat(this.response.getHeader(HEADER_X_AUTH_TOKEN)).isEmpty();
86+
assertThat(this.response.getHeader(HEADER_AUTHENTICATION_INFO)).isEmpty();
87+
}
88+
89+
}

0 commit comments

Comments
 (0)