|
36 | 36 | import java.util.concurrent.Callable; |
37 | 37 |
|
38 | 38 | import static org.apache.shiro.env.BasicIniEnvironment.INI_REALM_NAME; |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
39 | 40 | import static org.easymock.EasyMock.createNiceMock; |
40 | 41 | import static org.junit.jupiter.api.Assertions.assertEquals; |
41 | 42 | import static org.junit.jupiter.api.Assertions.assertFalse; |
@@ -156,6 +157,8 @@ void testRunAs() { |
156 | 157 | //login as user1 |
157 | 158 | Subject subject = new Subject.Builder(sm).buildSubject(); |
158 | 159 | subject.login(new UsernamePasswordToken("user1", "user1")); |
| 160 | + // duplicate login, test for https://github.com/apache/shiro/issues/2704 |
| 161 | + subject.login(new UsernamePasswordToken("user1", "user1")); |
159 | 162 |
|
160 | 163 | assertFalse(subject.isRunAs()); |
161 | 164 | assertEquals("user1", subject.getPrincipal()); |
@@ -223,6 +226,36 @@ void testRunAs() { |
223 | 226 | LifecycleUtils.destroy(sm); |
224 | 227 | } |
225 | 228 |
|
| 229 | + @Test |
| 230 | + void sessionAttributesSurviveLoginSessionRotation() { |
| 231 | + Ini ini = new Ini(); |
| 232 | + Ini.Section users = ini.addSection("users"); |
| 233 | + users.put("user1", "user1,role1"); |
| 234 | + users.put("user2", "user2,role2"); |
| 235 | + users.put("user3", "user3,role3"); |
| 236 | + SecurityManager sm = new BasicIniEnvironment(ini).getSecurityManager(); |
| 237 | + Subject subject = new Subject.Builder(sm).buildSubject(); |
| 238 | + |
| 239 | + subject.login(new UsernamePasswordToken("user1", "user1")); |
| 240 | + subject.logout(); |
| 241 | + |
| 242 | + Session preLoginSession = subject.getSession(true); |
| 243 | + preLoginSession.setAttribute("tenantId", "ACME"); |
| 244 | + Serializable preLoginSessionId = preLoginSession.getId(); |
| 245 | + |
| 246 | + subject.login(new UsernamePasswordToken("user1", "user1")); |
| 247 | + assertThat(subject.isAuthenticated()).isTrue(); |
| 248 | + |
| 249 | + Session postLoginSession = subject.getSession(false); |
| 250 | + assertThat(postLoginSession).isNotNull(); |
| 251 | + |
| 252 | + assertThat(preLoginSessionId).as("session ID should change on login (session fixation protection)") |
| 253 | + .isNotEqualTo(postLoginSession.getId()); |
| 254 | + assertThat(postLoginSession.getAttribute("tenantId")) |
| 255 | + .as("session attributes set before login must survive session rotation") |
| 256 | + .isEqualTo("ACME"); |
| 257 | + } |
| 258 | + |
226 | 259 | @Test |
227 | 260 | void testToString() { |
228 | 261 | // given |
|
0 commit comments