|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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 | + * http://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.security.web.authentication.session; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import javax.servlet.http.HttpSession; |
| 25 | + |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 29 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 30 | +import org.springframework.mock.web.MockHttpSession; |
| 31 | +import org.springframework.security.core.Authentication; |
| 32 | + |
| 33 | +public class SessionFixationProtectionStrategyTest { |
| 34 | + |
| 35 | + private Authentication authentication; |
| 36 | + private MockHttpServletRequest httpServletRequest; |
| 37 | + private MockHttpServletResponse httpServletResponse; |
| 38 | + private MockHttpSession httpSession; |
| 39 | + private SessionFixationProtectionStrategy sessionFixationProtectionStrategy; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + this.authentication = mock(Authentication.class); |
| 44 | + this.httpServletRequest = new MockHttpServletRequest(); |
| 45 | + this.httpServletResponse = new MockHttpServletResponse(); |
| 46 | + this.httpSession = new MockHttpSession(); |
| 47 | + this.httpServletRequest.setSession(httpSession); |
| 48 | + this.sessionFixationProtectionStrategy = new SessionFixationProtectionStrategy(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void createsANewSessionWithAllAttributesTransferredAndTheSessionMaxInactiveInterval() { |
| 53 | + String name = "jaswanth"; |
| 54 | + List<String> hobbies = Arrays.asList("reading", "blah"); |
| 55 | + httpSession.setAttribute("name", name); |
| 56 | + httpSession.setAttribute("hobbies", hobbies); |
| 57 | + httpSession.setMaxInactiveInterval(2480); |
| 58 | + |
| 59 | + sessionFixationProtectionStrategy.onAuthentication(authentication, httpServletRequest, httpServletResponse); |
| 60 | + |
| 61 | + HttpSession newHttpSession = httpServletRequest.getSession(false); |
| 62 | + assertThat(httpSession.hashCode()).isNotEqualTo(newHttpSession.hashCode()); |
| 63 | + assertThat(newHttpSession.getAttribute("name")).isEqualTo(name); |
| 64 | + assertThat(newHttpSession.getAttribute("hobbies")).isEqualTo(hobbies); |
| 65 | + assertThat(newHttpSession.getMaxInactiveInterval()).isEqualTo(2480); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldNotTransferAttributesIfNotRequested() { |
| 70 | + httpSession.setAttribute("name", "jaswanth"); |
| 71 | + httpSession.setMaxInactiveInterval(2480); |
| 72 | + this.sessionFixationProtectionStrategy.setMigrateSessionAttributes(false); |
| 73 | + |
| 74 | + sessionFixationProtectionStrategy.onAuthentication(authentication, httpServletRequest, httpServletResponse); |
| 75 | + |
| 76 | + HttpSession newHttpSession = httpServletRequest.getSession(false); |
| 77 | + assertThat(httpSession.hashCode()).isNotEqualTo(newHttpSession.hashCode()); |
| 78 | + assertThat(newHttpSession.getAttributeNames().hasMoreElements()).isFalse(); |
| 79 | + assertThat(newHttpSession.getMaxInactiveInterval()).isEqualTo(2480); |
| 80 | + } |
| 81 | +} |
0 commit comments