Add authorize requests filters
This commit is contained in:
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user