24
24
import jakarta .servlet .http .HttpServletResponse ;
25
25
26
26
import 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 ;
30
27
import org .springframework .security .authentication .AbstractAuthenticationToken ;
31
28
import org .springframework .security .authentication .AuthenticationDetailsSource ;
32
29
import org .springframework .security .authentication .AuthenticationManager ;
33
30
import org .springframework .security .core .Authentication ;
34
- import org .springframework .security .core .AuthenticationException ;
35
31
import org .springframework .security .core .context .SecurityContext ;
36
32
import org .springframework .security .core .context .SecurityContextHolder ;
37
- import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
38
33
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
39
34
import org .springframework .security .oauth2 .core .OAuth2Error ;
40
35
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
41
- import org .springframework .security .oauth2 .core .http .converter .OAuth2ErrorHttpMessageConverter ;
42
36
import org .springframework .security .oauth2 .server .authorization .authentication .ClientSecretAuthenticationProvider ;
43
37
import org .springframework .security .oauth2 .server .authorization .authentication .JwtClientAssertionAuthenticationProvider ;
38
+ import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2ClientAuthenticationException ;
44
39
import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2ClientAuthenticationToken ;
45
40
import org .springframework .security .oauth2 .server .authorization .authentication .PublicClientAuthenticationProvider ;
46
41
import org .springframework .security .oauth2 .server .authorization .authentication .X509ClientCertificateAuthenticationProvider ;
47
42
import org .springframework .security .oauth2 .server .authorization .web .authentication .ClientSecretBasicAuthenticationConverter ;
48
43
import org .springframework .security .oauth2 .server .authorization .web .authentication .ClientSecretPostAuthenticationConverter ;
49
44
import org .springframework .security .oauth2 .server .authorization .web .authentication .JwtClientAssertionAuthenticationConverter ;
45
+ import org .springframework .security .oauth2 .server .authorization .web .authentication .OAuth2ClientAuthenticationFailureHandler ;
50
46
import org .springframework .security .oauth2 .server .authorization .web .authentication .PublicClientAuthenticationConverter ;
51
47
import org .springframework .security .oauth2 .server .authorization .web .authentication .X509ClientCertificateAuthenticationConverter ;
52
48
import org .springframework .security .web .authentication .AuthenticationConverter ;
53
49
import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
54
50
import org .springframework .security .web .authentication .AuthenticationSuccessHandler ;
55
51
import org .springframework .security .web .authentication .DelegatingAuthenticationConverter ;
56
52
import org .springframework .security .web .authentication .WebAuthenticationDetailsSource ;
57
- import org .springframework .security .web .authentication .www .BasicAuthenticationEntryPoint ;
58
53
import org .springframework .security .web .util .matcher .RequestMatcher ;
59
54
import org .springframework .util .Assert ;
60
55
import org .springframework .web .filter .OncePerRequestFilter ;
75
70
* @see ClientSecretAuthenticationProvider
76
71
* @see PublicClientAuthenticationConverter
77
72
* @see PublicClientAuthenticationProvider
73
+ * @see OAuth2ClientAuthenticationFailureHandler
78
74
* @see <a target="_blank" href=
79
75
* "https://datatracker.ietf.org/doc/html/rfc6749#section-2.3">Section 2.3 Client
80
76
* Authentication</a>
@@ -88,17 +84,13 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter
88
84
89
85
private final RequestMatcher requestMatcher ;
90
86
91
- private final HttpMessageConverter <OAuth2Error > errorHttpResponseConverter = new OAuth2ErrorHttpMessageConverter ();
92
-
93
87
private final AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource = new WebAuthenticationDetailsSource ();
94
88
95
- private final BasicAuthenticationEntryPoint basicAuthenticationEntryPoint = new BasicAuthenticationEntryPoint ();
96
-
97
89
private AuthenticationConverter authenticationConverter ;
98
90
99
91
private AuthenticationSuccessHandler authenticationSuccessHandler = this ::onAuthenticationSuccess ;
100
92
101
- private AuthenticationFailureHandler authenticationFailureHandler = this :: onAuthenticationFailure ;
93
+ private AuthenticationFailureHandler authenticationFailureHandler = new OAuth2ClientAuthenticationFailureHandler () ;
102
94
103
95
/**
104
96
* Constructs an {@code OAuth2ClientAuthenticationFilter} using the provided
@@ -114,7 +106,6 @@ public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationMana
114
106
Assert .notNull (requestMatcher , "requestMatcher cannot be null" );
115
107
this .authenticationManager = authenticationManager ;
116
108
this .requestMatcher = requestMatcher ;
117
- this .basicAuthenticationEntryPoint .setRealmName ("default" );
118
109
// @formatter:off
119
110
this .authenticationConverter = new DelegatingAuthenticationConverter (
120
111
Arrays .asList (
@@ -138,16 +129,16 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
138
129
Authentication authenticationRequest = null ;
139
130
try {
140
131
authenticationRequest = this .authenticationConverter .convert (request );
132
+ if (authenticationRequest == null ) {
133
+ throw new OAuth2AuthenticationException (OAuth2ErrorCodes .INVALID_CLIENT );
134
+ }
141
135
if (authenticationRequest instanceof AbstractAuthenticationToken authenticationToken ) {
142
136
authenticationToken .setDetails (this .authenticationDetailsSource .buildDetails (request ));
143
137
}
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 );
149
141
filterChain .doFilter (request , response );
150
-
151
142
}
152
143
catch (OAuth2AuthenticationException ex ) {
153
144
if (this .logger .isTraceEnabled ()) {
@@ -160,8 +151,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
160
151
else {
161
152
this .authenticationFailureHandler .onAuthenticationFailure (request , response , ex );
162
153
}
163
-
164
154
}
155
+
165
156
}
166
157
167
158
/**
@@ -211,35 +202,6 @@ private void onAuthenticationSuccess(HttpServletRequest request, HttpServletResp
211
202
}
212
203
}
213
204
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
-
243
205
private static void validateClientIdentifier (Authentication authentication ) {
244
206
if (!(authentication instanceof OAuth2ClientAuthenticationToken )) {
245
207
return ;
@@ -261,21 +223,4 @@ private static void validateClientIdentifier(Authentication authentication) {
261
223
}
262
224
}
263
225
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
-
281
226
}
0 commit comments