|
16 | 16 |
|
17 | 17 | package org.springframework.security.core.context;
|
18 | 18 |
|
19 |
| -import java.util.List; |
20 |
| -import java.util.concurrent.CopyOnWriteArrayList; |
21 |
| -import java.util.function.BiConsumer; |
22 |
| -import java.util.function.Supplier; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
23 | 21 |
|
24 |
| -final class ListeningSecurityContextHolderStrategy implements SecurityContextHolderStrategy { |
| 22 | +import org.springframework.util.Assert; |
25 | 23 |
|
26 |
| - private static final BiConsumer<SecurityContext, SecurityContext> NULL_PUBLISHER = (previous, current) -> { |
27 |
| - }; |
| 24 | +/** |
| 25 | + * An API for notifying when the {@link SecurityContext} changes. |
| 26 | + * |
| 27 | + * Note that this does not notify when the underlying authentication changes. To get |
| 28 | + * notified about authentication changes, ensure that you are using {@link #setContext} |
| 29 | + * when changing the authentication like so: |
| 30 | + * |
| 31 | + * <pre> |
| 32 | + * SecurityContext context = SecurityContextHolder.createEmptyContext(); |
| 33 | + * context.setAuthentication(authentication); |
| 34 | + * SecurityContextHolder.setContext(context); |
| 35 | + * </pre> |
| 36 | + * |
| 37 | + * To add a listener to the existing {@link SecurityContextHolder}, you can do: |
| 38 | + * |
| 39 | + * <pre> |
| 40 | + * SecurityContextHolderStrategy original = SecurityContextHolder.getContextHolderStrategy(); |
| 41 | + * SecurityContextChangedListener listener = new YourListener(); |
| 42 | + * SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy(original, listener); |
| 43 | + * SecurityContextHolder.setContextHolderStrategy(strategy); |
| 44 | + * </pre> |
| 45 | + * |
| 46 | + * NOTE: Any object that you supply to the {@link SecurityContextHolder} is now part of |
| 47 | + * the static context and as such will not get garbage collected. To remove the reference, |
| 48 | + * {@link SecurityContextHolder#setContextHolderStrategy reset the strategy} like so: |
| 49 | + * |
| 50 | + * <pre> |
| 51 | + * SecurityContextHolder.setContextHolderStrategy(original); |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * This will then allow {@code YourListener} and its members to be garbage collected. |
| 55 | + * |
| 56 | + * @author Josh Cummings |
| 57 | + * @since 5.6 |
| 58 | + */ |
| 59 | +public final class ListeningSecurityContextHolderStrategy implements SecurityContextHolderStrategy { |
28 | 60 |
|
29 |
| - private final Supplier<SecurityContext> peek; |
| 61 | + private final Collection<SecurityContextChangedListener> listeners; |
30 | 62 |
|
31 | 63 | private final SecurityContextHolderStrategy delegate;
|
32 | 64 |
|
33 |
| - private final SecurityContextEventPublisher base = new SecurityContextEventPublisher(); |
34 |
| - |
35 |
| - private BiConsumer<SecurityContext, SecurityContext> publisher = NULL_PUBLISHER; |
| 65 | + /** |
| 66 | + * Construct a {@link ListeningSecurityContextHolderStrategy} |
| 67 | + * @param listeners the listeners that should be notified when the |
| 68 | + * {@link SecurityContext} is {@link #setContext(SecurityContext) set} or |
| 69 | + * {@link #clearContext() cleared} |
| 70 | + * @param delegate the underlying {@link SecurityContextHolderStrategy} |
| 71 | + */ |
| 72 | + public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, |
| 73 | + Collection<SecurityContextChangedListener> listeners) { |
| 74 | + Assert.notNull(delegate, "securityContextHolderStrategy cannot be null"); |
| 75 | + Assert.notNull(listeners, "securityContextChangedListeners cannot be null"); |
| 76 | + Assert.notEmpty(listeners, "securityContextChangedListeners cannot be empty"); |
| 77 | + Assert.noNullElements(listeners, "securityContextChangedListeners cannot contain null elements"); |
| 78 | + this.delegate = delegate; |
| 79 | + this.listeners = listeners; |
| 80 | + } |
36 | 81 |
|
37 |
| - ListeningSecurityContextHolderStrategy(Supplier<SecurityContext> peek, SecurityContextHolderStrategy delegate) { |
38 |
| - this.peek = peek; |
| 82 | + /** |
| 83 | + * Construct a {@link ListeningSecurityContextHolderStrategy} |
| 84 | + * @param listeners the listeners that should be notified when the |
| 85 | + * {@link SecurityContext} is {@link #setContext(SecurityContext) set} or |
| 86 | + * {@link #clearContext() cleared} |
| 87 | + * @param delegate the underlying {@link SecurityContextHolderStrategy} |
| 88 | + */ |
| 89 | + public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, |
| 90 | + SecurityContextChangedListener... listeners) { |
| 91 | + Assert.notNull(delegate, "securityContextHolderStrategy cannot be null"); |
| 92 | + Assert.notNull(listeners, "securityContextChangedListeners cannot be null"); |
| 93 | + Assert.notEmpty(listeners, "securityContextChangedListeners cannot be empty"); |
| 94 | + Assert.noNullElements(listeners, "securityContextChangedListeners cannot contain null elements"); |
39 | 95 | this.delegate = delegate;
|
| 96 | + this.listeners = Arrays.asList(listeners); |
40 | 97 | }
|
41 | 98 |
|
| 99 | + /** |
| 100 | + * {@inheritDoc} |
| 101 | + */ |
42 | 102 | @Override
|
43 | 103 | public void clearContext() {
|
44 |
| - SecurityContext from = this.peek.get(); |
| 104 | + SecurityContext from = getContext(); |
45 | 105 | this.delegate.clearContext();
|
46 |
| - this.publisher.accept(from, null); |
| 106 | + publish(from, null); |
47 | 107 | }
|
48 | 108 |
|
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + */ |
49 | 112 | @Override
|
50 | 113 | public SecurityContext getContext() {
|
51 | 114 | return this.delegate.getContext();
|
52 | 115 | }
|
53 | 116 |
|
| 117 | + /** |
| 118 | + * {@inheritDoc} |
| 119 | + */ |
54 | 120 | @Override
|
55 | 121 | public void setContext(SecurityContext context) {
|
56 |
| - SecurityContext from = this.peek.get(); |
| 122 | + SecurityContext from = getContext(); |
57 | 123 | this.delegate.setContext(context);
|
58 |
| - this.publisher.accept(from, context); |
| 124 | + publish(from, context); |
59 | 125 | }
|
60 | 126 |
|
| 127 | + /** |
| 128 | + * {@inheritDoc} |
| 129 | + */ |
61 | 130 | @Override
|
62 | 131 | public SecurityContext createEmptyContext() {
|
63 | 132 | return this.delegate.createEmptyContext();
|
64 | 133 | }
|
65 | 134 |
|
66 |
| - void addListener(SecurityContextChangedListener listener) { |
67 |
| - this.base.listeners.add(listener); |
68 |
| - this.publisher = this.base; |
69 |
| - } |
70 |
| - |
71 |
| - private static class SecurityContextEventPublisher implements BiConsumer<SecurityContext, SecurityContext> { |
72 |
| - |
73 |
| - private final List<SecurityContextChangedListener> listeners = new CopyOnWriteArrayList<>(); |
74 |
| - |
75 |
| - @Override |
76 |
| - public void accept(SecurityContext previous, SecurityContext current) { |
77 |
| - if (previous == current) { |
78 |
| - return; |
79 |
| - } |
80 |
| - SecurityContextChangedEvent event = new SecurityContextChangedEvent(previous, current); |
81 |
| - for (SecurityContextChangedListener listener : this.listeners) { |
82 |
| - listener.securityContextChanged(event); |
83 |
| - } |
| 135 | + private void publish(SecurityContext previous, SecurityContext current) { |
| 136 | + if (previous == current) { |
| 137 | + return; |
| 138 | + } |
| 139 | + SecurityContextChangedEvent event = new SecurityContextChangedEvent(previous, current); |
| 140 | + for (SecurityContextChangedListener listener : this.listeners) { |
| 141 | + listener.securityContextChanged(event); |
84 | 142 | }
|
85 |
| - |
86 | 143 | }
|
87 | 144 |
|
88 | 145 | }
|
0 commit comments