|
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 | 33 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
38 | 34 | import org.springframework.security.oauth2.core.OAuth2Error;
|
39 | 35 | import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
40 |
| -import org.springframework.security.oauth2.core.http.converter.OAuth2ErrorHttpMessageConverter; |
41 | 36 | import org.springframework.security.oauth2.server.authorization.authentication.ClientSecretAuthenticationProvider;
|
42 | 37 | import org.springframework.security.oauth2.server.authorization.authentication.JwtClientAssertionAuthenticationProvider;
|
43 | 38 | import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
|
|
46 | 41 | import org.springframework.security.oauth2.server.authorization.web.authentication.ClientSecretBasicAuthenticationConverter;
|
47 | 42 | import org.springframework.security.oauth2.server.authorization.web.authentication.ClientSecretPostAuthenticationConverter;
|
48 | 43 | import org.springframework.security.oauth2.server.authorization.web.authentication.JwtClientAssertionAuthenticationConverter;
|
| 44 | +import org.springframework.security.oauth2.server.authorization.web.authentication.OAuth2ErrorAuthenticationFailureHandler; |
49 | 45 | import org.springframework.security.oauth2.server.authorization.web.authentication.PublicClientAuthenticationConverter;
|
50 | 46 | import org.springframework.security.oauth2.server.authorization.web.authentication.X509ClientCertificateAuthenticationConverter;
|
51 | 47 | import org.springframework.security.web.authentication.AuthenticationConverter;
|
@@ -86,15 +82,13 @@ public final class OAuth2ClientAuthenticationFilter extends OncePerRequestFilter
|
86 | 82 |
|
87 | 83 | private final RequestMatcher requestMatcher;
|
88 | 84 |
|
89 |
| - private final HttpMessageConverter<OAuth2Error> errorHttpResponseConverter = new OAuth2ErrorHttpMessageConverter(); |
90 |
| - |
91 | 85 | private final AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new WebAuthenticationDetailsSource();
|
92 | 86 |
|
93 | 87 | private AuthenticationConverter authenticationConverter;
|
94 | 88 |
|
95 | 89 | private AuthenticationSuccessHandler authenticationSuccessHandler = this::onAuthenticationSuccess;
|
96 | 90 |
|
97 |
| - private AuthenticationFailureHandler authenticationFailureHandler = this::onAuthenticationFailure; |
| 91 | + private AuthenticationFailureHandler authenticationFailureHandler = new OAuth2ErrorAuthenticationFailureHandler(); |
98 | 92 |
|
99 | 93 | /**
|
100 | 94 | * Constructs an {@code OAuth2ClientAuthenticationFilter} using the provided
|
@@ -199,35 +193,6 @@ private void onAuthenticationSuccess(HttpServletRequest request, HttpServletResp
|
199 | 193 | }
|
200 | 194 | }
|
201 | 195 |
|
202 |
| - private void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, |
203 |
| - AuthenticationException exception) throws IOException { |
204 |
| - |
205 |
| - SecurityContextHolder.clearContext(); |
206 |
| - |
207 |
| - // TODO |
208 |
| - // The authorization server MAY return an HTTP 401 (Unauthorized) status code |
209 |
| - // to indicate which HTTP authentication schemes are supported. |
210 |
| - // If the client attempted to authenticate via the "Authorization" request header |
211 |
| - // field, |
212 |
| - // the authorization server MUST respond with an HTTP 401 (Unauthorized) status |
213 |
| - // code and |
214 |
| - // include the "WWW-Authenticate" response header field |
215 |
| - // matching the authentication scheme used by the client. |
216 |
| - |
217 |
| - OAuth2Error error = ((OAuth2AuthenticationException) exception).getError(); |
218 |
| - ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); |
219 |
| - if (OAuth2ErrorCodes.INVALID_CLIENT.equals(error.getErrorCode())) { |
220 |
| - httpResponse.setStatusCode(HttpStatus.UNAUTHORIZED); |
221 |
| - } |
222 |
| - else { |
223 |
| - httpResponse.setStatusCode(HttpStatus.BAD_REQUEST); |
224 |
| - } |
225 |
| - // We don't want to reveal too much information to the caller so just return the |
226 |
| - // error code |
227 |
| - OAuth2Error errorResponse = new OAuth2Error(error.getErrorCode()); |
228 |
| - this.errorHttpResponseConverter.write(errorResponse, null, httpResponse); |
229 |
| - } |
230 |
| - |
231 | 196 | private static void validateClientIdentifier(Authentication authentication) {
|
232 | 197 | if (!(authentication instanceof OAuth2ClientAuthenticationToken)) {
|
233 | 198 | return;
|
|
0 commit comments