Add authorize requests filters
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package org.springframework.grpc.sample;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -16,7 +14,6 @@ import org.springframework.grpc.sample.proto.HelloReply;
|
||||
import org.springframework.grpc.sample.proto.HelloRequest;
|
||||
import org.springframework.grpc.sample.proto.ReactorSimpleGrpc;
|
||||
import org.springframework.grpc.sample.proto.ReactorSimpleGrpc.ReactorSimpleStub;
|
||||
import org.springframework.grpc.sample.proto.SimpleGrpc;
|
||||
import org.springframework.grpc.test.AutoConfigureInProcessTransport;
|
||||
import org.springframework.grpc.test.LocalGrpcPort;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.4.0'
|
||||
id 'org.springframework.boot' version '3.4.1'
|
||||
id 'io.spring.dependency-management' version '1.1.6'
|
||||
id 'org.graalvm.buildtools.native' version '0.10.3'
|
||||
id 'com.google.protobuf' version '0.9.4'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.3.0-SNAPSHOT'
|
||||
version = '0.4.0-SNAPSHOT'
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
@@ -24,7 +24,7 @@ repositories {
|
||||
|
||||
dependencyManagement {
|
||||
imports {
|
||||
mavenBom 'org.springframework.grpc:spring-grpc-dependencies:0.3.0-SNAPSHOT'
|
||||
mavenBom 'org.springframework.grpc:spring-grpc-dependencies:0.4.0-SNAPSHOT'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.4.1</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>org.springframework.grpc</groupId>
|
||||
<artifactId>grpc-secure-sample</artifactId>
|
||||
<version>0.3.0-SNAPSHOT</version>
|
||||
<version>0.4.0-SNAPSHOT</version>
|
||||
<name>Spring gRPC Server Sample</name>
|
||||
<description>Demo project for Spring gRPC</description>
|
||||
<url />
|
||||
@@ -38,7 +38,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.grpc</groupId>
|
||||
<artifactId>spring-grpc-dependencies</artifactId>
|
||||
<version>0.3.0-SNAPSHOT</version>
|
||||
<version>0.4.0-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
<modules>
|
||||
<module>grpc-server</module>
|
||||
<module>grpc-secure</module>
|
||||
<module>grpc-reactive</module>
|
||||
<module>grpc-server-netty-shaded</module>
|
||||
<module>grpc-tomcat</module>
|
||||
|
||||
Reference in New Issue
Block a user