Skip to content

Commit b22e12e

Browse files
metsmasvenzik
authored andcommitted
Use session fixation protection strategy
WE2-849 Signed-off-by: Raul Metsma <[email protected]>
1 parent 96a156b commit b22e12e

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

example/src/main/java/eu/webeid/example/security/WebEidAjaxLoginProcessingFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.security.core.AuthenticationException;
4040
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
4141
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
42+
import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy;
4243

4344
public class WebEidAjaxLoginProcessingFilter extends AbstractAuthenticationProcessingFilter {
4445
private static final Logger LOG = LoggerFactory.getLogger(WebEidAjaxLoginProcessingFilter.class);
@@ -51,6 +52,7 @@ public WebEidAjaxLoginProcessingFilter(
5152
this.setAuthenticationManager(authenticationManager);
5253
this.setAuthenticationSuccessHandler(new AjaxAuthenticationSuccessHandler());
5354
this.setAuthenticationFailureHandler(new AjaxAuthenticationFailureHandler());
55+
setSessionAuthenticationStrategy(new SessionFixationProtectionStrategy());
5456
}
5557

5658
@Override

example/src/test/java/eu/webeid/example/WebApplicationTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.mock.web.MockHttpServletResponse;
3838
import org.springframework.mock.web.MockHttpSession;
3939
import org.springframework.test.context.web.WebAppConfiguration;
40+
import org.springframework.test.web.servlet.MvcResult;
4041
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
4142
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4243
import org.springframework.web.context.WebApplicationContext;
@@ -98,15 +99,17 @@ public void validateOcspResponse(XadesSignature xadesSignature) {
9899
}
99100
};
100101

101-
final MockHttpSession session = new MockHttpSession();
102+
MockHttpSession session = new MockHttpSession();
102103
session.setAttribute("challenge-nonce", new ChallengeNonce(ObjectMother.VALID_CHALLENGE_NONCE, DateAndTime.utcNow().plusMinutes(1)));
103104

104105
Dates.setMockedSignatureDate(Dates.getSigningDateTime());
105106

106107
// Act and assert
107108
mvcBuilder.build().perform(get("/auth/challenge"));
108109

109-
MockHttpServletResponse response = HttpHelper.login(mvcBuilder, session, ObjectMother.mockAuthToken());
110+
MvcResult result = HttpHelper.login(mvcBuilder, session, ObjectMother.mockAuthToken());
111+
session = (MockHttpSession) result.getRequest().getSession();
112+
MockHttpServletResponse response = result.getResponse();
110113
assertEquals("{\"sub\":\"JAAK-KRISTJAN JÕEORG\",\"auth\":[\"ROLE_USER\"]}", response.getContentAsString());
111114

112115
/* Example how to test file upload.

example/src/test/java/eu/webeid/example/testutil/HttpHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.mock.web.MockHttpServletResponse;
2727
import org.springframework.mock.web.MockHttpSession;
2828
import org.springframework.mock.web.MockMultipartFile;
29+
import org.springframework.test.web.servlet.MvcResult;
2930
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
3031
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
3132
import eu.webeid.example.security.dto.AuthTokenDTO;
@@ -38,7 +39,7 @@
3839

3940
public class HttpHelper {
4041

41-
public static MockHttpServletResponse login(DefaultMockMvcBuilder mvcBuilder, MockHttpSession session, AuthTokenDTO authTokenDTO) throws Exception {
42+
public static MvcResult login(DefaultMockMvcBuilder mvcBuilder, MockHttpSession session, AuthTokenDTO authTokenDTO) throws Exception {
4243
// @formatter:off
4344
return mvcBuilder
4445
.build()
@@ -47,8 +48,7 @@ public static MockHttpServletResponse login(DefaultMockMvcBuilder mvcBuilder, Mo
4748
.with(csrf())
4849
.contentType(MediaType.APPLICATION_JSON)
4950
.content(ObjectMother.toJson(authTokenDTO)))
50-
.andReturn()
51-
.getResponse();
51+
.andReturn();
5252
// @formatter:on
5353
}
5454

0 commit comments

Comments
 (0)