From d9c5e402a9f1a235f23e90e67655981b15b75cc0 Mon Sep 17 00:00:00 2001 From: Yuriy Savchenko Date: Sat, 12 Feb 2022 20:04:10 +0300 Subject: [PATCH] Make WebAuthenticationDetails constructor public Closes gh-10564 --- .../web/authentication/WebAuthenticationDetails.java | 11 +++++++---- .../jackson2/WebAuthenticationDetailsMixinTests.java | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java b/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java index 61c26dd982e..5d7ca6057ca 100644 --- a/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java +++ b/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java @@ -43,9 +43,7 @@ public class WebAuthenticationDetails implements Serializable { * @param request that the authentication request was received from */ public WebAuthenticationDetails(HttpServletRequest request) { - this.remoteAddress = request.getRemoteAddr(); - HttpSession session = request.getSession(false); - this.sessionId = (session != null) ? session.getId() : null; + this(request.getRemoteAddr(), extractSessionId(request)); } /** @@ -53,11 +51,16 @@ public WebAuthenticationDetails(HttpServletRequest request) { * @param remoteAddress remote address of current request * @param sessionId session id */ - private WebAuthenticationDetails(final String remoteAddress, final String sessionId) { + public WebAuthenticationDetails(String remoteAddress, String sessionId) { this.remoteAddress = remoteAddress; this.sessionId = sessionId; } + private static String extractSessionId(HttpServletRequest request) { + HttpSession session = request.getSession(false); + return (session != null) ? session.getId() : null; + } + @Override public boolean equals(Object obj) { if (obj instanceof WebAuthenticationDetails) { diff --git a/web/src/test/java/org/springframework/security/web/jackson2/WebAuthenticationDetailsMixinTests.java b/web/src/test/java/org/springframework/security/web/jackson2/WebAuthenticationDetailsMixinTests.java index 8a3d2f7c437..93fed9c7510 100644 --- a/web/src/test/java/org/springframework/security/web/jackson2/WebAuthenticationDetailsMixinTests.java +++ b/web/src/test/java/org/springframework/security/web/jackson2/WebAuthenticationDetailsMixinTests.java @@ -64,6 +64,13 @@ public void webAuthenticationDetailsSerializeTest() throws JsonProcessingExcepti JSONAssert.assertEquals(AUTHENTICATION_DETAILS_JSON, actualJson, true); } + @Test + public void webAuthenticationDetailsJackson2SerializeTest() throws JsonProcessingException, JSONException { + WebAuthenticationDetails details = new WebAuthenticationDetails("/localhost", "1"); + String actualJson = this.mapper.writeValueAsString(details); + JSONAssert.assertEquals(AUTHENTICATION_DETAILS_JSON, actualJson, true); + } + @Test public void webAuthenticationDetailsDeserializeTest() throws IOException { WebAuthenticationDetails details = this.mapper.readValue(AUTHENTICATION_DETAILS_JSON,