Skip to content

Commit f9d674c

Browse files
committed
Merge branch '6.0.x'
Closes gh-12525
2 parents 782b792 + 4d2dab9 commit f9d674c

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

core/src/main/java/org/springframework/security/authentication/ObservationReactiveAuthenticationManager.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.micrometer.observation.Observation;
2020
import io.micrometer.observation.ObservationRegistry;
21+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
2122
import reactor.core.publisher.Mono;
2223

2324
import org.springframework.security.core.Authentication;
@@ -48,13 +49,16 @@ public Mono<Authentication> authenticate(Authentication authentication) throws A
4849
AuthenticationObservationContext context = new AuthenticationObservationContext();
4950
context.setAuthenticationRequest(authentication);
5051
context.setAuthenticationManagerClass(this.delegate.getClass());
51-
Observation observation = Observation.createNotStarted(this.convention, () -> context, this.registry).start();
52-
return this.delegate.authenticate(authentication).doOnSuccess((result) -> {
53-
context.setAuthenticationResult(result);
54-
observation.stop();
55-
}).doOnCancel(observation::stop).doOnError((t) -> {
56-
observation.error(t);
57-
observation.stop();
52+
return Mono.deferContextual((contextView) -> {
53+
Observation observation = Observation.createNotStarted(this.convention, () -> context, this.registry)
54+
.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start();
55+
return this.delegate.authenticate(authentication).doOnSuccess((result) -> {
56+
context.setAuthenticationResult(result);
57+
observation.stop();
58+
}).doOnCancel(observation::stop).doOnError((t) -> {
59+
observation.error(t);
60+
observation.stop();
61+
});
5862
});
5963
}
6064

core/src/main/java/org/springframework/security/authorization/ObservationReactiveAuthorizationManager.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.micrometer.observation.Observation;
2020
import io.micrometer.observation.ObservationRegistry;
21+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
2122
import reactor.core.publisher.Mono;
2223

2324
import org.springframework.security.access.AccessDeniedException;
@@ -50,16 +51,19 @@ public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T
5051
context.setAuthentication(auth);
5152
return context.getAuthentication();
5253
});
53-
Observation observation = Observation.createNotStarted(this.convention, () -> context, this.registry).start();
54-
return this.delegate.check(wrapped, object).doOnSuccess((decision) -> {
55-
context.setDecision(decision);
56-
if (decision == null || !decision.isGranted()) {
57-
observation.error(new AccessDeniedException("Access Denied"));
58-
}
59-
observation.stop();
60-
}).doOnCancel(observation::stop).doOnError((t) -> {
61-
observation.error(t);
62-
observation.stop();
54+
return Mono.deferContextual((contextView) -> {
55+
Observation observation = Observation.createNotStarted(this.convention, () -> context, this.registry)
56+
.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start();
57+
return this.delegate.check(wrapped, object).doOnSuccess((decision) -> {
58+
context.setDecision(decision);
59+
if (decision == null || !decision.isGranted()) {
60+
observation.error(new AccessDeniedException("Access Denied"));
61+
}
62+
observation.stop();
63+
}).doOnCancel(observation::stop).doOnError((t) -> {
64+
observation.error(t);
65+
observation.stop();
66+
});
6367
});
6468
}
6569

web/src/main/java/org/springframework/security/web/server/ObservationWebFilterChainDecorator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.micrometer.observation.Observation;
2828
import io.micrometer.observation.ObservationConvention;
2929
import io.micrometer.observation.ObservationRegistry;
30+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
3031
import reactor.core.publisher.Mono;
3132

3233
import org.springframework.lang.Nullable;
@@ -73,20 +74,22 @@ private static AroundWebFilterObservation observation(ServerWebExchange exchange
7374
}
7475

7576
private WebFilterChain wrapSecured(WebFilterChain original) {
76-
return (exchange) -> {
77+
return (exchange) -> Mono.deferContextual((contextView) -> {
7778
AroundWebFilterObservation parent = observation(exchange);
79+
Observation parentObservation = contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
7880
Observation observation = Observation.createNotStarted(SECURED_OBSERVATION_NAME, this.registry)
79-
.contextualName("secured request");
81+
.contextualName("secured request").parentObservation(parentObservation);
8082
return parent.wrap(WebFilterObservation.create(observation).wrap(original)).filter(exchange);
81-
};
83+
});
8284
}
8385

8486
private WebFilterChain wrapUnsecured(WebFilterChain original) {
85-
return (exchange) -> {
87+
return (exchange) -> Mono.deferContextual((contextView) -> {
88+
Observation parentObservation = contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
8689
Observation observation = Observation.createNotStarted(UNSECURED_OBSERVATION_NAME, this.registry)
87-
.contextualName("unsecured request");
90+
.contextualName("unsecured request").parentObservation(parentObservation);
8891
return WebFilterObservation.create(observation).wrap(original).filter(exchange);
89-
};
92+
});
9093
}
9194

9295
private List<ObservationWebFilter> wrap(List<WebFilter> filters) {
@@ -186,8 +189,11 @@ String getName() {
186189
@Override
187190
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
188191
if (this.position == 1) {
189-
AroundWebFilterObservation parent = parent(exchange);
190-
return parent.wrap(this::wrapFilter).filter(exchange, chain);
192+
return Mono.deferContextual((contextView) -> {
193+
Observation parentObservation = contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null);
194+
AroundWebFilterObservation parent = parent(exchange, parentObservation);
195+
return parent.wrap(this::wrapFilter).filter(exchange, chain);
196+
});
191197
}
192198
else {
193199
return wrapFilter(exchange, chain);
@@ -211,11 +217,13 @@ private Mono<Void> wrapFilter(ServerWebExchange exchange, WebFilterChain chain)
211217
});
212218
}
213219

214-
private AroundWebFilterObservation parent(ServerWebExchange exchange) {
220+
private AroundWebFilterObservation parent(ServerWebExchange exchange, Observation parentObservation) {
215221
WebFilterChainObservationContext beforeContext = WebFilterChainObservationContext.before();
216222
WebFilterChainObservationContext afterContext = WebFilterChainObservationContext.after();
217-
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry);
218-
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry);
223+
Observation before = Observation.createNotStarted(this.convention, () -> beforeContext, this.registry)
224+
.parentObservation(parentObservation);
225+
Observation after = Observation.createNotStarted(this.convention, () -> afterContext, this.registry)
226+
.parentObservation(parentObservation);
219227
AroundWebFilterObservation parent = AroundWebFilterObservation.create(before, after);
220228
exchange.getAttributes().put(ATTRIBUTE, parent);
221229
return parent;

0 commit comments

Comments
 (0)