Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@
* </ul>
*
* <p>
* <b>Debug Logging Enhancements:</b>
* </p>
* <ul>
* <li>Added comprehensive logging throughout authentication process for troubleshooting</li>
* <li>Logs authentication mode (token vs username/password)</li>
* <li>Logs credential extraction and SecurityService interaction</li>
* <li>Logs success/failure outcomes with error details</li>
* <li>Logs servlet context initialization (SecurityService and authTokenEnabled flag
* retrieval)</li>
* </ul>
*
* <p>
* <b>ServletContextAware Implementation:</b>
* </p>
* <ul>
Expand All @@ -109,25 +97,17 @@ public SecurityService getSecurityService() {

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
logger.info("authenticate() called - principal: {}, credentials type: {}, authTokenEnabled: {}",
authentication.getName(),
authentication.getCredentials() != null
? authentication.getCredentials().getClass().getSimpleName() : "null",
authTokenEnabled);

Properties credentials = new Properties();
String username = authentication.getName();
String password = authentication.getCredentials().toString();

logger.info("Extracted - username: {}, password: {}", username, password);

if (authTokenEnabled) {
logger.info("Auth token mode - setting TOKEN property with value: {}", password);
logger.debug("Authenticating with a token.");
if (password != null) {
credentials.setProperty(ResourceConstants.TOKEN, password);
}
} else {
logger.info("Username/password mode - setting USER_NAME and PASSWORD properties");
logger.debug("Authenticating with a user name and password.");
if (username != null) {
credentials.put(ResourceConstants.USER_NAME, username);
}
Expand All @@ -136,14 +116,12 @@ public Authentication authenticate(Authentication authentication) throws Authent
}
}

logger.info("Calling securityService.login() with credentials: {}", credentials);
try {
securityService.login(credentials);
logger.info("Login successful - creating UsernamePasswordAuthenticationToken");
return new UsernamePasswordAuthenticationToken(username, password,
AuthorityUtils.NO_AUTHORITIES);
} catch (GemFireSecurityException e) {
logger.error("Login failed with GemFireSecurityException: {}", e.getMessage(), e);
logger.debug("Authentication was not successful.", e);
throw new BadCredentialsException(e.getLocalizedMessage(), e);
}
}
Expand All @@ -159,14 +137,11 @@ public boolean isAuthTokenEnabled() {

@Override
public void setServletContext(ServletContext servletContext) {
logger.info("setServletContext() called");

securityService = (SecurityService) servletContext
.getAttribute(HttpService.SECURITY_SERVICE_SERVLET_CONTEXT_PARAM);
logger.info("SecurityService from servlet context: {}", securityService);

authTokenEnabled =
(Boolean) servletContext.getAttribute(HttpService.AUTH_TOKEN_ENABLED_PARAM);
logger.info("authTokenEnabled from servlet context: {}", authTokenEnabled);
logger.debug("Authentication token mode enabled: {}", authTokenEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,75 +70,51 @@
* properly
* log authentication failures. This helps diagnose JWT authentication issues in production.</li>
* </ul>
*
* <p>
* <b>Debug Logging:</b>
* </p>
* <ul>
* <li>Added comprehensive logging throughout authentication flow for troubleshooting</li>
* <li>Logs: filter initialization, authentication requirements check, token parsing, authentication
* attempts, success/failure outcomes</li>
* </ul>
*/
public class JwtAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
private static final Logger logger = LogManager.getLogger();

public JwtAuthenticationFilter() {
super("/**");
logger.info("JwtAuthenticationFilter initialized");
}

@Override
protected boolean requiresAuthentication(HttpServletRequest request,
HttpServletResponse response) {
String header = request.getHeader("Authorization");
boolean requires = header != null && header.startsWith("Bearer ");
logger.info("requiresAuthentication() - URI: {}, Authorization header: {}, requires: {}",
request.getRequestURI(), header, requires);
return requires;
return header != null && header.startsWith("Bearer ");
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {

String header = request.getHeader("Authorization");
logger.info("attemptAuthentication() - URI: {}, Authorization header: {}",
request.getRequestURI(), header);

if (header == null || !header.startsWith("Bearer ")) {
logger.error("No JWT token found - header: {}", header);
throw new BadCredentialsException("No JWT token found in request headers, header: " + header);
throw new BadCredentialsException("No JWT token found in request headers");
}

String[] tokens = header.split(" ", 2);
logger.info("Split token - length: {}, token[0]: {}, token[1]: {}",
tokens.length, tokens[0], tokens.length > 1 ? tokens[1] : "N/A");

if (tokens.length != 2) {
logger.error("Wrong authentication header format: {}", header);
throw new BadCredentialsException("Wrong authentication header format: " + header);
throw new BadCredentialsException("Wrong authentication header format");
}

// FIX: Pass the token as credentials (password), not "Bearer" as username
// GeodeAuthenticationProvider expects the token in the credentials/password field
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(tokens[1], tokens[1]);
logger.info("Created UsernamePasswordAuthenticationToken - principal: {}, credentials: {}",
authToken.getPrincipal(), authToken.getCredentials());

// CRITICAL: Call AuthenticationManager to actually authenticate the token
// AbstractAuthenticationProcessingFilter expects us to return an authenticated token
logger.info("Calling getAuthenticationManager().authenticate()");
return getAuthenticationManager().authenticate(authToken);
}

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, Authentication authResult)
throws IOException, ServletException {
logger.info("successfulAuthentication() - authResult: {}, principal: {}",
authResult, authResult != null ? authResult.getPrincipal() : "null");
super.successfulAuthentication(request, response, chain, authResult);

// As this authentication is in HTTP header, after success we need to continue the request
Expand All @@ -150,8 +126,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
protected void unsuccessfulAuthentication(HttpServletRequest request,
HttpServletResponse response, AuthenticationException failed)
throws IOException, ServletException {
logger.error("unsuccessfulAuthentication() - URI: {}, exception: {}",
request.getRequestURI(), failed.getMessage(), failed);
logger.debug("Authentication was not successful for {}.", request.getRequestURI(), failed);
super.unsuccessfulAuthentication(request, response, failed);
}
}
Loading