Skip to content

Commit 5fd9010

Browse files
committed
Replace deprecated default authentication converter
Closes gh-18437 Signed-off-by: jihunparkkk <pjh2996@naver.com>
1 parent 87ab5e8 commit 5fd9010

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

rsocket/src/main/java/org/springframework/security/rsocket/authentication/AuthenticationPayloadInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class AuthenticationPayloadInterceptor implements PayloadInterceptor, Ord
4141

4242
private int order;
4343

44-
private PayloadExchangeAuthenticationConverter authenticationConverter = new BasicAuthenticationPayloadExchangeConverter();
44+
private PayloadExchangeAuthenticationConverter authenticationConverter = new AuthenticationPayloadExchangeConverter();
4545

4646
/**
4747
* Creates a new instance

rsocket/src/test/java/org/springframework/security/rsocket/authentication/AuthenticationPayloadInterceptorTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import java.util.Map;
2020

21+
import io.netty.buffer.ByteBuf;
2122
import io.netty.buffer.ByteBufAllocator;
2223
import io.netty.buffer.CompositeByteBuf;
2324
import io.rsocket.Payload;
25+
import io.rsocket.metadata.AuthMetadataCodec;
2426
import io.rsocket.metadata.CompositeMetadataCodec;
2527
import io.rsocket.metadata.WellKnownMimeType;
2628
import io.rsocket.util.DefaultPayload;
@@ -80,8 +82,10 @@ public void constructorWhenAuthenticationManagerNullThenException() {
8082
}
8183

8284
@Test
85+
@SuppressWarnings("deprecation")
8386
public void interceptWhenBasicCredentialsThenAuthenticates() {
8487
AuthenticationPayloadInterceptor interceptor = new AuthenticationPayloadInterceptor(this.authenticationManager);
88+
interceptor.setAuthenticationConverter(new BasicAuthenticationPayloadExchangeConverter());
8589
PayloadExchange exchange = createExchange();
8690
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password");
8791
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(expectedAuthentication));
@@ -95,8 +99,10 @@ public void interceptWhenBasicCredentialsThenAuthenticates() {
9599
}
96100

97101
@Test
102+
@SuppressWarnings("deprecation")
98103
public void interceptWhenAuthenticationSuccessThenChainSubscribedOnce() {
99104
AuthenticationPayloadInterceptor interceptor = new AuthenticationPayloadInterceptor(this.authenticationManager);
105+
interceptor.setAuthenticationConverter(new BasicAuthenticationPayloadExchangeConverter());
100106
PayloadExchange exchange = createExchange();
101107
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password");
102108
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(expectedAuthentication));
@@ -108,6 +114,7 @@ public void interceptWhenAuthenticationSuccessThenChainSubscribedOnce() {
108114
.verifyComplete();
109115
}
110116

117+
@SuppressWarnings("deprecation")
111118
private Payload createRequestPayload() {
112119
UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
113120
BasicAuthenticationEncoder encoder = new BasicAuthenticationEncoder();
@@ -128,4 +135,48 @@ private PayloadExchange createExchange() {
128135
COMPOSITE_METADATA, MediaType.APPLICATION_JSON);
129136
}
130137

138+
@Test
139+
public void interceptWhenSimpleCredentialsThenAuthenticates() {
140+
AuthenticationPayloadInterceptor interceptor = new AuthenticationPayloadInterceptor(this.authenticationManager);
141+
PayloadExchange exchange = createSimpleAuthExchange();
142+
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password");
143+
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(expectedAuthentication));
144+
AuthenticationPayloadInterceptorChain authenticationPayloadChain = new AuthenticationPayloadInterceptorChain();
145+
interceptor.intercept(exchange, authenticationPayloadChain).block();
146+
Authentication authentication = authenticationPayloadChain.getAuthentication();
147+
verify(this.authenticationManager).authenticate(this.authenticationArg.capture());
148+
assertThat(this.authenticationArg.getValue()).usingRecursiveComparison()
149+
.isEqualTo(UsernamePasswordAuthenticationToken.unauthenticated("user", "password"));
150+
assertThat(authentication).isEqualTo(expectedAuthentication);
151+
}
152+
153+
@Test
154+
public void interceptWhenSimpleAuthenticationSuccessThenChainSubscribedOnce() {
155+
AuthenticationPayloadInterceptor interceptor = new AuthenticationPayloadInterceptor(this.authenticationManager);
156+
PayloadExchange exchange = createSimpleAuthExchange();
157+
TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password");
158+
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(expectedAuthentication));
159+
PublisherProbe<Void> voidResult = PublisherProbe.empty();
160+
PayloadInterceptorChain chain = mock(PayloadInterceptorChain.class);
161+
given(chain.next(any())).willReturn(voidResult.mono());
162+
StepVerifier.create(interceptor.intercept(exchange, chain))
163+
.then(() -> assertThat(voidResult.subscribeCount()).isEqualTo(1))
164+
.verifyComplete();
165+
}
166+
167+
private Payload createSimpleAuthPayload() {
168+
ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
169+
CompositeByteBuf metadata = allocator.compositeBuffer();
170+
ByteBuf authMetadata = AuthMetadataCodec.encodeSimpleMetadata(allocator, "user".toCharArray(),
171+
"password".toCharArray());
172+
CompositeMetadataCodec.encodeAndAddMetadata(metadata, allocator,
173+
WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION, authMetadata);
174+
return DefaultPayload.create(allocator.buffer(), metadata);
175+
}
176+
177+
private PayloadExchange createSimpleAuthExchange() {
178+
return new DefaultPayloadExchange(PayloadExchangeType.REQUEST_RESPONSE, createSimpleAuthPayload(),
179+
COMPOSITE_METADATA, MediaType.APPLICATION_JSON);
180+
}
181+
131182
}

rsocket/src/test/java/org/springframework/security/rsocket/metadata/BasicAuthenticationDecoderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/**
3232
* @author Rob Winch
3333
*/
34+
@SuppressWarnings("deprecation")
3435
public class BasicAuthenticationDecoderTests {
3536

3637
@Test

0 commit comments

Comments
 (0)