From 25b95b8b982f7ce8c38cf6c4fff87397731d09ca Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 31 Jan 2023 11:50:16 +0000 Subject: [PATCH] Avoid ClassLoader issue in Jetty10 WebSocket upgrade Closes gh-29256 --- .../Jetty10RequestUpgradeStrategy.java | 50 ++++-------------- .../jetty/Jetty10RequestUpgradeStrategy.java | 51 ++++--------------- 2 files changed, 20 insertions(+), 81 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/Jetty10RequestUpgradeStrategy.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/Jetty10RequestUpgradeStrategy.java index 4629c969802..e2422a19c34 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/Jetty10RequestUpgradeStrategy.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/Jetty10RequestUpgradeStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,24 +17,20 @@ package org.springframework.web.reactive.socket.server.upgrade; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.function.Supplier; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; import reactor.core.publisher.Mono; -import org.springframework.aop.framework.ProxyFactory; -import org.springframework.aop.target.EmptyTargetSource; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponseDecorator; -import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; @@ -119,40 +115,14 @@ public class Jetty10RequestUpgradeStrategy implements RequestUpgradeStrategy { private static Object createJettyWebSocketCreator( Jetty10WebSocketHandlerAdapter adapter, @Nullable String protocol) { - ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE); - factory.addInterface(webSocketCreatorClass); - factory.addAdvice(new WebSocketCreatorInterceptor(adapter, protocol)); - return factory.getProxy(); - } - - - /** - * Proxy for a JettyWebSocketCreator to supply the WebSocket handler and set the sub-protocol. - */ - private static class WebSocketCreatorInterceptor implements MethodInterceptor { - - private final Jetty10WebSocketHandlerAdapter adapter; - - @Nullable - private final String protocol; - - - public WebSocketCreatorInterceptor( - Jetty10WebSocketHandlerAdapter adapter, @Nullable String protocol) { - - this.adapter = adapter; - this.protocol = protocol; - } - - @Nullable - @Override - public Object invoke(@NonNull MethodInvocation invocation) { - if (this.protocol != null) { - ReflectionUtils.invokeMethod( - setAcceptedSubProtocol, invocation.getArguments()[1], this.protocol); - } - return this.adapter; - } + return Proxy.newProxyInstance( + webSocketCreatorClass.getClassLoader(), new Class[] {webSocketCreatorClass}, + (proxy, method, args) -> { + if (protocol != null) { + ReflectionUtils.invokeMethod(setAcceptedSubProtocol, args[1], protocol); + } + return adapter; + }); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/Jetty10RequestUpgradeStrategy.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/Jetty10RequestUpgradeStrategy.java index 0391bbf5f94..c4ac7000742 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/Jetty10RequestUpgradeStrategy.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/Jetty10RequestUpgradeStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package org.springframework.web.socket.server.jetty; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; import java.security.Principal; import java.util.Collections; @@ -27,16 +28,10 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; - -import org.springframework.aop.framework.ProxyFactory; -import org.springframework.aop.target.EmptyTargetSource; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.http.server.ServletServerHttpResponse; -import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; @@ -128,40 +123,14 @@ public class Jetty10RequestUpgradeStrategy implements RequestUpgradeStrategy { private static Object createJettyWebSocketCreator( Jetty10WebSocketHandlerAdapter adapter, @Nullable String protocol) { - ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE); - factory.addInterface(webSocketCreatorClass); - factory.addAdvice(new WebSocketCreatorInterceptor(adapter, protocol)); - return factory.getProxy(); - } - - - /** - * Proxy for a JettyWebSocketCreator to supply the WebSocket handler and set the sub-protocol. - */ - private static class WebSocketCreatorInterceptor implements MethodInterceptor { - - private final Jetty10WebSocketHandlerAdapter adapter; - - @Nullable - private final String protocol; - - - public WebSocketCreatorInterceptor( - Jetty10WebSocketHandlerAdapter adapter, @Nullable String protocol) { - - this.adapter = adapter; - this.protocol = protocol; - } - - @Nullable - @Override - public Object invoke(@NonNull MethodInvocation invocation) { - if (this.protocol != null) { - ReflectionUtils.invokeMethod( - setAcceptedSubProtocol, invocation.getArguments()[1], this.protocol); - } - return this.adapter; - } + return Proxy.newProxyInstance( + webSocketCreatorClass.getClassLoader(), new Class[] {webSocketCreatorClass}, + (proxy, method, args) -> { + if (protocol != null) { + ReflectionUtils.invokeMethod(setAcceptedSubProtocol, args[1], protocol); + } + return adapter; + }); } }