Skip to content

Commit 24b8f43

Browse files
author
Steve Riesenberg
committed
Add logging for authentication filters
Closes spring-projectsgh-159
1 parent 133de51 commit 24b8f43

7 files changed

+48
-0
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/web/OidcClientRegistrationEndpointFilter.java

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.springframework.core.log.LogMessage;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.converter.HttpMessageConverter;
@@ -137,12 +138,18 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
137138

138139
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, clientRegistrationAuthenticationResult);
139140
} catch (OAuth2AuthenticationException ex) {
141+
if (this.logger.isTraceEnabled()) {
142+
this.logger.trace(LogMessage.format("Client registration request failed: %s", ex.getError()), ex);
143+
}
140144
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
141145
} catch (Exception ex) {
142146
OAuth2Error error = new OAuth2Error(
143147
OAuth2ErrorCodes.INVALID_REQUEST,
144148
"OpenID Connect 1.0 Client Registration Error: " + ex.getMessage(),
145149
"https://openid.net/specs/openid-connect-registration-1_0.html#RegistrationError");
150+
if (this.logger.isTraceEnabled()) {
151+
this.logger.trace(error.getDescription(), ex);
152+
}
146153
this.authenticationFailureHandler.onAuthenticationFailure(request, response,
147154
new OAuth2AuthenticationException(error));
148155
} finally {

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/web/OidcUserInfoEndpointFilter.java

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.springframework.core.log.LogMessage;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.converter.HttpMessageConverter;
@@ -116,12 +117,18 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
116117

117118
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, userInfoAuthenticationResult);
118119
} catch (OAuth2AuthenticationException ex) {
120+
if (this.logger.isTraceEnabled()) {
121+
this.logger.trace(LogMessage.format("User info request failed: %s", ex.getError()), ex);
122+
}
119123
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
120124
} catch (Exception ex) {
121125
OAuth2Error error = new OAuth2Error(
122126
OAuth2ErrorCodes.INVALID_REQUEST,
123127
"OpenID Connect 1.0 UserInfo Error: " + ex.getMessage(),
124128
"https://openid.net/specs/openid-connect-core-1_0.html#UserInfoError");
129+
if (this.logger.isTraceEnabled()) {
130+
this.logger.trace(error.getDescription(), ex);
131+
}
125132
this.authenticationFailureHandler.onAuthenticationFailure(request, response,
126133
new OAuth2AuthenticationException(error));
127134
} finally {

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2AuthorizationEndpointFilter.java

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.servlet.http.HttpServletRequest;
2929
import javax.servlet.http.HttpServletResponse;
3030

31+
import org.springframework.core.log.LogMessage;
3132
import org.springframework.http.HttpMethod;
3233
import org.springframework.http.HttpStatus;
3334
import org.springframework.http.MediaType;
@@ -173,6 +174,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
173174
}
174175

175176
if (authenticationResult instanceof OAuth2AuthorizationConsentAuthenticationToken) {
177+
if (this.logger.isTraceEnabled()) {
178+
this.logger.trace("Authorization consent is required");
179+
}
176180
sendAuthorizationConsent(request, response,
177181
(OAuth2AuthorizationCodeRequestAuthenticationToken) authentication,
178182
(OAuth2AuthorizationConsentAuthenticationToken) authenticationResult);
@@ -183,6 +187,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
183187
request, response, authenticationResult);
184188

185189
} catch (OAuth2AuthenticationException ex) {
190+
if (this.logger.isTraceEnabled()) {
191+
this.logger.trace(LogMessage.format("Authorization request failed: %s", ex.getError()), ex);
192+
}
186193
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
187194
}
188195
}
@@ -260,6 +267,9 @@ private void sendAuthorizationConsent(HttpServletRequest request, HttpServletRes
260267
.toUriString();
261268
this.redirectStrategy.sendRedirect(request, response, redirectUri);
262269
} else {
270+
if (this.logger.isTraceEnabled()) {
271+
this.logger.trace("Displaying generated consent screen");
272+
}
263273
DefaultConsentPage.displayConsent(request, response, clientId, principal, requestedScopes, authorizedScopes, state);
264274
}
265275
}
@@ -316,6 +326,10 @@ private void sendErrorResponse(HttpServletRequest request, HttpServletResponse r
316326
return;
317327
}
318328

329+
if (this.logger.isTraceEnabled()) {
330+
this.logger.trace("Redirecting to client with error");
331+
}
332+
319333
UriComponentsBuilder uriBuilder = UriComponentsBuilder
320334
.fromUriString(authorizationCodeRequestAuthentication.getRedirectUri())
321335
.queryParam(OAuth2ParameterNames.ERROR, error.getErrorCode());

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2ClientAuthenticationFilter.java

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.servlet.http.HttpServletRequest;
2424
import javax.servlet.http.HttpServletResponse;
2525

26+
import org.springframework.core.log.LogMessage;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.converter.HttpMessageConverter;
2829
import org.springframework.http.server.ServletServerHttpResponse;
@@ -123,6 +124,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
123124
filterChain.doFilter(request, response);
124125

125126
} catch (OAuth2AuthenticationException ex) {
127+
if (this.logger.isTraceEnabled()) {
128+
this.logger.trace(LogMessage.format("Invalid client authentication: %s", ex.getError()), ex);
129+
}
126130
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
127131
}
128132
}
@@ -166,6 +170,10 @@ private void onAuthenticationSuccess(HttpServletRequest request, HttpServletResp
166170
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
167171
securityContext.setAuthentication(authentication);
168172
SecurityContextHolder.setContext(securityContext);
173+
if (this.logger.isDebugEnabled()) {
174+
this.logger.debug(LogMessage.format("Set SecurityContextHolder authentication to %s",
175+
authentication.getClass().getSimpleName()));
176+
}
169177
}
170178

171179
private void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenEndpointFilter.java

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.http.HttpServletRequest;
2626
import javax.servlet.http.HttpServletResponse;
2727

28+
import org.springframework.core.log.LogMessage;
2829
import org.springframework.http.HttpMethod;
2930
import org.springframework.http.HttpStatus;
3031
import org.springframework.http.converter.HttpMessageConverter;
@@ -167,6 +168,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
167168
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, accessTokenAuthentication);
168169
} catch (OAuth2AuthenticationException ex) {
169170
SecurityContextHolder.clearContext();
171+
if (this.logger.isTraceEnabled()) {
172+
this.logger.trace(LogMessage.format("Token request failed: %s", ex.getError()), ex);
173+
}
170174
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
171175
}
172176
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenIntrospectionEndpointFilter.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.springframework.core.log.LogMessage;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.converter.HttpMessageConverter;
@@ -113,6 +114,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
113114
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, tokenIntrospectionAuthenticationResult);
114115
} catch (OAuth2AuthenticationException ex) {
115116
SecurityContextHolder.clearContext();
117+
if (this.logger.isTraceEnabled()) {
118+
this.logger.trace(LogMessage.format("Token introspection request failed: %s", ex.getError()), ex);
119+
}
116120
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
117121
}
118122
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenRevocationEndpointFilter.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.springframework.core.log.LogMessage;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.converter.HttpMessageConverter;
@@ -110,6 +111,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
110111
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, tokenRevocationAuthenticationResult);
111112
} catch (OAuth2AuthenticationException ex) {
112113
SecurityContextHolder.clearContext();
114+
if (this.logger.isTraceEnabled()) {
115+
this.logger.trace(LogMessage.format("Token revocation request failed: %s", ex.getError()), ex);
116+
}
113117
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
114118
}
115119
}

0 commit comments

Comments
 (0)