在jetty 9.2 getCurrentHttpChannel
上有一个HttpChannel
方法,我们将其用作HttpChannel.getCurrentHttpChannel().getHttpConfiguration();
我现在想要转换为Jetty 9.3但是该方法缺失了,我在文档等中找不到任何已更改的引用。 9.3中是否有等价物?
答案 0 :(得分:2)
在Jetty 9.3中,可以使用以下命令:
HttpConnection.getCurrentConnection().getHttpChannel().getHttpConfiguration();
答案 1 :(得分:1)
几乎所有Jetty内部的Thread Local访问方法都已被删除,因为它们在HTTP / 2,WebSocket和各种其他客户端上下文中使用时无效。
如果您需要访问Jetty的内部,则必须在ServletContext之外执行此操作。
/* License: Eclipse Public License 1.0 and Apache License 2.0
* See linked source file above for details.
*/
package org.eclipse.jetty.server.handler;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.URIUtil;
/**
* Secured Redirect Handler
* <p>
* Using information present in the {@link HttpConfiguration}, will attempt to redirect to the {@link HttpConfiguration#getSecureScheme()} and
* {@link HttpConfiguration#getSecurePort()} for any request that {@link HttpServletRequest#isSecure()} == false.
*/
public class SecuredRedirectHandler extends AbstractHandler
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
HttpChannel channel = baseRequest.getHttpChannel();
if (baseRequest.isSecure() || (channel == null))
{
// nothing to do
return;
}
HttpConfiguration httpConfig = channel.getHttpConfiguration();
if (httpConfig == null)
{
// no config, show error
response.sendError(HttpStatus.FORBIDDEN_403,"No http configuration available");
return;
}
if (httpConfig.getSecurePort() > 0)
{
String scheme = httpConfig.getSecureScheme();
int port = httpConfig.getSecurePort();
String url = URIUtil.newURI(scheme,baseRequest.getServerName(),port,baseRequest.getRequestURI(),baseRequest.getQueryString());
response.setContentLength(0);
response.sendRedirect(url);
}
else
{
response.sendError(HttpStatus.FORBIDDEN_403,"Not Secure");
}
baseRequest.setHandled(true);
}
}