Skip to content

Commit 8ca3917

Browse files
committed
Polish OAuth2ClientAuthenticationFilter
1 parent 7478376 commit 8ca3917

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

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

+37-32
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
* @since 0.0.1
6060
* @see AuthenticationManager
6161
* @see OAuth2ClientAuthenticationProvider
62-
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-2.3">Section 2.3 Client Authentication</a>
63-
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-3.2.1">Section 3.2.1 Token Endpoint Client Authentication</a>
62+
* @see <a target="_blank" href="https://datatracker.ietf.org/doc/html/rfc6749#section-2.3">Section 2.3 Client Authentication</a>
63+
* @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>
6464
*/
6565
public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter {
6666
private final AuthenticationManager authenticationManager;
@@ -69,8 +69,8 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter
6969
private final AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource =
7070
new WebAuthenticationDetailsSource();
7171
private AuthenticationConverter authenticationConverter;
72-
private AuthenticationSuccessHandler authenticationSuccessHandler;
73-
private AuthenticationFailureHandler authenticationFailureHandler;
72+
private AuthenticationSuccessHandler authenticationSuccessHandler = this::onAuthenticationSuccess;
73+
private AuthenticationFailureHandler authenticationFailureHandler = this::onAuthenticationFailure;
7474

7575
/**
7676
* Constructs an {@code OAuth2ClientAuthenticationFilter} using the provided parameters.
@@ -89,57 +89,61 @@ public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationMana
8989
new ClientSecretBasicAuthenticationConverter(),
9090
new ClientSecretPostAuthenticationConverter(),
9191
new PublicClientAuthenticationConverter()));
92-
this.authenticationSuccessHandler = this::onAuthenticationSuccess;
93-
this.authenticationFailureHandler = this::onAuthenticationFailure;
9492
}
9593

9694
@Override
9795
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
9896
throws ServletException, IOException {
9997

100-
if (this.requestMatcher.matches(request)) {
101-
try {
102-
Authentication authenticationRequest = this.authenticationConverter.convert(request);
103-
if (authenticationRequest instanceof AbstractAuthenticationToken) {
104-
((AbstractAuthenticationToken) authenticationRequest).setDetails(
105-
this.authenticationDetailsSource.buildDetails(request));
106-
}
107-
if (authenticationRequest != null) {
108-
Authentication authenticationResult = this.authenticationManager.authenticate(authenticationRequest);
109-
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, authenticationResult);
110-
}
111-
} catch (OAuth2AuthenticationException failed) {
112-
this.authenticationFailureHandler.onAuthenticationFailure(request, response, failed);
113-
return;
98+
if (!this.requestMatcher.matches(request)) {
99+
filterChain.doFilter(request, response);
100+
return;
101+
}
102+
103+
try {
104+
Authentication authenticationRequest = this.authenticationConverter.convert(request);
105+
if (authenticationRequest instanceof AbstractAuthenticationToken) {
106+
((AbstractAuthenticationToken) authenticationRequest).setDetails(
107+
this.authenticationDetailsSource.buildDetails(request));
108+
}
109+
if (authenticationRequest != null) {
110+
Authentication authenticationResult = this.authenticationManager.authenticate(authenticationRequest);
111+
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, authenticationResult);
114112
}
113+
filterChain.doFilter(request, response);
114+
115+
} catch (OAuth2AuthenticationException ex) {
116+
this.authenticationFailureHandler.onAuthenticationFailure(request, response, ex);
115117
}
116-
filterChain.doFilter(request, response);
117118
}
118119

119120
/**
120-
* Sets the {@link AuthenticationConverter} used for converting a {@link HttpServletRequest} to an {@link OAuth2ClientAuthenticationToken}.
121+
* Sets the {@link AuthenticationConverter} used when attempting to extract client credentials from {@link HttpServletRequest}
122+
* to an instance of {@link OAuth2ClientAuthenticationToken} used for authenticating the client.
121123
*
122-
* @param authenticationConverter used for converting a {@link HttpServletRequest} to an {@link OAuth2ClientAuthenticationToken}
124+
* @param authenticationConverter the {@link AuthenticationConverter} used when attempting to extract client credentials from {@link HttpServletRequest}
123125
*/
124126
public void setAuthenticationConverter(AuthenticationConverter authenticationConverter) {
125127
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
126128
this.authenticationConverter = authenticationConverter;
127129
}
128130

129131
/**
130-
* Sets the {@link AuthenticationSuccessHandler} used for handling successful authentications.
132+
* Sets the {@link AuthenticationSuccessHandler} used for handling a successful client authentication
133+
* and associating the {@link OAuth2ClientAuthenticationToken} to the {@link SecurityContext}.
131134
*
132-
* @param authenticationSuccessHandler the {@link AuthenticationSuccessHandler} used for handling successful authentications
135+
* @param authenticationSuccessHandler the {@link AuthenticationSuccessHandler} used for handling a successful client authentication
133136
*/
134137
public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler authenticationSuccessHandler) {
135138
Assert.notNull(authenticationSuccessHandler, "authenticationSuccessHandler cannot be null");
136139
this.authenticationSuccessHandler = authenticationSuccessHandler;
137140
}
138141

139142
/**
140-
* Sets the {@link AuthenticationFailureHandler} used for handling failed authentications.
143+
* Sets the {@link AuthenticationFailureHandler} used for handling a failed client authentication
144+
* and returning the {@link OAuth2Error Error Response}.
141145
*
142-
* @param authenticationFailureHandler the {@link AuthenticationFailureHandler} used for handling failed authentications
146+
* @param authenticationFailureHandler the {@link AuthenticationFailureHandler} used for handling a failed client authentication
143147
*/
144148
public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
145149
Assert.notNull(authenticationFailureHandler, "authenticationFailureHandler cannot be null");
@@ -149,13 +153,13 @@ public void setAuthenticationFailureHandler(AuthenticationFailureHandler authent
149153
private void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
150154
Authentication authentication) {
151155

152-
SecurityContext context = SecurityContextHolder.createEmptyContext();
153-
context.setAuthentication(authentication);
154-
SecurityContextHolder.setContext(context);
156+
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
157+
securityContext.setAuthentication(authentication);
158+
SecurityContextHolder.setContext(securityContext);
155159
}
156160

157161
private void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
158-
AuthenticationException failed) throws IOException {
162+
AuthenticationException exception) throws IOException {
159163

160164
SecurityContextHolder.clearContext();
161165

@@ -167,7 +171,7 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
167171
// include the "WWW-Authenticate" response header field
168172
// matching the authentication scheme used by the client.
169173

170-
OAuth2Error error = ((OAuth2AuthenticationException) failed).getError();
174+
OAuth2Error error = ((OAuth2AuthenticationException) exception).getError();
171175
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response);
172176
if (OAuth2ErrorCodes.INVALID_CLIENT.equals(error.getErrorCode())) {
173177
httpResponse.setStatusCode(HttpStatus.UNAUTHORIZED);
@@ -176,4 +180,5 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
176180
}
177181
this.errorHttpResponseConverter.write(error, null, httpResponse);
178182
}
183+
179184
}

0 commit comments

Comments
 (0)