|
25 | 25 |
|
26 | 26 | import org.springframework.beans.BeansException;
|
27 | 27 | import org.springframework.beans.PropertyValue;
|
| 28 | +import org.springframework.beans.factory.FactoryBean; |
28 | 29 | import org.springframework.beans.factory.config.BeanDefinition;
|
29 | 30 | import org.springframework.beans.factory.config.BeanReference;
|
30 | 31 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
50 | 51 | import org.springframework.security.authorization.AuthorizationManager;
|
51 | 52 | import org.springframework.security.config.Elements;
|
52 | 53 | import org.springframework.security.core.Authentication;
|
| 54 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 55 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
53 | 56 | import org.springframework.security.messaging.access.expression.ExpressionBasedMessageSecurityMetadataSourceFactory;
|
54 | 57 | import org.springframework.security.messaging.access.expression.MessageAuthorizationContextSecurityExpressionHandler;
|
55 | 58 | import org.springframework.security.messaging.access.expression.MessageExpressionVoter;
|
@@ -118,6 +121,8 @@ public final class WebSocketMessageBrokerSecurityBeanDefinitionParser implements
|
118 | 121 |
|
119 | 122 | private static final String AUTHORIZATION_MANAGER_REF_ATTR = "authorization-manager-ref";
|
120 | 123 |
|
| 124 | + private static final String SECURITY_CONTEXT_HOLDER_STRATEGY_REF_ATTR = "security-context-holder-strategy-ref"; |
| 125 | + |
121 | 126 | private static final String PATTERN_ATTR = "pattern";
|
122 | 127 |
|
123 | 128 | private static final String ACCESS_ATTR = "access";
|
@@ -170,6 +175,16 @@ private String parseAuthorizationManager(Element element, ParserContext parserCo
|
170 | 175 | BeanDefinitionBuilder inboundChannelSecurityInterceptor = BeanDefinitionBuilder
|
171 | 176 | .rootBeanDefinition(AuthorizationChannelInterceptor.class);
|
172 | 177 | inboundChannelSecurityInterceptor.addConstructorArgReference(mdsId);
|
| 178 | + String holderStrategyRef = element.getAttribute(SECURITY_CONTEXT_HOLDER_STRATEGY_REF_ATTR); |
| 179 | + if (StringUtils.hasText(holderStrategyRef)) { |
| 180 | + inboundChannelSecurityInterceptor.addPropertyValue("securityContextHolderStrategy", |
| 181 | + new RuntimeBeanReference(holderStrategyRef)); |
| 182 | + } |
| 183 | + else { |
| 184 | + inboundChannelSecurityInterceptor.addPropertyValue("securityContextHolderStrategy", BeanDefinitionBuilder |
| 185 | + .rootBeanDefinition(SecurityContextHolderStrategyFactory.class).getBeanDefinition()); |
| 186 | + } |
| 187 | + |
173 | 188 | return context.registerWithGeneratedName(inboundChannelSecurityInterceptor.getBeanDefinition());
|
174 | 189 | }
|
175 | 190 |
|
@@ -459,4 +474,18 @@ private static AuthorizationManager<Message<?>> createMessageMatcherDelegatingAu
|
459 | 474 |
|
460 | 475 | }
|
461 | 476 |
|
| 477 | + static class SecurityContextHolderStrategyFactory implements FactoryBean<SecurityContextHolderStrategy> { |
| 478 | + |
| 479 | + @Override |
| 480 | + public SecurityContextHolderStrategy getObject() throws Exception { |
| 481 | + return SecurityContextHolder.getContextHolderStrategy(); |
| 482 | + } |
| 483 | + |
| 484 | + @Override |
| 485 | + public Class<?> getObjectType() { |
| 486 | + return SecurityContextHolderStrategy.class; |
| 487 | + } |
| 488 | + |
| 489 | + } |
| 490 | + |
462 | 491 | }
|
0 commit comments