Skip to content

Commit 2c4364b

Browse files
author
Alexander Furer
committed
global user details service
1 parent 7ba49c5 commit 2c4364b

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

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

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.security.access.annotation.Secured;
3232
import org.springframework.security.core.userdetails.User;
3333
import org.springframework.security.core.userdetails.UserDetails;
34+
import org.springframework.security.core.userdetails.UserDetailsService;
3435
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
3536
import org.springframework.test.context.junit4.SpringRunner;
3637

@@ -42,18 +43,18 @@
4243
import static org.junit.Assert.assertTrue;
4344

4445

45-
@SpringBootTest(classes = DemoApp.class,webEnvironment = SpringBootTest.WebEnvironment.NONE)
46+
@SpringBootTest(classes = DemoApp.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
4647
@RunWith(SpringRunner.class)
4748
@Import({UserDetailsAuthTest.TestCfg.class})
4849
public class UserDetailsAuthTest extends GrpcServerTestBase {
4950

5051

5152
@TestConfiguration
52-
static class TestCfg extends GrpcSecurityConfigurerAdapter {
53+
static class TestCfg extends GrpcSecurityConfigurerAdapter {
5354

5455
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
5556
@Secured({})
56-
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase{
57+
public static class SecuredCalculatorService extends SecuredCalculatorGrpc.SecuredCalculatorImplBase {
5758
@Override
5859
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
5960
responseObserver.onNext(DemoAppConfiguration.CalculatorService.calculate(request));
@@ -62,30 +63,33 @@ public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObse
6263

6364
}
6465
}
65-
static final String pwd="strongPassword1";
66-
67-
@Bean
68-
public UserDetails user() {
69-
return User.withDefaultPasswordEncoder()
70-
.username("user1")
71-
.password(pwd)
72-
.roles("reader")
73-
.build();
74-
}
7566

67+
static final String pwd = "strongPassword1";
7668

77-
@Override
78-
public void configure(GrpcSecurity builder) throws Exception {
79-
builder.authorizeRequests()
80-
.methods(GreeterGrpc.getSayHelloMethod()).hasAnyRole("reader")
81-
.methods(GreeterGrpc.getSayAuthOnlyHelloMethod()).hasAnyRole("reader")
82-
.methods(CalculatorGrpc.getCalculateMethod()).hasAnyRole("anotherRole")
83-
.withSecuredAnnotation()
84-
.userDetailsService(new InMemoryUserDetailsManager(builder.getApplicationContext().getBean(UserDetails.class)));
69+
@Bean
70+
public static UserDetailsService userDetailsService() {
71+
return new InMemoryUserDetailsManager(user());
72+
}
73+
74+
@Bean
75+
public static UserDetails user() {
76+
return User.withDefaultPasswordEncoder()
77+
.username("user1")
78+
.password(pwd)
79+
.roles("reader")
80+
.build();
81+
}
8582

86-
}
8783

84+
@Override
85+
public void configure(GrpcSecurity builder) throws Exception {
86+
builder.authorizeRequests()
87+
.methods(GreeterGrpc.getSayHelloMethod()).hasAnyRole("reader")
88+
.methods(GreeterGrpc.getSayAuthOnlyHelloMethod()).hasAnyRole("reader")
89+
.methods(CalculatorGrpc.getCalculateMethod()).hasAnyRole("anotherRole")
90+
.withSecuredAnnotation();
8891

92+
}
8993

9094

9195
}
@@ -98,17 +102,15 @@ public void configure(GrpcSecurity builder) throws Exception {
98102
public void simpleAuthHeaderFormat() throws ExecutionException, InterruptedException {
99103

100104

101-
102105
final GreeterGrpc.GreeterFutureStub greeterFutureStub = GreeterGrpc.newFutureStub(getChannel(false));
103106
final String reply = greeterFutureStub.sayAuthOnlyHello(Empty.newBuilder().build()).get().getMessage();
104-
assertNotNull("Reply should not be null",reply);
105-
assertTrue(String.format("Reply should contain name '%s'",user.getUsername()),reply.contains(user.getUsername()));
107+
assertNotNull("Reply should not be null", reply);
108+
assertTrue(String.format("Reply should contain name '%s'", user.getUsername()), reply.contains(user.getUsername()));
106109

107110

108111
}
109112

110113

111-
112114
@Test
113115
public void shouldFailWithPermissionDenied() {
114116

@@ -133,11 +135,11 @@ public void serviceLevelSecurityAuthenticationWithoutAuthorization() {
133135
final CalculatorOuterClass.CalculatorResponse response = SecuredCalculatorGrpc
134136
.newBlockingStub(selectedChanel)//auth channel
135137
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder()
136-
.setNumber1(1)
137-
.setNumber2(1)
138-
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
139-
.build());
140-
assertThat(response.getResult(),Matchers.is(2d));
138+
.setNumber1(1)
139+
.setNumber2(1)
140+
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
141+
.build());
142+
assertThat(response.getResult(), Matchers.is(2d));
141143

142144
}
143145

@@ -148,24 +150,24 @@ public void shouldFailWithUnauthenticated() {
148150
SecuredCalculatorGrpc
149151
.newBlockingStub(super.getChannel()) // channel without auth
150152
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder()
151-
.setNumber1(1)
152-
.setNumber2(1)
153-
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
154-
.build());
153+
.setNumber1(1)
154+
.setNumber2(1)
155+
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
156+
.build());
155157
});
156158
assertThat(statusRuntimeException.getStatus().getCode(), Matchers.is(Status.Code.UNAUTHENTICATED));
157159

158160
}
159161

160162
@Override
161163
protected Channel getChannel() {
162-
return getChannel(true);
164+
return getChannel(true);
163165
}
164166

165167
protected Channel getChannel(boolean binaryFormat) {
166168

167169
final AuthClientInterceptor interceptor = new AuthClientInterceptor(AuthHeader.builder()
168-
.basic(user.getUsername(),TestCfg.pwd.getBytes())
170+
.basic(user.getUsername(), TestCfg.pwd.getBytes())
169171
.binaryFormat(binaryFormat)
170172
);
171173
return ClientInterceptors.intercept(super.getChannel(), interceptor);

0 commit comments

Comments
 (0)