Skip to content

Commit e3eaa99

Browse files
committed
Polish ServerAuthenticationConverter
Update changes for ServerAuthenticationConverter to be passive. Issue: gh-5338
1 parent b6afe66 commit e3eaa99

File tree

8 files changed

+51
-15
lines changed

8 files changed

+51
-15
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ protected void configure(ServerHttpSecurity http) {
485485

486486
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(manager);
487487
authenticationFilter.setRequiresAuthenticationMatcher(new PathPatternParserServerWebExchangeMatcher("/login/oauth2/code/{registrationId}"));
488-
authenticationFilter.setAuthenticationConverter(new ServerOAuth2LoginAuthenticationTokenConverter(clientRegistrationRepository));
488+
authenticationFilter.setServerAuthenticationConverter(new ServerOAuth2LoginAuthenticationTokenConverter(clientRegistrationRepository));
489489

490490
RedirectServerAuthenticationSuccessHandler redirectHandler = new RedirectServerAuthenticationSuccessHandler();
491491

@@ -651,7 +651,7 @@ protected void configure(ServerHttpSecurity http) {
651651
JwtReactiveAuthenticationManager authenticationManager = new JwtReactiveAuthenticationManager(
652652
this.jwtDecoder);
653653
AuthenticationWebFilter oauth2 = new AuthenticationWebFilter(authenticationManager);
654-
oauth2.setAuthenticationConverter(new ServerBearerTokenAuthenticationConverter());
654+
oauth2.setServerAuthenticationConverter(new ServerBearerTokenAuthenticationConverter());
655655
oauth2.setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(entryPoint));
656656
http
657657
.exceptionHandling()

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/ServerOAuth2LoginAuthenticationTokenConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
* converter does not validate any errors it only performs a conversion.
3939
* @author Rob Winch
4040
* @since 5.1
41-
* @see org.springframework.security.web.server.authentication.AuthenticationWebFilter#setAuthenticationConverter(ServerAuthenticationConverter)
41+
* @see org.springframework.security.web.server.authentication.AuthenticationWebFilter#setServerAuthenticationConverter(ServerAuthenticationConverter)
4242
*/
43-
public class ServerOAuth2LoginAuthenticationTokenConverter implements ServerAuthenticationConverter {
43+
public class ServerOAuth2LoginAuthenticationTokenConverter
44+
implements ServerAuthenticationConverter {
4445

4546
static final String AUTHORIZATION_REQUEST_NOT_FOUND_ERROR_CODE = "authorization_request_not_found";
4647

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/ServerOAuth2LoginAuthenticationTokenConverterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public void applyWhenAdditionalParametersMissingThenOAuth2AuthenticationExceptio
102102

103103
assertThatThrownBy(() -> applyConverter())
104104
.isInstanceOf(OAuth2AuthenticationException.class)
105-
.hasMessageContaining(ServerOAuth2LoginAuthenticationTokenConverter.CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE);
105+
.hasMessageContaining(
106+
ServerOAuth2LoginAuthenticationTokenConverter.CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE);
106107
}
107108

108109
@Test
@@ -112,7 +113,8 @@ public void applyWhenClientRegistrationMissingThenOAuth2AuthenticationException(
112113

113114
assertThatThrownBy(() -> applyConverter())
114115
.isInstanceOf(OAuth2AuthenticationException.class)
115-
.hasMessageContaining(ServerOAuth2LoginAuthenticationTokenConverter.CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE);
116+
.hasMessageContaining(
117+
ServerOAuth2LoginAuthenticationTokenConverter.CLIENT_REGISTRATION_NOT_FOUND_ERROR_CODE);
116118
}
117119

118120
@Test

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* @since 5.1
4242
* @see <a href="https://tools.ietf.org/html/rfc6750#section-2" target="_blank">RFC 6750 Section 2: Authenticated Requests</a>
4343
*/
44-
public class ServerBearerTokenAuthenticationConverter implements ServerAuthenticationConverter {
44+
public class ServerBearerTokenAuthenticationConverter
45+
implements ServerAuthenticationConverter {
4546
private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$");
4647

4748
private boolean allowUriQueryParameter = false;

web/src/main/java/org/springframework/security/web/server/ServerFormLoginAuthenticationConverter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import org.springframework.util.MultiValueMap;
2525
import org.springframework.web.server.ServerWebExchange;
2626

27+
import java.util.function.Function;
28+
2729
/**
2830
* Converts a ServerWebExchange into a UsernamePasswordAuthenticationToken from the form
2931
* data HTTP parameters.
3032
*
3133
* @author Rob Winch
3234
* @since 5.0
3335
*/
34-
public class ServerFormLoginAuthenticationConverter implements ServerAuthenticationConverter {
36+
public class ServerFormLoginAuthenticationConverter implements
37+
ServerAuthenticationConverter,
38+
Function<ServerWebExchange, Mono<Authentication>> {
3539

3640
private String usernameParameter = "username";
3741

@@ -43,6 +47,18 @@ public Mono<Authentication> convert(ServerWebExchange exchange) {
4347
.map( data -> createAuthentication(data));
4448
}
4549

50+
/**
51+
* Alias for {@link #convert(ServerWebExchange)}
52+
* @param exchange the {@link ServerWebExchange} to use
53+
* @return the {@link Authentication}
54+
* @deprecated Use {@link #convert(ServerWebExchange)}
55+
*/
56+
@Override
57+
@Deprecated
58+
public Mono<Authentication> apply(ServerWebExchange exchange) {
59+
return convert(exchange);
60+
}
61+
4662
private UsernamePasswordAuthenticationToken createAuthentication(
4763
MultiValueMap<String, String> data) {
4864
String username = data.getFirst(this.usernameParameter);

web/src/main/java/org/springframework/security/web/server/ServerHttpBasicAuthenticationConverter.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.security.web.server;
1717

1818
import java.util.Base64;
19+
import java.util.function.Function;
1920

2021
import org.springframework.http.HttpHeaders;
2122
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -32,7 +33,9 @@
3233
* @author Rob Winch
3334
* @since 5.0
3435
*/
35-
public class ServerHttpBasicAuthenticationConverter implements ServerAuthenticationConverter {
36+
public class ServerHttpBasicAuthenticationConverter implements
37+
ServerAuthenticationConverter,
38+
Function<ServerWebExchange, Mono<Authentication>> {
3639

3740
public static final String BASIC = "Basic ";
3841

@@ -61,6 +64,18 @@ public Mono<Authentication> convert(ServerWebExchange exchange) {
6164
return Mono.just(new UsernamePasswordAuthenticationToken(username, password));
6265
}
6366

67+
/**
68+
* Alias for {@link #convert(ServerWebExchange)}
69+
* @param exchange the {@link ServerWebExchange} to use
70+
* @return the {@link Authentication}
71+
* @deprecated Use {@link #convert(ServerWebExchange)}
72+
*/
73+
@Override
74+
@Deprecated
75+
public Mono<Authentication> apply(ServerWebExchange exchange) {
76+
return convert(exchange);
77+
}
78+
6479
private byte[] base64Decode(String value) {
6580
try {
6681
return Base64.getDecoder().decode(value);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ public void setAuthenticationSuccessHandler(ServerAuthenticationSuccessHandler a
138138
* that no authentication attempt should be made. The default converter is
139139
* {@link ServerHttpBasicAuthenticationConverter}
140140
* @param authenticationConverter the converter to use
141-
* @deprecated As of 5.1 in favor of {@link #setAuthenticationConverter(ServerAuthenticationConverter)}
142-
* @see #setAuthenticationConverter(ServerAuthenticationConverter)
141+
* @deprecated As of 5.1 in favor of {@link #setServerAuthenticationConverter(ServerAuthenticationConverter)}
142+
* @see #setServerAuthenticationConverter(ServerAuthenticationConverter)
143143
*/
144144
@Deprecated
145145
public void setAuthenticationConverter(Function<ServerWebExchange, Mono<Authentication>> authenticationConverter) {
146146
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
147-
setAuthenticationConverter((ServerAuthenticationConverter) authenticationConverter);
147+
setServerAuthenticationConverter(authenticationConverter::apply);
148148
}
149149

150150
/**
@@ -155,7 +155,8 @@ public void setAuthenticationConverter(Function<ServerWebExchange, Mono<Authenti
155155
* @param authenticationConverter the converter to use
156156
* @since 5.1
157157
*/
158-
public void setAuthenticationConverter(ServerAuthenticationConverter authenticationConverter) {
158+
public void setServerAuthenticationConverter(
159+
ServerAuthenticationConverter authenticationConverter) {
159160
Assert.notNull(authenticationConverter, "authenticationConverter cannot be null");
160161
this.authenticationConverter = authenticationConverter;
161162
}
@@ -172,7 +173,7 @@ public void setAuthenticationFailureHandler(
172173

173174
/**
174175
* Sets the matcher used to determine when creating an {@link Authentication} from
175-
* {@link #setAuthenticationConverter(ServerAuthenticationConverter)} to be authentication. If the converter returns an empty
176+
* {@link #setServerAuthenticationConverter(ServerAuthenticationConverter)} to be authentication. If the converter returns an empty
176177
* result, then no authentication is attempted. The default is any request
177178
* @param requiresAuthenticationMatcher the matcher to use. Cannot be null.
178179
*/

web/src/test/java/org/springframework/security/web/server/authentication/AuthenticationWebFilterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class AuthenticationWebFilterTests {
6161
public void setup() {
6262
this.filter = new AuthenticationWebFilter(this.authenticationManager);
6363
this.filter.setAuthenticationSuccessHandler(this.successHandler);
64-
this.filter.setAuthenticationConverter(this.authenticationConverter);
64+
this.filter.setServerAuthenticationConverter(this.authenticationConverter);
6565
this.filter.setSecurityContextRepository(this.securityContextRepository);
6666
this.filter.setAuthenticationFailureHandler(this.failureHandler);
6767
}

0 commit comments

Comments
 (0)