Skip to content

Commit 8461feb

Browse files
committed
Merge branch '6.3.x' into 6.4.x
Closes gh-17494
2 parents 3f4ef16 + 4f5b173 commit 8461feb

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

config/src/main/java/org/springframework/security/config/websocket/WebSocketMessageBrokerSecurityBeanDefinitionParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ static class MessageSecurityPostProcessor implements BeanDefinitionRegistryPostP
301301

302302
private static final String CLIENT_INBOUND_CHANNEL_BEAN_ID = "clientInboundChannel";
303303

304+
private static final String CSRF_CHANNEL_INTERCEPTOR_BEAN_ID = "csrfChannelInterceptor";
305+
304306
private static final String INTERCEPTORS_PROP = "interceptors";
305307

306308
private static final String CUSTOM_ARG_RESOLVERS_PROP = "customArgumentResolvers";
@@ -364,7 +366,12 @@ else if ("org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsS
364366
ManagedList<Object> interceptors = new ManagedList();
365367
interceptors.add(new RootBeanDefinition(SecurityContextChannelInterceptor.class));
366368
if (!this.sameOriginDisabled) {
367-
interceptors.add(new RootBeanDefinition(CsrfChannelInterceptor.class));
369+
if (!registry.containsBeanDefinition(CSRF_CHANNEL_INTERCEPTOR_BEAN_ID)) {
370+
interceptors.add(new RootBeanDefinition(CsrfChannelInterceptor.class));
371+
}
372+
else {
373+
interceptors.add(new RuntimeBeanReference(CSRF_CHANNEL_INTERCEPTOR_BEAN_ID));
374+
}
368375
}
369376
interceptors.add(registry.getBeanDefinition(this.inboundSecurityInterceptorId));
370377
BeanDefinition inboundChannel = registry.getBeanDefinition(CLIENT_INBOUND_CHANNEL_BEAN_ID);

config/src/test/java/org/springframework/security/config/websocket/WebSocketMessageBrokerConfigTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
4949
import org.springframework.messaging.simp.SimpMessageType;
5050
import org.springframework.messaging.support.ChannelInterceptor;
51+
import org.springframework.messaging.support.ExecutorSubscribableChannel;
5152
import org.springframework.messaging.support.GenericMessage;
5253
import org.springframework.security.access.AccessDeniedException;
5354
import org.springframework.security.access.expression.SecurityExpressionOperations;
@@ -521,6 +522,16 @@ public void sendWhenCustomAuthorizationManagerThenAuthorizesAccordingly() {
521522
verify(authorizationManager).check(any(), any());
522523
}
523524

525+
@Test
526+
public void configureWhenCsrfChannelInterceptorBeanThenUses() {
527+
this.spring.configLocations(xml("CustomCsrfInterceptor")).autowire();
528+
ExecutorSubscribableChannel channel = this.spring.getContext()
529+
.getBean("clientInboundChannel", ExecutorSubscribableChannel.class);
530+
ChannelInterceptor interceptor = this.spring.getContext()
531+
.getBean("csrfChannelInterceptor", ChannelInterceptor.class);
532+
assertThat(channel.getInterceptors()).contains(interceptor);
533+
}
534+
524535
private String xml(String configName) {
525536
return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml";
526537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2018 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://www.springframework.org/schema/security"
20+
xsi:schemaLocation="http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd
21+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
22+
23+
<b:import resource="classpath:org/springframework/security/config/websocket/controllers.xml"/>
24+
<b:import resource="classpath:org/springframework/security/config/websocket/websocket.xml"/>
25+
26+
<b:bean id="csrfChannelInterceptor" class="org.mockito.Mockito" factory-method="mock">
27+
<b:constructor-arg value="org.springframework.messaging.support.ChannelInterceptor" type="java.lang.Class"/>
28+
</b:bean>
29+
30+
<websocket-message-broker use-authorization-manager="false">
31+
<intercept-message pattern="/**" access="denyNile()"/>
32+
</websocket-message-broker>
33+
34+
</b:beans>

0 commit comments

Comments
 (0)