Add authorize requests filters

This commit is contained in:
Dave Syer
2025-01-22 12:16:16 +00:00
parent aa8d680c3f
commit ff40f5b323
15 changed files with 282 additions and 74 deletions

View File

@@ -40,21 +40,23 @@ public class GrpcServerApplication {
@GlobalServerInterceptor
public ServerInterceptor securityInterceptor(GrpcSecurity security) throws Exception {
return security
.authorizeRequests(requests -> requests
.methods("Simple/StreamHello").hasAuthority("ROLE_ADMIN")
.methods("Simple/SayHello").hasAuthority("ROLE_USER")
.allRequests().permitAll())
.httpBasic(withDefaults())
.preauth(withDefaults())
.authenticationExtractor((headers, attributes) -> {
String user = headers.get(USER_KEY);
if (user != null) {
return new PreAuthenticatedAuthenticationToken(user, "N/A",
AuthorityUtils.createAuthorityList("ROLE_" + user.toUpperCase()));
}
return null;
})
.build();
.authorizeRequests(requests -> requests.methods("Simple/StreamHello")
.hasAuthority("ROLE_ADMIN")
.methods("Simple/SayHello")
.hasAuthority("ROLE_USER")
.allRequests()
.permitAll())
.httpBasic(withDefaults())
.preauth(withDefaults())
.authenticationExtractor((headers, attributes) -> {
String user = headers.get(USER_KEY);
if (user != null) {
return new PreAuthenticatedAuthenticationToken(user, "N/A",
AuthorityUtils.createAuthorityList("ROLE_" + user.toUpperCase()));
}
return null;
})
.build();
}

View File

@@ -38,8 +38,7 @@ import io.grpc.StatusRuntimeException;
public class GrpcServerApplicationTests {
public static void main(String[] args) {
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class)
.run(args);
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).run(args);
}
@Autowired
@@ -115,7 +114,7 @@ public class GrpcServerApplicationTests {
@Lazy
SimpleGrpc.SimpleBlockingStub basic(GrpcChannelFactory channels) {
return SimpleGrpc.newBlockingStub(channels.createChannel("basic", ChannelBuilderOptions.defaults()
.withInterceptors(List.of(new BasicAuthenticationInterceptor("user", "user")))));
.withInterceptors(List.of(new BasicAuthenticationInterceptor("user", "user")))));
}
@Bean