Add BasicAuthenticationInterceptor for client

This commit is contained in:
Dave Syer
2025-01-06 11:51:01 +00:00
parent f1a8299887
commit f3b0ac11e6
2 changed files with 66 additions and 26 deletions

View File

@@ -3,8 +3,6 @@ package org.springframework.grpc.sample;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
@@ -18,20 +16,13 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.grpc.client.ChannelBuilderOptions;
import org.springframework.grpc.client.GrpcChannelFactory;
import org.springframework.grpc.client.security.BasicAuthenticationInterceptor;
import org.springframework.grpc.sample.proto.HelloReply;
import org.springframework.grpc.sample.proto.HelloRequest;
import org.springframework.grpc.sample.proto.SimpleGrpc;
import org.springframework.grpc.server.security.GrpcSecurity;
import org.springframework.grpc.test.LocalGrpcPort;
import org.springframework.test.annotation.DirtiesContext;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.Status.Code;
import io.grpc.MethodDescriptor;
import io.grpc.StatusRuntimeException;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@@ -79,22 +70,9 @@ public class GrpcServerApplicationTests {
@Lazy
SimpleGrpc.SimpleBlockingStub basic(GrpcChannelFactory channels, @LocalServerPort int port) {
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port,
ChannelBuilderOptions.defaults().withCustomizer((__, channel) -> {
channel.intercept(new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
public void start(ClientCall.Listener<RespT> responseListener,
io.grpc.Metadata headers) {
headers.put(GrpcSecurity.AUTHORIZATION_KEY,
"Basic " + Base64.getEncoder().encodeToString("user:user".getBytes()));
super.start(responseListener, headers);
};
};
}
});
})));
ChannelBuilderOptions.defaults()
.withCustomizer((authority, channel) -> channel
.intercept(new BasicAuthenticationInterceptor("user", "user")))));
}
@Bean

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.grpc.client.security;
import java.util.Base64;
import org.springframework.grpc.server.security.GrpcSecurity;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.MethodDescriptor;
/**
* An interceptor that adds basic authentication headers to gRPC client calls. This
* interceptor is used to authenticate gRPC client calls with a username and password. The
* username and password are encoded using Base64 and added to the Authorization header.
*
* @author Dave Syer
*/
public class BasicAuthenticationInterceptor implements ClientInterceptor {
private final String username;
private final String password;
public BasicAuthenticationInterceptor(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
public void start(ClientCall.Listener<RespT> responseListener, io.grpc.Metadata headers) {
headers.put(GrpcSecurity.AUTHORIZATION_KEY,
"Basic " + Base64.getEncoder()
.encodeToString((BasicAuthenticationInterceptor.this.username + ":"
+ BasicAuthenticationInterceptor.this.password)
.getBytes()));
super.start(responseListener, headers);
};
};
}
}