|
2 | 2 |
|
3 | 3 | import io.grpc.Status; |
4 | 4 | import io.grpc.StatusRuntimeException; |
| 5 | +import io.grpc.examples.CalculatorOuterClass; |
| 6 | +import io.grpc.examples.SecuredCalculatorGrpc; |
5 | 7 | import io.grpc.examples.tasks.Assignment; |
6 | 8 | import io.grpc.examples.tasks.Person; |
7 | 9 | import io.grpc.examples.tasks.TaskServiceGrpc; |
|
10 | 12 | import org.junit.Assert; |
11 | 13 | import org.junit.Test; |
12 | 14 | import org.junit.runner.RunWith; |
| 15 | +import org.lognet.springboot.grpc.GRpcService; |
13 | 16 | import org.lognet.springboot.grpc.GrpcServerTestBase; |
14 | 17 | import org.lognet.springboot.grpc.demo.DemoApp; |
| 18 | +import org.lognet.springboot.grpc.demo.DemoAppConfiguration; |
15 | 19 | import org.lognet.springboot.grpc.demo.ITaskService; |
| 20 | +import org.lognet.springboot.grpc.demo.NotSpringBeanInterceptor; |
16 | 21 | import org.lognet.springboot.grpc.security.AuthCallCredentials; |
17 | 22 | import org.lognet.springboot.grpc.security.AuthHeader; |
18 | 23 | import org.lognet.springboot.grpc.security.GrpcSecurity; |
|
22 | 27 | import org.springframework.boot.test.context.TestConfiguration; |
23 | 28 | import org.springframework.boot.test.mock.mockito.MockBean; |
24 | 29 | import org.springframework.context.annotation.Import; |
| 30 | +import org.springframework.security.access.prepost.PreAuthorize; |
25 | 31 | import org.springframework.security.core.userdetails.User; |
26 | 32 | import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
27 | 33 | import org.springframework.test.context.junit4.SpringRunner; |
@@ -75,6 +81,17 @@ public List<T> get(Duration duration) throws Throwable { |
75 | 81 |
|
76 | 82 | @TestConfiguration |
77 | 83 | static class TestCfg extends GrpcSecurityConfigurerAdapter { |
| 84 | + @GRpcService(interceptors = NotSpringBeanInterceptor.class) |
| 85 | + @PreAuthorize("isAuthenticated()") |
| 86 | + public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{ |
| 87 | + @Override |
| 88 | + public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) { |
| 89 | + responseObserver.onNext(DemoAppConfiguration.CalculatorService.calculate(request)); |
| 90 | + responseObserver.onCompleted(); |
| 91 | + |
| 92 | + |
| 93 | + } |
| 94 | + } |
78 | 95 | @Override |
79 | 96 | public void configure(GrpcSecurity builder) throws Exception { |
80 | 97 | builder.authorizeRequests() |
@@ -119,6 +136,33 @@ public void configure(GrpcSecurity builder) throws Exception { |
119 | 136 | @MockBean |
120 | 137 | private ITaskService service; |
121 | 138 |
|
| 139 | + @Test |
| 140 | + public void preAuthAnnotationOnClassTest() { |
| 141 | + |
| 142 | + |
| 143 | + final SecuredCalculatorGrpc.SecuredCalculatorBlockingStub stub = SecuredCalculatorGrpc |
| 144 | + .newBlockingStub(selectedChanel); |
| 145 | + |
| 146 | + final CalculatorOuterClass.CalculatorResponse response = stub |
| 147 | + .withCallCredentials(user2Credentials()) |
| 148 | + .calculate(CalculatorOuterClass.CalculatorRequest.newBuilder() |
| 149 | + .setNumber1(1) |
| 150 | + .setNumber2(1) |
| 151 | + .setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD) |
| 152 | + .build()); |
| 153 | + assertThat(response.getResult(),Matchers.is(2d)); |
| 154 | + |
| 155 | + final StatusRuntimeException statusRuntimeException = Assert.assertThrows(StatusRuntimeException.class, () -> { |
| 156 | + stub.withCallCredentials(unAuthUserCredentials()) |
| 157 | + .calculate(CalculatorOuterClass.CalculatorRequest.newBuilder() |
| 158 | + .setNumber1(1) |
| 159 | + .setNumber2(1) |
| 160 | + .setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD) |
| 161 | + .build()); |
| 162 | + }); |
| 163 | + assertThat(statusRuntimeException.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED)); |
| 164 | + } |
| 165 | + |
122 | 166 | @Test |
123 | 167 | public void unaryPreAuthorizeCallTest() { |
124 | 168 | final TaskServiceGrpc.TaskServiceBlockingStub stub = TaskServiceGrpc.newBlockingStub(getChannel()) |
|
0 commit comments