Skip to content

Commit e34454e

Browse files
committed
closes #310
1 parent ed199ce commit e34454e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/PrePostSecurityAuthTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import org.lognet.springboot.grpc.security.GrpcSecurity;
2424
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
2525
import org.mockito.Mockito;
26+
import org.mockito.verification.VerificationMode;
2627
import org.springframework.boot.test.context.SpringBootTest;
2728
import org.springframework.boot.test.context.TestConfiguration;
2829
import org.springframework.boot.test.mock.mockito.MockBean;
30+
import org.springframework.boot.test.mock.mockito.SpyBean;
31+
import org.springframework.context.annotation.Bean;
2932
import org.springframework.context.annotation.Import;
3033
import org.springframework.security.access.prepost.PreAuthorize;
34+
import org.springframework.security.core.AuthenticatedPrincipal;
35+
import org.springframework.security.core.Authentication;
3136
import org.springframework.security.core.userdetails.User;
3237
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
3338
import org.springframework.test.context.junit4.SpringRunner;
@@ -44,6 +49,7 @@
4449
import java.util.stream.Stream;
4550

4651
import static org.hamcrest.MatcherAssert.assertThat;
52+
import static org.mockito.ArgumentMatchers.notNull;
4753

4854
@SpringBootTest(classes = DemoApp.class)
4955
@RunWith(SpringRunner.class)
@@ -79,19 +85,30 @@ public List<T> get(Duration duration) throws Throwable {
7985
}
8086
}
8187

88+
public static class PermissionService{
89+
public boolean allow() {
90+
final Authentication auth = GrpcSecurity.AUTHENTICATION_CONTEXT_KEY.get();
91+
assertThat(auth, Matchers.notNullValue(Authentication.class));
92+
return true;
93+
}
94+
}
8295
@TestConfiguration
8396
static class TestCfg extends GrpcSecurityConfigurerAdapter {
8497
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
85-
@PreAuthorize("isAuthenticated()")
98+
@PreAuthorize("isAuthenticated() && @permissionService.allow()")
8699
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{
87100
@Override
88101
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
89102
responseObserver.onNext(DemoAppConfiguration.CalculatorService.calculate(request));
90103
responseObserver.onCompleted();
91-
92-
93104
}
105+
94106
}
107+
@Bean
108+
public PermissionService permissionService(){
109+
return new PermissionService();
110+
}
111+
95112
@Override
96113
public void configure(GrpcSecurity builder) throws Exception {
97114
builder.authorizeRequests()
@@ -136,6 +153,9 @@ public void configure(GrpcSecurity builder) throws Exception {
136153
@MockBean
137154
private ITaskService service;
138155

156+
@SpyBean
157+
private PermissionService permissionService;
158+
139159
@Test
140160
public void preAuthAnnotationOnClassTest() {
141161

@@ -161,6 +181,8 @@ public void preAuthAnnotationOnClassTest() {
161181
.build());
162182
});
163183
assertThat(statusRuntimeException.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));
184+
185+
Mockito.verify(permissionService, Mockito.atLeastOnce()).allow();
164186
}
165187

166188
@Test

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/security/GrpcSecurity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ protected ServerInterceptor performBuild() throws Exception {
8787

8888

8989
final GrpcSecurityMetadataSource metadataSource =getSharedObject(GrpcSecurityMetadataSource.class);
90+
DefaultMethodSecurityExpressionHandler methodSecurityExpressionHandler = new DefaultMethodSecurityExpressionHandler();
91+
methodSecurityExpressionHandler.setApplicationContext(getApplicationContext());
9092
final DelegatingMethodSecurityMetadataSource compositeMDS = new DelegatingMethodSecurityMetadataSource(Arrays.asList(
9193
metadataSource,
9294
new PrePostAnnotationSecurityMetadataSource(
9395
new ExpressionBasedAnnotationAttributeFactory(
94-
new DefaultMethodSecurityExpressionHandler()
96+
methodSecurityExpressionHandler
9597
)
9698
)
9799
));
@@ -104,7 +106,7 @@ protected ServerInterceptor performBuild() throws Exception {
104106

105107

106108
ExpressionBasedPreInvocationAdvice expressionAdvice = new ExpressionBasedPreInvocationAdvice();
107-
expressionAdvice.setExpressionHandler(new DefaultMethodSecurityExpressionHandler());
109+
expressionAdvice.setExpressionHandler(methodSecurityExpressionHandler);
108110

109111

110112
final AffirmativeBased accessDecisionManager = new AffirmativeBased(Arrays.asList(

0 commit comments

Comments
 (0)