Skip to content

Commit 9a891b2

Browse files
authored
Merge pull request #174 from devondragon/issue-173-BUG---Login-with-Google-SSO-Causes-Error
Update dependencies and enhance logging in authentication process
2 parents f347011 + 094a534 commit 9a891b2

File tree

6 files changed

+85
-46
lines changed

6 files changed

+85
-46
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ description = 'Spring User Framework'
1919

2020
ext {
2121
springBootVersion = '3.4.4'
22-
lombokVersion = '1.18.36'
22+
lombokVersion = '1.18.38'
2323
}
2424

2525
java {

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/com/digitalsanctuary/spring/user/api/data/Response.java

Whitespace-only changes.

src/main/java/com/digitalsanctuary/spring/user/service/DSUserDetails.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import java.util.Collection;
55
import java.util.HashMap;
66
import java.util.Map;
7-
8-
import lombok.Builder;
97
import org.springframework.security.core.GrantedAuthority;
108
import org.springframework.security.core.userdetails.UserDetails;
119
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
1210
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
1311
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
14-
import org.springframework.security.oauth2.core.user.OAuth2User;
1512
import com.digitalsanctuary.spring.user.persistence.model.User;
13+
import lombok.Builder;
1614
import lombok.ToString;
1715

1816
/**
@@ -41,7 +39,7 @@
4139
* }</pre>
4240
*/
4341
@ToString
44-
public class DSUserDetails implements UserDetails, OAuth2User, OidcUser {
42+
public class DSUserDetails implements UserDetails, OidcUser {
4543

4644
/** The Constant serialVersionUID. */
4745
private static final long serialVersionUID = 5286810064622508389L;
@@ -86,8 +84,8 @@ public DSUserDetails(User user) {
8684
* Instantiates a new DS user details.
8785
*
8886
* @param user the user
89-
* @param oidcUserInfo containing claims about the user
90-
* @param oidcIdToken containing claims about the user
87+
* @param oidcUserInfo containing claims about the user
88+
* @param oidcIdToken containing claims about the user
9189
* @param grantedAuthorities the granted authorities (optional, default = empty list)
9290
*/
9391
@Builder
@@ -102,8 +100,8 @@ public DSUserDetails(User user, OidcUserInfo oidcUserInfo, OidcIdToken oidcIdTok
102100
* Instantiates a new DS user details.
103101
*
104102
* @param user the user
105-
* @param oidcUserInfo containing claims about the user
106-
* @param oidcIdToken containing claims about the user
103+
* @param oidcUserInfo containing claims about the user
104+
* @param oidcIdToken containing claims about the user
107105
*/
108106
@Builder
109107
public DSUserDetails(User user, OidcUserInfo oidcUserInfo, OidcIdToken oidcIdToken) {

src/main/java/com/digitalsanctuary/spring/user/service/LoginSuccessService.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,19 @@ public class LoginSuccessService extends SavedRequestAwareAuthenticationSuccessH
4444
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException,
4545
ServletException {
4646
log.debug("LoginSuccessService.onAuthenticationSuccess()");
47-
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "called with authentiation: {}", authentication);
47+
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "called with request: {}", request);
48+
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "called with authentication: {}", authentication);
49+
50+
// Enhanced logging to check request attributes
51+
log.debug("Request URI: {}", request.getRequestURI());
52+
log.debug("Request URL: {}", request.getRequestURL());
53+
log.debug("Request query string: {}", request.getQueryString());
54+
log.debug("Session ID: {}", request.getSession().getId());
55+
56+
// Log saved request if present
57+
Object savedRequest = request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
58+
log.debug("Saved request in session: {}", savedRequest);
59+
4860
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "targetUrl: {}", super.determineTargetUrl(request, response));
4961

5062
User user = null;
@@ -59,22 +71,53 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
5971
}
6072
}
6173

74+
// Create audit event
6275
AuditEvent loginAuditEvent =
6376
AuditEvent.builder().source(this).user(user).sessionId(request.getSession().getId()).ipAddress(UserUtils.getClientIP(request))
6477
.userAgent(request.getHeader("User-Agent")).action("Login").actionStatus("Success").message("Success").build();
6578

66-
eventPublisher.publishEvent(loginAuditEvent);
79+
// Publish audit event in a try-catch to prevent redirection issues
80+
try {
81+
eventPublisher.publishEvent(loginAuditEvent);
82+
} catch (Exception e) {
83+
log.error("Error publishing login audit event", e);
84+
// Continue with the login flow even if audit logging fails
85+
}
6786

87+
// Get and set the target URL with enhanced logging
6888
String targetUrl = super.determineTargetUrl(request, response);
89+
log.debug("Initial targetUrl from super.determineTargetUrl: {}", targetUrl);
90+
6991
if (StringUtils.isEmptyOrWhitespace(targetUrl) || StringUtils.equals(targetUrl, "/")) {
7092
targetUrl = loginSuccessUri;
93+
log.debug("Using configured loginSuccessUri: {}", loginSuccessUri);
7194
this.setDefaultTargetUrl(targetUrl);
72-
7395
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "set defaultTargetUrl to: {}", this.getDefaultTargetUrl());
74-
log.debug("LoginSuccessService.onAuthenticationSuccess:" + "defaultTargetParam: {}", this.getTargetUrlParameter());
96+
} else {
97+
log.debug("Using existing targetUrl: {}", targetUrl);
7598
}
7699

100+
// Set the alwaysUseDefaultTargetUrl to ensure our target URL is always used
101+
this.setAlwaysUseDefaultTargetUrl(true);
102+
log.debug("AlwaysUseDefaultTargetUrl set to: {}", this.isAlwaysUseDefaultTargetUrl());
103+
104+
// Check if there's a redirect URL in the request parameters (common in OAuth2 flows)
105+
String continueParam = request.getParameter("continue");
106+
if (continueParam != null) {
107+
log.debug("Found 'continue' parameter in request: {}", continueParam);
108+
}
109+
110+
// Extra logging to track redirection
111+
log.debug("LoginSuccessService.onAuthenticationSuccess: Proceeding with redirection to {}", this.getDefaultTargetUrl());
112+
113+
// Log the SavedRequest state
114+
log.debug("SavedRequest state before calling super.onAuthenticationSuccess: {}",
115+
request.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST"));
116+
77117
super.onAuthenticationSuccess(request, response, authentication);
118+
119+
// This won't execute if the super method redirects, but might help with debugging
120+
log.debug("After super.onAuthenticationSuccess - if you see this, no redirect happened");
78121
}
79122

80123
}

src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import com.digitalsanctuary.spring.user.api.data.Response;
1919
import com.digitalsanctuary.spring.user.api.helper.AssertionsHelper;
2020
import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsProvider;
21-
import com.digitalsanctuary.spring.user.api.provider.ApiTestUpdatePasswordArgumentsProvider;
2221
import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder;
23-
import com.digitalsanctuary.spring.user.dto.PasswordDto;
2422
import com.digitalsanctuary.spring.user.dto.UserDto;
2523
import com.digitalsanctuary.spring.user.jdbc.Jdbc;
2624
import com.digitalsanctuary.spring.user.persistence.model.User;
@@ -80,36 +78,36 @@ public void resetPassword() throws Exception {
8078
}
8179

8280
// Tests temporarily disabled until OAuth2 dependency issue is resolved
83-
// /**
84-
// * Tests the update password functionality with valid and invalid password combinations.
85-
// *
86-
// * @param argumentsHolder Contains test data for password updates (valid/invalid scenarios)
87-
// * @throws Exception if any error occurs during test execution
88-
// */
89-
// @ParameterizedTest
90-
// @ArgumentsSource(ApiTestUpdatePasswordArgumentsProvider.class)
91-
// @Order(3)
92-
// public void updatePassword(ApiTestArgumentsHolder argumentsHolder) throws Exception {
93-
// // Register and login test user first
94-
// login(baseTestUser);
95-
//
96-
// PasswordDto passwordDto = argumentsHolder.getPasswordDto();
97-
//
98-
// ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updatePassword")
99-
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
100-
// .content(buildUrlEncodedFormEntity(passwordDto)));
101-
//
102-
// if (argumentsHolder.getStatus() == DataStatus.VALID) {
103-
// action.andExpect(status().isOk());
104-
// }
105-
// if (argumentsHolder.getStatus() == DataStatus.INVALID) {
106-
// action.andExpect(status().isBadRequest());
107-
// }
108-
//
109-
// MockHttpServletResponse actual = action.andReturn().getResponse();
110-
// Response expected = argumentsHolder.getResponse();
111-
// AssertionsHelper.compareResponses(actual, expected);
112-
// }
81+
// /**
82+
// * Tests the update password functionality with valid and invalid password combinations.
83+
// *
84+
// * @param argumentsHolder Contains test data for password updates (valid/invalid scenarios)
85+
// * @throws Exception if any error occurs during test execution
86+
// */
87+
// @ParameterizedTest
88+
// @ArgumentsSource(ApiTestUpdatePasswordArgumentsProvider.class)
89+
// @Order(3)
90+
// public void updatePassword(ApiTestArgumentsHolder argumentsHolder) throws Exception {
91+
// // Register and login test user first
92+
// login(baseTestUser);
93+
//
94+
// PasswordDto passwordDto = argumentsHolder.getPasswordDto();
95+
//
96+
// ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updatePassword")
97+
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
98+
// .content(buildUrlEncodedFormEntity(passwordDto)));
99+
//
100+
// if (argumentsHolder.getStatus() == DataStatus.VALID) {
101+
// action.andExpect(status().isOk());
102+
// }
103+
// if (argumentsHolder.getStatus() == DataStatus.INVALID) {
104+
// action.andExpect(status().isBadRequest());
105+
// }
106+
//
107+
// MockHttpServletResponse actual = action.andReturn().getResponse();
108+
// Response expected = argumentsHolder.getResponse();
109+
// AssertionsHelper.compareResponses(actual, expected);
110+
// }
113111

114112

115113
protected void login(UserDto userDto) {

0 commit comments

Comments
 (0)