2424import jakarta .servlet .http .HttpServletResponse ;
2525
2626import org .springframework .core .log .LogMessage ;
27- import org .springframework .http .HttpStatus ;
28- import org .springframework .http .converter .HttpMessageConverter ;
29- import org .springframework .http .server .ServletServerHttpResponse ;
3027import org .springframework .security .authentication .AbstractAuthenticationToken ;
3128import org .springframework .security .authentication .AuthenticationDetailsSource ;
3229import org .springframework .security .authentication .AuthenticationManager ;
3330import org .springframework .security .core .Authentication ;
34- import org .springframework .security .core .AuthenticationException ;
3531import org .springframework .security .core .context .SecurityContext ;
3632import org .springframework .security .core .context .SecurityContextHolder ;
37- import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
3833import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
3934import org .springframework .security .oauth2 .core .OAuth2Error ;
4035import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
41- import org .springframework .security .oauth2 .core .http .converter .OAuth2ErrorHttpMessageConverter ;
4236import org .springframework .security .oauth2 .server .authorization .authentication .ClientSecretAuthenticationProvider ;
4337import org .springframework .security .oauth2 .server .authorization .authentication .JwtClientAssertionAuthenticationProvider ;
38+ import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2ClientAuthenticationException ;
4439import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2ClientAuthenticationToken ;
4540import org .springframework .security .oauth2 .server .authorization .authentication .PublicClientAuthenticationProvider ;
4641import org .springframework .security .oauth2 .server .authorization .authentication .X509ClientCertificateAuthenticationProvider ;
4742import org .springframework .security .oauth2 .server .authorization .web .authentication .ClientSecretBasicAuthenticationConverter ;
4843import org .springframework .security .oauth2 .server .authorization .web .authentication .ClientSecretPostAuthenticationConverter ;
4944import org .springframework .security .oauth2 .server .authorization .web .authentication .JwtClientAssertionAuthenticationConverter ;
45+ import org .springframework .security .oauth2 .server .authorization .web .authentication .OAuth2ClientAuthenticationFailureHandler ;
5046import org .springframework .security .oauth2 .server .authorization .web .authentication .PublicClientAuthenticationConverter ;
5147import org .springframework .security .oauth2 .server .authorization .web .authentication .X509ClientCertificateAuthenticationConverter ;
5248import org .springframework .security .web .authentication .AuthenticationConverter ;
5349import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
5450import org .springframework .security .web .authentication .AuthenticationSuccessHandler ;
5551import org .springframework .security .web .authentication .DelegatingAuthenticationConverter ;
5652import org .springframework .security .web .authentication .WebAuthenticationDetailsSource ;
57- import org .springframework .security .web .authentication .www .BasicAuthenticationEntryPoint ;
5853import org .springframework .security .web .util .matcher .RequestMatcher ;
5954import org .springframework .util .Assert ;
6055import org .springframework .web .filter .OncePerRequestFilter ;
7570 * @see ClientSecretAuthenticationProvider
7671 * @see PublicClientAuthenticationConverter
7772 * @see PublicClientAuthenticationProvider
73+ * @see OAuth2ClientAuthenticationFailureHandler
7874 * @see <a target="_blank" href=
7975 * "https://datatracker.ietf.org/doc/html/rfc6749#section-2.3">Section 2.3 Client
8076 * Authentication</a>
@@ -88,17 +84,13 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter
8884
8985 private final RequestMatcher requestMatcher ;
9086
91- private final HttpMessageConverter <OAuth2Error > errorHttpResponseConverter = new OAuth2ErrorHttpMessageConverter ();
92-
9387 private final AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource = new WebAuthenticationDetailsSource ();
9488
95- private final BasicAuthenticationEntryPoint basicAuthenticationEntryPoint = new BasicAuthenticationEntryPoint ();
96-
9789 private AuthenticationConverter authenticationConverter ;
9890
9991 private AuthenticationSuccessHandler authenticationSuccessHandler = this ::onAuthenticationSuccess ;
10092
101- private AuthenticationFailureHandler authenticationFailureHandler = this :: onAuthenticationFailure ;
93+ private AuthenticationFailureHandler authenticationFailureHandler = new OAuth2ClientAuthenticationFailureHandler () ;
10294
10395 /**
10496 * Constructs an {@code OAuth2ClientAuthenticationFilter} using the provided
@@ -114,7 +106,6 @@ public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationMana
114106 Assert .notNull (requestMatcher , "requestMatcher cannot be null" );
115107 this .authenticationManager = authenticationManager ;
116108 this .requestMatcher = requestMatcher ;
117- this .basicAuthenticationEntryPoint .setRealmName ("default" );
118109 // @formatter:off
119110 this .authenticationConverter = new DelegatingAuthenticationConverter (
120111 Arrays .asList (
@@ -138,16 +129,16 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
138129 Authentication authenticationRequest = null ;
139130 try {
140131 authenticationRequest = this .authenticationConverter .convert (request );
132+ if (authenticationRequest == null ) {
133+ throw new OAuth2AuthenticationException (OAuth2ErrorCodes .INVALID_CLIENT );
134+ }
141135 if (authenticationRequest instanceof AbstractAuthenticationToken authenticationToken ) {
142136 authenticationToken .setDetails (this .authenticationDetailsSource .buildDetails (request ));
143137 }
144- if (authenticationRequest != null ) {
145- validateClientIdentifier (authenticationRequest );
146- Authentication authenticationResult = this .authenticationManager .authenticate (authenticationRequest );
147- this .authenticationSuccessHandler .onAuthenticationSuccess (request , response , authenticationResult );
148- }
138+ validateClientIdentifier (authenticationRequest );
139+ Authentication authenticationResult = this .authenticationManager .authenticate (authenticationRequest );
140+ this .authenticationSuccessHandler .onAuthenticationSuccess (request , response , authenticationResult );
149141 filterChain .doFilter (request , response );
150-
151142 }
152143 catch (OAuth2AuthenticationException ex ) {
153144 if (this .logger .isTraceEnabled ()) {
@@ -160,8 +151,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
160151 else {
161152 this .authenticationFailureHandler .onAuthenticationFailure (request , response , ex );
162153 }
163-
164154 }
155+
165156 }
166157
167158 /**
@@ -211,35 +202,6 @@ private void onAuthenticationSuccess(HttpServletRequest request, HttpServletResp
211202 }
212203 }
213204
214- private void onAuthenticationFailure (HttpServletRequest request , HttpServletResponse response ,
215- AuthenticationException authenticationException ) throws IOException {
216-
217- SecurityContextHolder .clearContext ();
218-
219- if (authenticationException instanceof OAuth2ClientAuthenticationException clientAuthenticationException ) {
220- OAuth2ClientAuthenticationToken clientAuthentication = clientAuthenticationException
221- .getClientAuthentication ();
222- if (ClientAuthenticationMethod .CLIENT_SECRET_BASIC
223- .equals (clientAuthentication .getClientAuthenticationMethod ())) {
224- this .basicAuthenticationEntryPoint .commence (request , response , authenticationException );
225- return ;
226- }
227- }
228-
229- OAuth2Error error = ((OAuth2AuthenticationException ) authenticationException ).getError ();
230- ServletServerHttpResponse httpResponse = new ServletServerHttpResponse (response );
231- if (OAuth2ErrorCodes .INVALID_CLIENT .equals (error .getErrorCode ())) {
232- httpResponse .setStatusCode (HttpStatus .UNAUTHORIZED );
233- }
234- else {
235- httpResponse .setStatusCode (HttpStatus .BAD_REQUEST );
236- }
237- // We don't want to reveal too much information to the caller so just return the
238- // error code
239- OAuth2Error errorResponse = new OAuth2Error (error .getErrorCode ());
240- this .errorHttpResponseConverter .write (errorResponse , null , httpResponse );
241- }
242-
243205 private static void validateClientIdentifier (Authentication authentication ) {
244206 if (!(authentication instanceof OAuth2ClientAuthenticationToken )) {
245207 return ;
@@ -261,21 +223,4 @@ private static void validateClientIdentifier(Authentication authentication) {
261223 }
262224 }
263225
264- private static final class OAuth2ClientAuthenticationException extends OAuth2AuthenticationException {
265-
266- private final OAuth2ClientAuthenticationToken clientAuthentication ;
267-
268- private OAuth2ClientAuthenticationException (OAuth2Error error , Throwable cause ,
269- OAuth2ClientAuthenticationToken clientAuthentication ) {
270- super (error , cause );
271- Assert .notNull (clientAuthentication , "clientAuthentication cannot be null" );
272- this .clientAuthentication = clientAuthentication ;
273- }
274-
275- private OAuth2ClientAuthenticationToken getClientAuthentication () {
276- return this .clientAuthentication ;
277- }
278-
279- }
280-
281226}
0 commit comments