|
45 | 45 | import org.springframework.security.core.context.SecurityContextHolder;
|
46 | 46 | import org.springframework.session.FindByIndexNameSessionRepository;
|
47 | 47 | import org.springframework.session.MapSession;
|
| 48 | +import org.springframework.session.SaveMode; |
48 | 49 | import org.springframework.session.config.SessionRepositoryCustomizer;
|
49 | 50 | import org.springframework.session.jdbc.JdbcIndexedSessionRepository.JdbcSession;
|
50 | 51 | import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
|
@@ -819,6 +820,33 @@ void saveNewSessionAttributeConcurrently() {
|
819 | 820 | }
|
820 | 821 | }
|
821 | 822 |
|
| 823 | + @Test |
| 824 | + void savesWhenSaveModeIsOnGetAttributeAndANewAttributeIsSetAndGet() { |
| 825 | + try { |
| 826 | + // Prerequisite: SaveMode.ON_GET_ATTRIBUTE is enabled |
| 827 | + this.repository.setSaveMode(SaveMode.ON_GET_ATTRIBUTE); |
| 828 | + |
| 829 | + // Step 1: Save session |
| 830 | + JdbcSession initialSession = this.repository.createSession(); |
| 831 | + this.repository.save(initialSession); |
| 832 | + |
| 833 | + // Step 2: Load session again, set an attribute and immediately get it again |
| 834 | + JdbcSession session = this.repository.findById(initialSession.getId()); |
| 835 | + String attributeName = "attribute1"; |
| 836 | + String attributeValue = "value1"; |
| 837 | + session.setAttribute(attributeName, attributeValue); |
| 838 | + session.getAttribute(attributeName); |
| 839 | + this.repository.save(session); |
| 840 | + |
| 841 | + // Step3: Load session 3rd time, verify that attribute is set correctly |
| 842 | + assertThat((String) this.repository.findById(initialSession.getId()).getAttribute(attributeName)) |
| 843 | + .isEqualTo(attributeValue); |
| 844 | + } |
| 845 | + finally { |
| 846 | + this.repository.setSaveMode(SaveMode.ON_SET_ATTRIBUTE); |
| 847 | + } |
| 848 | + } |
| 849 | + |
822 | 850 | private String getSecurityName() {
|
823 | 851 | return this.context.getAuthentication().getName();
|
824 | 852 | }
|
|
0 commit comments