Skip to content

Commit 61072e8

Browse files
author
Steve Riesenberg
committed
Add logging for protocol endpoint filters
Issue spring-projectsgh-159
1 parent 241e688 commit 61072e8

7 files changed

+51
-2
lines changed

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

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

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
28+
import org.springframework.core.log.LogMessage;
2529
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.HttpStatus;
2731
import org.springframework.http.converter.HttpMessageConverter;
@@ -72,6 +76,7 @@ public final class OidcClientRegistrationEndpointFilter extends OncePerRequestFi
7276
*/
7377
private static final String DEFAULT_OIDC_CLIENT_REGISTRATION_ENDPOINT_URI = "/connect/register";
7478

79+
private static final Log logger = LogFactory.getLog(OidcClientRegistrationEndpointFilter.class);
7580
private final AuthenticationManager authenticationManager;
7681
private final RequestMatcher clientRegistrationEndpointMatcher;
7782
private final HttpMessageConverter<OidcClientRegistration> clientRegistrationHttpMessageConverter =
@@ -202,6 +207,7 @@ private void sendClientRegistrationResponse(HttpServletRequest request, HttpServ
202207
private void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
203208
AuthenticationException authenticationException) throws IOException {
204209
OAuth2Error error = ((OAuth2AuthenticationException) authenticationException).getError();
210+
logger.trace(LogMessage.format("Client registration request failed: %s", error), authenticationException);
205211
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
206212
if (OAuth2ErrorCodes.INVALID_TOKEN.equals(error.getErrorCode())) {
207213
httpStatus = HttpStatus.UNAUTHORIZED;

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

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

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
28+
import org.springframework.core.log.LogMessage;
2529
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.HttpStatus;
2731
import org.springframework.http.converter.HttpMessageConverter;
@@ -65,6 +69,7 @@ public final class OidcUserInfoEndpointFilter extends OncePerRequestFilter {
6569
*/
6670
private static final String DEFAULT_OIDC_USER_INFO_ENDPOINT_URI = "/userinfo";
6771

72+
private static final Log logger = LogFactory.getLog(OidcUserInfoEndpointFilter.class);
6873
private final AuthenticationManager authenticationManager;
6974
private final RequestMatcher userInfoEndpointMatcher;
7075
private final HttpMessageConverter<OidcUserInfo> userInfoHttpMessageConverter =
@@ -180,6 +185,7 @@ private void sendUserInfoResponse(HttpServletRequest request, HttpServletRespons
180185
private void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
181186
AuthenticationException authenticationException) throws IOException {
182187
OAuth2Error error = ((OAuth2AuthenticationException) authenticationException).getError();
188+
logger.trace(LogMessage.format("User info request failed: %s", error), authenticationException);
183189
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
184190
if (error.getErrorCode().equals(OAuth2ErrorCodes.INVALID_TOKEN)) {
185191
httpStatus = HttpStatus.UNAUTHORIZED;

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

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

31+
import org.apache.commons.logging.Log;
32+
import org.apache.commons.logging.LogFactory;
33+
34+
import org.springframework.core.log.LogMessage;
3135
import org.springframework.http.HttpMethod;
3236
import org.springframework.http.HttpStatus;
3337
import org.springframework.http.MediaType;
@@ -90,6 +94,7 @@ public final class OAuth2AuthorizationEndpointFilter extends OncePerRequestFilte
9094
*/
9195
private static final String DEFAULT_AUTHORIZATION_ENDPOINT_URI = "/oauth2/authorize";
9296

97+
private static final Log logger = LogFactory.getLog(OAuth2AuthorizationEndpointFilter.class);
9398
private final AuthenticationManager authenticationManager;
9499
private final RequestMatcher authorizationEndpointMatcher;
95100
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@@ -173,6 +178,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
173178
}
174179

175180
if (authenticationResult instanceof OAuth2AuthorizationConsentAuthenticationToken) {
181+
logger.debug("Authorization consent is required");
176182
sendAuthorizationConsent(request, response,
177183
(OAuth2AuthorizationCodeRequestAuthenticationToken) authentication,
178184
(OAuth2AuthorizationConsentAuthenticationToken) authenticationResult);
@@ -260,6 +266,7 @@ private void sendAuthorizationConsent(HttpServletRequest request, HttpServletRes
260266
.toUriString();
261267
this.redirectStrategy.sendRedirect(request, response, redirectUri);
262268
} else {
269+
logger.trace("Displaying generated consent screen");
263270
DefaultConsentPage.displayConsent(request, response, clientId, principal, requestedScopes, authorizedScopes, state);
264271
}
265272
}
@@ -307,6 +314,8 @@ private void sendErrorResponse(HttpServletRequest request, HttpServletResponse r
307314
OAuth2AuthorizationCodeRequestAuthenticationException authorizationCodeRequestAuthenticationException =
308315
(OAuth2AuthorizationCodeRequestAuthenticationException) exception;
309316
OAuth2Error error = authorizationCodeRequestAuthenticationException.getError();
317+
logger.trace(LogMessage.format("Authorization request failed: %s", error),
318+
authorizationCodeRequestAuthenticationException);
310319
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
311320
authorizationCodeRequestAuthenticationException.getAuthorizationCodeRequestAuthentication();
312321

@@ -316,6 +325,8 @@ private void sendErrorResponse(HttpServletRequest request, HttpServletResponse r
316325
return;
317326
}
318327

328+
logger.trace("Redirecting to client with error");
329+
319330
UriComponentsBuilder uriBuilder = UriComponentsBuilder
320331
.fromUriString(authorizationCodeRequestAuthentication.getRedirectUri())
321332
.queryParam(OAuth2ParameterNames.ERROR, error.getErrorCode());

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

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

26+
import org.apache.commons.logging.Log;
27+
import org.apache.commons.logging.LogFactory;
28+
29+
import org.springframework.core.log.LogMessage;
2630
import org.springframework.http.HttpStatus;
2731
import org.springframework.http.converter.HttpMessageConverter;
2832
import org.springframework.http.server.ServletServerHttpResponse;
@@ -72,6 +76,7 @@
7276
* @see <a target="_blank" href="https://datatracker.ietf.org/doc/html/rfc6749#section-3.2.1">Section 3.2.1 Token Endpoint Client Authentication</a>
7377
*/
7478
public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter {
79+
private static final Log logger = LogFactory.getLog(OAuth2ClientAuthenticationFilter.class);
7580
private final AuthenticationManager authenticationManager;
7681
private final RequestMatcher requestMatcher;
7782
private final HttpMessageConverter<OAuth2Error> errorHttpResponseConverter = new OAuth2ErrorHttpMessageConverter();
@@ -181,7 +186,9 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
181186
// include the "WWW-Authenticate" response header field
182187
// matching the authentication scheme used by the client.
183188

184-
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError();
189+
OAuth2AuthenticationException authenticationException = (OAuth2AuthenticationException) exception;
190+
OAuth2Error error = authenticationException.getError();
191+
logger.trace(LogMessage.format("Invalid client authentication: %s", error), authenticationException);
185192
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
186193
if (OAuth2ErrorCodes.INVALID_CLIENT.equals(error.getErrorCode())) {
187194
httpResponse.setStatusCode(HttpStatus.UNAUTHORIZED);

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

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

28+
import org.apache.commons.logging.Log;
29+
import org.apache.commons.logging.LogFactory;
30+
31+
import org.springframework.core.log.LogMessage;
2832
import org.springframework.http.HttpMethod;
2933
import org.springframework.http.HttpStatus;
3034
import org.springframework.http.converter.HttpMessageConverter;
@@ -99,6 +103,7 @@ public final class OAuth2TokenEndpointFilter extends OncePerRequestFilter {
99103
private static final String DEFAULT_TOKEN_ENDPOINT_URI = "/oauth2/token";
100104

101105
private static final String DEFAULT_ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc6749#section-5.2";
106+
private static final Log logger = LogFactory.getLog(OAuth2TokenEndpointFilter.class);
102107
private final AuthenticationManager authenticationManager;
103108
private final RequestMatcher tokenEndpointMatcher;
104109
private final HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenHttpResponseConverter =
@@ -245,7 +250,9 @@ private void sendAccessTokenResponse(HttpServletRequest request, HttpServletResp
245250
private void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
246251
AuthenticationException exception) throws IOException {
247252

248-
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError();
253+
OAuth2AuthenticationException authenticationException = (OAuth2AuthenticationException) exception;
254+
OAuth2Error error = authenticationException.getError();
255+
logger.trace(LogMessage.format("Token request failed: %s", error), authenticationException);
249256
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
250257
httpResponse.setStatusCode(HttpStatus.BAD_REQUEST);
251258
this.errorHttpResponseConverter.write(error, null, httpResponse);

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

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

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
28+
import org.springframework.core.log.LogMessage;
2529
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.HttpStatus;
2731
import org.springframework.http.converter.HttpMessageConverter;
@@ -63,6 +67,7 @@ public final class OAuth2TokenIntrospectionEndpointFilter extends OncePerRequest
6367
*/
6468
private static final String DEFAULT_TOKEN_INTROSPECTION_ENDPOINT_URI = "/oauth2/introspect";
6569

70+
private static final Log logger = LogFactory.getLog(OAuth2TokenIntrospectionEndpointFilter.class);
6671
private final AuthenticationManager authenticationManager;
6772
private final RequestMatcher tokenIntrospectionEndpointMatcher;
6873
private AuthenticationConverter authenticationConverter;
@@ -165,6 +170,7 @@ private void sendIntrospectionResponse(HttpServletRequest request, HttpServletRe
165170
private void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
166171
AuthenticationException exception) throws IOException {
167172
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError();
173+
logger.trace(LogMessage.format("Token introspection request failed: %s", error), exception);
168174
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
169175
httpResponse.setStatusCode(HttpStatus.BAD_REQUEST);
170176
this.errorHttpResponseConverter.write(error, null, httpResponse);

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

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

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
27+
28+
import org.springframework.core.log.LogMessage;
2529
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.HttpStatus;
2731
import org.springframework.http.converter.HttpMessageConverter;
@@ -61,6 +65,7 @@ public final class OAuth2TokenRevocationEndpointFilter extends OncePerRequestFil
6165
*/
6266
private static final String DEFAULT_TOKEN_REVOCATION_ENDPOINT_URI = "/oauth2/revoke";
6367

68+
private static final Log logger = LogFactory.getLog(OAuth2TokenRevocationEndpointFilter.class);
6469
private final AuthenticationManager authenticationManager;
6570
private final RequestMatcher tokenRevocationEndpointMatcher;
6671
private AuthenticationConverter authenticationConverter;
@@ -156,6 +161,7 @@ private void sendRevocationSuccessResponse(HttpServletRequest request, HttpServl
156161
private void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
157162
AuthenticationException exception) throws IOException {
158163
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError();
164+
logger.trace(LogMessage.format("Token revocation request failed: %s", error), exception);
159165
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
160166
httpResponse.setStatusCode(HttpStatus.BAD_REQUEST);
161167
this.errorHttpResponseConverter.write(error, null, httpResponse);

0 commit comments

Comments
 (0)