59
59
* @since 0.0.1
60
60
* @see AuthenticationManager
61
61
* @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>
64
64
*/
65
65
public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter {
66
66
private final AuthenticationManager authenticationManager ;
@@ -69,8 +69,8 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter
69
69
private final AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource =
70
70
new WebAuthenticationDetailsSource ();
71
71
private AuthenticationConverter authenticationConverter ;
72
- private AuthenticationSuccessHandler authenticationSuccessHandler ;
73
- private AuthenticationFailureHandler authenticationFailureHandler ;
72
+ private AuthenticationSuccessHandler authenticationSuccessHandler = this :: onAuthenticationSuccess ;
73
+ private AuthenticationFailureHandler authenticationFailureHandler = this :: onAuthenticationFailure ;
74
74
75
75
/**
76
76
* Constructs an {@code OAuth2ClientAuthenticationFilter} using the provided parameters.
@@ -89,57 +89,61 @@ public OAuth2ClientAuthenticationFilter(AuthenticationManager authenticationMana
89
89
new ClientSecretBasicAuthenticationConverter (),
90
90
new ClientSecretPostAuthenticationConverter (),
91
91
new PublicClientAuthenticationConverter ()));
92
- this .authenticationSuccessHandler = this ::onAuthenticationSuccess ;
93
- this .authenticationFailureHandler = this ::onAuthenticationFailure ;
94
92
}
95
93
96
94
@ Override
97
95
protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain filterChain )
98
96
throws ServletException , IOException {
99
97
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 ) ;
114
112
}
113
+ filterChain .doFilter (request , response );
114
+
115
+ } catch (OAuth2AuthenticationException ex ) {
116
+ this .authenticationFailureHandler .onAuthenticationFailure (request , response , ex );
115
117
}
116
- filterChain .doFilter (request , response );
117
118
}
118
119
119
120
/**
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.
121
123
*
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 }
123
125
*/
124
126
public void setAuthenticationConverter (AuthenticationConverter authenticationConverter ) {
125
127
Assert .notNull (authenticationConverter , "authenticationConverter cannot be null" );
126
128
this .authenticationConverter = authenticationConverter ;
127
129
}
128
130
129
131
/**
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}.
131
134
*
132
- * @param authenticationSuccessHandler the {@link AuthenticationSuccessHandler} used for handling successful authentications
135
+ * @param authenticationSuccessHandler the {@link AuthenticationSuccessHandler} used for handling a successful client authentication
133
136
*/
134
137
public void setAuthenticationSuccessHandler (AuthenticationSuccessHandler authenticationSuccessHandler ) {
135
138
Assert .notNull (authenticationSuccessHandler , "authenticationSuccessHandler cannot be null" );
136
139
this .authenticationSuccessHandler = authenticationSuccessHandler ;
137
140
}
138
141
139
142
/**
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}.
141
145
*
142
- * @param authenticationFailureHandler the {@link AuthenticationFailureHandler} used for handling failed authentications
146
+ * @param authenticationFailureHandler the {@link AuthenticationFailureHandler} used for handling a failed client authentication
143
147
*/
144
148
public void setAuthenticationFailureHandler (AuthenticationFailureHandler authenticationFailureHandler ) {
145
149
Assert .notNull (authenticationFailureHandler , "authenticationFailureHandler cannot be null" );
@@ -149,13 +153,13 @@ public void setAuthenticationFailureHandler(AuthenticationFailureHandler authent
149
153
private void onAuthenticationSuccess (HttpServletRequest request , HttpServletResponse response ,
150
154
Authentication authentication ) {
151
155
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 );
155
159
}
156
160
157
161
private void onAuthenticationFailure (HttpServletRequest request , HttpServletResponse response ,
158
- AuthenticationException failed ) throws IOException {
162
+ AuthenticationException exception ) throws IOException {
159
163
160
164
SecurityContextHolder .clearContext ();
161
165
@@ -167,7 +171,7 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
167
171
// include the "WWW-Authenticate" response header field
168
172
// matching the authentication scheme used by the client.
169
173
170
- OAuth2Error error = ((OAuth2AuthenticationException ) failed ).getError ();
174
+ OAuth2Error error = ((OAuth2AuthenticationException ) exception ).getError ();
171
175
ServletServerHttpResponse httpResponse = new ServletServerHttpResponse (response );
172
176
if (OAuth2ErrorCodes .INVALID_CLIENT .equals (error .getErrorCode ())) {
173
177
httpResponse .setStatusCode (HttpStatus .UNAUTHORIZED );
@@ -176,4 +180,5 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
176
180
}
177
181
this .errorHttpResponseConverter .write (error , null , httpResponse );
178
182
}
183
+
179
184
}
0 commit comments