Skip to content

Commit 099aaa3

Browse files
committed
Remove Deprecation Markers
Since Spring Security still needs these methods and classes, we should wait on deprecating them if we can. Instead, this commit changes the original classes to have a boolean property that is currently false, but will switch to true in 6.0. At that time, BearerTokenAuthenticationFilter can change to use the handler. Closes gh-11932
1 parent 200b7fe commit 099aaa3

File tree

9 files changed

+114
-271
lines changed

9 files changed

+114
-271
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/authentication/BearerTokenAuthenticationFilter.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.security.authentication.AuthenticationDetailsSource;
2828
import org.springframework.security.authentication.AuthenticationManager;
2929
import org.springframework.security.authentication.AuthenticationManagerResolver;
30+
import org.springframework.security.authentication.AuthenticationServiceException;
3031
import org.springframework.security.core.Authentication;
3132
import org.springframework.security.core.AuthenticationException;
3233
import org.springframework.security.core.context.SecurityContext;
@@ -39,7 +40,6 @@
3940
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;
4041
import org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver;
4142
import org.springframework.security.web.AuthenticationEntryPoint;
42-
import org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandlerAdapter;
4343
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
4444
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
4545
import org.springframework.security.web.context.NullSecurityContextRepository;
@@ -73,12 +73,12 @@ public class BearerTokenAuthenticationFilter extends OncePerRequestFilter {
7373

7474
private AuthenticationEntryPoint authenticationEntryPoint = new BearerTokenAuthenticationEntryPoint();
7575

76-
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationEntryPointFailureHandlerAdapter(
77-
(request, response, authException) -> {
78-
// This is a lambda and not a method reference so that the FailureHandler
79-
// reflects entrypoint updates
80-
this.authenticationEntryPoint.commence(request, response, authException);
81-
});
76+
private AuthenticationFailureHandler authenticationFailureHandler = (request, response, exception) -> {
77+
if (exception instanceof AuthenticationServiceException) {
78+
throw exception;
79+
}
80+
this.authenticationEntryPoint.commence(request, response, exception);
81+
};
8282

8383
private BearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver();
8484

@@ -192,10 +192,7 @@ public void setBearerTokenResolver(BearerTokenResolver bearerTokenResolver) {
192192
* Set the {@link AuthenticationEntryPoint} to use. Defaults to
193193
* {@link BearerTokenAuthenticationEntryPoint}.
194194
* @param authenticationEntryPoint the {@code AuthenticationEntryPoint} to use
195-
* @deprecated use
196-
* {@link BearerTokenAuthenticationFilter#authenticationFailureHandler} instead
197195
*/
198-
@Deprecated
199196
public void setAuthenticationEntryPoint(final AuthenticationEntryPoint authenticationEntryPoint) {
200197
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
201198
this.authenticationEntryPoint = authenticationEntryPoint;

web/src/main/java/org/springframework/security/web/authentication/AuthenticationEntryPointFailureHandler.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.springframework.security.authentication.AuthenticationServiceException;
2526
import org.springframework.security.core.AuthenticationException;
2627
import org.springframework.security.web.AuthenticationEntryPoint;
2728
import org.springframework.util.Assert;
@@ -31,11 +32,11 @@
3132
*
3233
* @author Sergey Bespalov
3334
* @since 5.2.0
34-
* @deprecated Use {@link AuthenticationEntryPointFailureHandlerAdapter} instead
3535
*/
36-
@Deprecated
3736
public class AuthenticationEntryPointFailureHandler implements AuthenticationFailureHandler {
3837

38+
private boolean rethrowAuthenticationServiceException = false;
39+
3940
private final AuthenticationEntryPoint authenticationEntryPoint;
4041

4142
public AuthenticationEntryPointFailureHandler(AuthenticationEntryPoint authenticationEntryPoint) {
@@ -46,7 +47,25 @@ public AuthenticationEntryPointFailureHandler(AuthenticationEntryPoint authentic
4647
@Override
4748
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
4849
AuthenticationException exception) throws IOException, ServletException {
49-
this.authenticationEntryPoint.commence(request, response, exception);
50+
if (!this.rethrowAuthenticationServiceException) {
51+
this.authenticationEntryPoint.commence(request, response, exception);
52+
return;
53+
}
54+
if (!AuthenticationServiceException.class.isAssignableFrom(exception.getClass())) {
55+
this.authenticationEntryPoint.commence(request, response, exception);
56+
return;
57+
}
58+
throw exception;
59+
}
60+
61+
/**
62+
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to false)
63+
* @param rethrowAuthenticationServiceException whether to rethrow
64+
* {@link AuthenticationServiceException}s
65+
* @since 5.8
66+
*/
67+
public void setRethrowAuthenticationServiceException(boolean rethrowAuthenticationServiceException) {
68+
this.rethrowAuthenticationServiceException = rethrowAuthenticationServiceException;
5069
}
5170

5271
}

web/src/main/java/org/springframework/security/web/authentication/AuthenticationEntryPointFailureHandlerAdapter.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

web/src/main/java/org/springframework/security/web/server/authentication/ServerAuthenticationEntryPointFailureHandler.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import reactor.core.publisher.Mono;
2020

21+
import org.springframework.security.authentication.AuthenticationServiceException;
2122
import org.springframework.security.core.AuthenticationException;
2223
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
2324
import org.springframework.security.web.server.WebFilterExchange;
@@ -29,21 +30,37 @@
2930
*
3031
* @author Rob Winch
3132
* @since 5.0
32-
* @deprecated use {@link ServerAuthenticationEntryPointFailureHandlerAdapter} instead.
3333
*/
34-
@Deprecated
3534
public class ServerAuthenticationEntryPointFailureHandler implements ServerAuthenticationFailureHandler {
3635

3736
private final ServerAuthenticationEntryPoint authenticationEntryPoint;
3837

38+
private boolean rethrowAuthenticationServiceException = false;
39+
3940
public ServerAuthenticationEntryPointFailureHandler(ServerAuthenticationEntryPoint authenticationEntryPoint) {
4041
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
4142
this.authenticationEntryPoint = authenticationEntryPoint;
4243
}
4344

4445
@Override
4546
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) {
46-
return this.authenticationEntryPoint.commence(webFilterExchange.getExchange(), exception);
47+
if (!this.rethrowAuthenticationServiceException) {
48+
return this.authenticationEntryPoint.commence(webFilterExchange.getExchange(), exception);
49+
}
50+
if (!AuthenticationServiceException.class.isAssignableFrom(exception.getClass())) {
51+
return this.authenticationEntryPoint.commence(webFilterExchange.getExchange(), exception);
52+
}
53+
return Mono.error(exception);
54+
}
55+
56+
/**
57+
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to false)
58+
* @param rethrowAuthenticationServiceException whether to rethrow
59+
* {@link AuthenticationServiceException}s
60+
* @since 5.8
61+
*/
62+
public void setRethrowAuthenticationServiceException(boolean rethrowAuthenticationServiceException) {
63+
this.rethrowAuthenticationServiceException = rethrowAuthenticationServiceException;
4764
}
4865

4966
}

web/src/main/java/org/springframework/security/web/server/authentication/ServerAuthenticationEntryPointFailureHandlerAdapter.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

web/src/test/java/org/springframework/security/web/authentication/AuthenticationEntryPointFailureHandlerAdapterTest.java

Lines changed: 0 additions & 69 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.authentication;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.security.authentication.AuthenticationServiceException;
22+
import org.springframework.security.web.AuthenticationEntryPoint;
23+
24+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25+
import static org.mockito.Mockito.mock;
26+
27+
/**
28+
* Tests for {@link AuthenticationEntryPointFailureHandler}
29+
*/
30+
public class AuthenticationEntryPointFailureHandlerTests {
31+
32+
@Test
33+
void onAuthenticationFailureWhenDefaultsThenAuthenticationServiceExceptionSwallowed() throws Exception {
34+
AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class);
35+
AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(entryPoint);
36+
handler.onAuthenticationFailure(null, null, new AuthenticationServiceException("fail"));
37+
}
38+
39+
@Test
40+
void handleWhenRethrowingThenAuthenticationServiceExceptionRethrown() {
41+
AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class);
42+
AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(entryPoint);
43+
handler.setRethrowAuthenticationServiceException(true);
44+
assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(
45+
() -> handler.onAuthenticationFailure(null, null, new AuthenticationServiceException("fail")));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)