Skip to content

Commit

Permalink
add current security context
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurtsang committed Apr 28, 2022
1 parent 0cfaa2f commit 39a6019
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion server/src/main/java/org/example/RSocketController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.annotation.CurrentSecurityContext;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.oauth2.jwt.JwtClaimAccessor;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Mono;
Expand All @@ -26,10 +28,11 @@ public class RSocketController {
*/
@SneakyThrows
@MessageMapping("request-response")
Mono<Location> requestResponse(Location request, @Headers Map<String, Object> metadata, @AuthenticationPrincipal JwtClaimAccessor user ) {
Mono<Location> requestResponse(Location request, @Headers Map<String, Object> metadata, @AuthenticationPrincipal JwtClaimAccessor user, @CurrentSecurityContext SecurityContext securityContext) {
log.info("Received request-response request: {}", request);
log.info("Received request-response header: {}", metadata);
log.info("Received request-response user details: {} {}", user.getSubject(), user.getClaim("scope"));
log.info("Received request-response security context {} {}", securityContext.getAuthentication().getName(), securityContext.getAuthentication().getAuthorities());
return Mono.just(Location.newBuilder().setLocation("s3://newbucket/newfile\n").build());
}

Expand Down
2 changes: 2 additions & 0 deletions server/src/main/java/org/example/RSocketSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.security.config.annotation.rsocket.EnableRSocketSecurity;
import org.springframework.security.config.annotation.rsocket.RSocketSecurity;
import org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver;
import org.springframework.security.messaging.handler.invocation.reactive.CurrentSecurityContextArgumentResolver;
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class RSocketSecurityConfig {
RSocketMessageHandler messageHandler(RSocketStrategies strategies) {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.getArgumentResolverConfigurer().addCustomResolver(new AuthenticationPrincipalArgumentResolver());
handler.getArgumentResolverConfigurer().addCustomResolver(new CurrentSecurityContextArgumentResolver());
handler.setRSocketStrategies(strategies);
return handler;
}
Expand Down

0 comments on commit 39a6019

Please sign in to comment.