Skip to content

Commit fd7adfc

Browse files
committed
refactor(websocket): unify handshake handler class check with Set
Replaces repeated if-else string comparisons with a Set.contains() check for known WebSocket handshake handler class names in MessageSecurityPostProcessor. Improves readability and maintainability without changing behavior. Signed-off-by: evga7 <evga7@naver.com>
1 parent e1d8033 commit fd7adfc

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import java.util.Comparator;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Set;
23+
import java.util.HashSet;
24+
import java.util.Arrays;
25+
2226
import java.util.function.Supplier;
2327

2428
import org.w3c.dom.Element;
@@ -307,6 +311,12 @@ static class MessageSecurityPostProcessor implements BeanDefinitionRegistryPostP
307311

308312
private static final String TEMPLATE_EXPRESSION_BEAN_ID = "annotationExpressionTemplateDefaults";
309313

314+
private static final Set<String> CSRF_HANDSHAKE_HANDLER_CLASSES = new HashSet<>(Arrays.asList(
315+
"org.springframework.web.socket.server.support.WebSocketHttpRequestHandler",
316+
"org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService",
317+
"org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService"
318+
));
319+
310320
private final String inboundSecurityInterceptorId;
311321

312322
private final boolean sameOriginDisabled;
@@ -345,16 +355,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) t
345355
}
346356
}
347357
}
348-
else if ("org.springframework.web.socket.server.support.WebSocketHttpRequestHandler"
349-
.equals(beanClassName)) {
350-
addCsrfTokenHandshakeInterceptor(bd);
351-
}
352-
else if ("org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService"
353-
.equals(beanClassName)) {
354-
addCsrfTokenHandshakeInterceptor(bd);
355-
}
356-
else if ("org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService"
357-
.equals(beanClassName)) {
358+
else if (CSRF_HANDSHAKE_HANDLER_CLASSES.contains(beanClassName)) {
358359
addCsrfTokenHandshakeInterceptor(bd);
359360
}
360361
}

0 commit comments

Comments
 (0)