Skip to content

Commit 63f706d

Browse files
committed
Fix Hazelcast session with flush mode immediate
Closes gh-1921
1 parent beb7b33 commit 63f706d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2014-2021 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.hazelcast;
18+
19+
import java.util.Map;
20+
21+
import com.hazelcast.core.HazelcastInstance;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
29+
import org.springframework.security.core.Authentication;
30+
import org.springframework.security.core.authority.AuthorityUtils;
31+
import org.springframework.security.core.context.SecurityContext;
32+
import org.springframework.security.core.context.SecurityContextHolder;
33+
import org.springframework.session.FlushMode;
34+
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
35+
import org.springframework.test.context.ContextConfiguration;
36+
import org.springframework.test.context.junit.jupiter.SpringExtension;
37+
import org.springframework.test.context.web.WebAppConfiguration;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Integration tests for {@link HazelcastIndexedSessionRepository} using embedded
43+
* topology, with flush mode set to immediate.
44+
*
45+
* @author Eleftheria Stein
46+
*/
47+
@ExtendWith(SpringExtension.class)
48+
@ContextConfiguration
49+
@WebAppConfiguration
50+
class FlushImmediateHazelcastIndexedSessionRepositoryITests {
51+
52+
@Autowired
53+
private HazelcastIndexedSessionRepository repository;
54+
55+
@Test
56+
void createSessionWithSecurityContextAndFindByPrincipalName() {
57+
String username = "saves-" + System.currentTimeMillis();
58+
59+
HazelcastIndexedSessionRepository.HazelcastSession session = this.repository.createSession();
60+
String sessionId = session.getId();
61+
62+
Authentication authentication = new UsernamePasswordAuthenticationToken(username, "password",
63+
AuthorityUtils.createAuthorityList("ROLE_USER"));
64+
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
65+
securityContext.setAuthentication(authentication);
66+
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
67+
68+
this.repository.save(session);
69+
70+
Map<String, HazelcastIndexedSessionRepository.HazelcastSession> findByPrincipalName = this.repository
71+
.findByPrincipalName(username);
72+
73+
assertThat(findByPrincipalName).hasSize(1);
74+
assertThat(findByPrincipalName.keySet()).containsOnly(sessionId);
75+
76+
this.repository.deleteById(sessionId);
77+
}
78+
79+
@EnableHazelcastHttpSession(flushMode = FlushMode.IMMEDIATE)
80+
@Configuration
81+
static class HazelcastSessionConfig {
82+
83+
@Bean
84+
HazelcastInstance hazelcastInstance() {
85+
return HazelcastITestUtils.embeddedHazelcastServer();
86+
}
87+
88+
}
89+
90+
}

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/HazelcastIndexedSessionRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ public void setAttribute(String attributeName, Object attributeValue) {
441441
.resolveIndexesFor(this);
442442
String principal = (attributeValue != null) ? indexes.get(PRINCIPAL_NAME_INDEX_NAME) : null;
443443
this.delegate.setAttribute(PRINCIPAL_NAME_INDEX_NAME, principal);
444+
this.delta.put(PRINCIPAL_NAME_INDEX_NAME, principal);
444445
}
445446
flushImmediateIfNecessary();
446447
}

0 commit comments

Comments
 (0)