Skip to content

Commit 67686f0

Browse files
committed
Make credentialsCharset in ServerHttpBasicAuthenticationConverter configurable
1 parent b633cb2 commit 67686f0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.web.server;
1818

19+
import java.nio.charset.Charset;
1920
import java.nio.charset.StandardCharsets;
2021
import java.util.Base64;
2122
import java.util.function.Function;
@@ -26,6 +27,7 @@
2627
import org.springframework.http.server.reactive.ServerHttpRequest;
2728
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
2829
import org.springframework.security.core.Authentication;
30+
import org.springframework.util.Assert;
2931
import org.springframework.util.StringUtils;
3032
import org.springframework.web.server.ServerWebExchange;
3133

@@ -44,6 +46,8 @@ public class ServerHttpBasicAuthenticationConverter implements Function<ServerWe
4446

4547
public static final String BASIC = "Basic ";
4648

49+
private Charset credentialsCharset = StandardCharsets.UTF_8;
50+
4751
@Override
4852
@Deprecated
4953
public Mono<Authentication> apply(ServerWebExchange exchange) {
@@ -53,7 +57,7 @@ public Mono<Authentication> apply(ServerWebExchange exchange) {
5357
return Mono.empty();
5458
}
5559
String credentials = (authorization.length() <= BASIC.length()) ? "" : authorization.substring(BASIC.length());
56-
String decoded = new String(base64Decode(credentials), StandardCharsets.UTF_8);
60+
String decoded = new String(base64Decode(credentials), this.credentialsCharset);
5761
String[] parts = decoded.split(":", 2);
5862
if (parts.length != 2) {
5963
return Mono.empty();
@@ -70,4 +74,13 @@ private byte[] base64Decode(String value) {
7074
}
7175
}
7276

77+
public Charset getCredentialsCharset() {
78+
return this.credentialsCharset;
79+
}
80+
81+
public void setCredentialsCharset(Charset credentialsCharset) {
82+
Assert.notNull(credentialsCharset, "credentialsCharset cannot be null");
83+
this.credentialsCharset = credentialsCharset;
84+
}
85+
7386
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.web.server.authentication;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import org.junit.jupiter.api.Test;
2022
import reactor.core.publisher.Mono;
2123

@@ -114,6 +116,28 @@ public void applyWhenNonAsciiThenAuthentication() {
114116
assertThat(authentication.getCredentials()).isEqualTo("passwörd");
115117
}
116118

119+
@Test
120+
public void applyWhenIsoOnlyAsciiThenAuthentication() {
121+
this.converter.setCredentialsCharset(StandardCharsets.ISO_8859_1);
122+
Mono<Authentication> result = apply(
123+
this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA=="));
124+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
125+
.block();
126+
assertThat(authentication.getPrincipal()).isEqualTo("user");
127+
assertThat(authentication.getCredentials()).isEqualTo("password");
128+
}
129+
130+
@Test
131+
public void applyWhenIsoNonAsciiThenAuthentication() {
132+
this.converter.setCredentialsCharset(StandardCharsets.ISO_8859_1);
133+
Mono<Authentication> result = apply(
134+
this.request.header(HttpHeaders.AUTHORIZATION, "Basic /HNlcjpwYXNzd/ZyZA=="));
135+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
136+
.block();
137+
assertThat(authentication.getPrincipal()).isEqualTo("üser");
138+
assertThat(authentication.getCredentials()).isEqualTo("passwörd");
139+
}
140+
117141
private Mono<Authentication> apply(MockServerHttpRequest.BaseBuilder<?> request) {
118142
return this.converter.convert(MockServerWebExchange.from(this.request.build()));
119143
}

0 commit comments

Comments
 (0)