Skip to content

Commit dd6e3c9

Browse files
committed
Added Grpc Delegated Token Jwt.
1 parent c3d8000 commit dd6e3c9

File tree

11 files changed

+77
-17
lines changed

11 files changed

+77
-17
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ services:
130130
- POSTGRESQL_DATABASE=${DB_NAME}
131131
- ALLOW_EMPTY_PASSWORD=yes
132132

133+
# Postgres Slave
133134
postgresql-slave:
134135
image: bitnami/postgresql
135136
restart: always

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<protobuf.version>3.23.4</protobuf.version>
2323
<protobuf-plugin.version>0.6.1</protobuf-plugin.version>
24-
<grpc.version>1.58.0</grpc.version>
24+
<grpc.version>1.62.2</grpc.version>
2525
</properties>
2626
<dependencies>
2727
<dependency>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package org.wsd.app.config;
22

3+
import feign.RequestInterceptor;
4+
import org.springframework.context.annotation.Bean;
35
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.security.core.Authentication;
8+
import org.springframework.security.core.context.SecurityContextHolder;
9+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
410

511

612
@Configuration
713
public class FeignConfig {
814

15+
@Bean
16+
public RequestInterceptor requestInterceptor() {
17+
return requestTemplate -> {
18+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
19+
if (authentication != null && authentication instanceof JwtAuthenticationToken) {
20+
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
21+
String tokenValue = jwtAuthenticationToken.getToken().getTokenValue();
22+
requestTemplate.header(HttpHeaders.AUTHORIZATION, "Bearer " + tokenValue);
23+
}
24+
};
25+
}
26+
927
}

src/main/java/org/wsd/app/config/GrpcConfig.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
package org.wsd.app.config;
2424

2525
import io.grpc.ManagedChannelBuilder;
26+
import lombok.RequiredArgsConstructor;
2627
import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer;
2728
import org.springframework.beans.factory.annotation.Autowired;
2829
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2930
import org.springframework.context.annotation.Bean;
3031
import org.springframework.context.annotation.Configuration;
3132
import org.wsd.app.grpc.interceptor.ErrorHandlingClientInterceptor;
33+
import org.wsd.app.grpc.interceptor.GrpcClientInterceptor;
3234

3335
@Configuration
3436
@ImportAutoConfiguration({
@@ -40,17 +42,19 @@
4042
net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration.class,
4143
net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration.class,
4244
})
45+
@RequiredArgsConstructor
4346
public class GrpcConfig {
4447

4548

46-
@Autowired
47-
private ErrorHandlingClientInterceptor errorHandlingClientInterceptor;
49+
private final ErrorHandlingClientInterceptor errorHandlingClientInterceptor;
50+
private final GrpcClientInterceptor grpcClientInterceptor;
4851

4952
@Bean
5053
public GrpcChannelConfigurer clientInterceptorConfigurer() {
5154
return (channelBuilder, name) -> {
52-
if (channelBuilder instanceof ManagedChannelBuilder) {
55+
if (channelBuilder != null) {
5356
channelBuilder.intercept(errorHandlingClientInterceptor);
57+
channelBuilder.intercept(grpcClientInterceptor);
5458
}
5559
};
5660
}

src/main/java/org/wsd/app/config/RetryConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,4 @@
3232
@Configuration
3333
public class RetryConfiguration {
3434

35-
3635
}

src/main/java/org/wsd/app/config/TransactionManagerConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@
4545
@RequiredArgsConstructor
4646
@EnableTransactionManagement
4747
public class TransactionManagerConfig {
48-
49-
5048
private final ProducerFactory<UUID, Object> producerFactory;
51-
5249
@Primary
5350
@Bean(name = "transactionManager")
5451
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

src/main/java/org/wsd/app/controller/AuthenticationController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
@RequiredArgsConstructor
2929
public class AuthenticationController {
3030
private final AuthenticationService authenticationService;
31-
3231
@ResponseStatus(value = HttpStatus.OK)
3332
@Operation(description = "Sign In", summary = "Endpoint for user sign in.")
3433
@PostMapping(path = "/signIn",
@@ -42,7 +41,6 @@ public ResponseEntity<?> signIn(@Valid @RequestBody
4241
return ResponseEntity.status(HttpStatus.OK)
4342
.body(authenticationService.signIn(signInRequest));
4443
}
45-
4644
@Operation(description = "Sign Up", summary = "Endpoint for user sign up.")
4745
@PostMapping(path = "/signUp",
4846
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},

src/main/java/org/wsd/app/controller/OrderController.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
@SecurityRequirement(name = "BEARER_TOKEN")
1515
@RequestMapping("/api/orders")
1616
public class OrderController {
17-
1817
private final OrderService orderService;
19-
2018
@GetMapping("/{orderId}")
2119
@ResponseStatus(HttpStatus.FOUND)
2220
public ResponseEntity<?> getOrderById(@PathVariable("orderId") int orderId) {
@@ -25,5 +23,4 @@ public ResponseEntity<?> getOrderById(@PathVariable("orderId") int orderId) {
2523
return ResponseEntity.
2624
ok("Success");
2725
}
28-
2926
}

src/main/java/org/wsd/app/controller/TwoFactorAuthController.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
@Tag(name = "Two Factor Authentication")
1919
@SecurityRequirement(name = "BEARER_TOKEN")
2020
public class TwoFactorAuthController {
21-
2221
private final TwoFactorService twoFactorService;
23-
2422
@Operation(description = "Two Factor Authentication", summary = "Endpoint for two factor authentication service enable and disable.")
2523
@GetMapping("/2Fa/configure")
2624
public Payload<TwoFactorResponse> twoFactorSetup(TwoFactorRequest request) {
2725
return twoFactorService.setup(request);
2826
}
29-
3027
}

src/main/java/org/wsd/app/grpc/OrderService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public class OrderService {
3939

4040
@GrpcClient("grpc-client")
41-
OrderServiceGrpc.OrderServiceBlockingStub orderServiceBlockingStub;
41+
public OrderServiceGrpc.OrderServiceBlockingStub orderServiceBlockingStub;
4242

4343
public void getOrderById(int orderId) {
4444
log.info("Order Request Id : " + orderId);

0 commit comments

Comments
 (0)