我有一个嵌入式码头应用程序(jetty 9.3.12),我需要将请求从http://host和http://host/my/app重定向到https://host/my/app
我已经使用RewriteHandler一段时间了,以成功重定向http://host-> http://host/my/app和https://host-> https://host/my/app。
我不知道如何使http://host-> https://host/my/app工作。
我还有另一个由我同一个服务器处理的码头上下文,我将其称为/ other(未反映在下面的代码中)。我不想重定向http://other-> https://other。
以下是我想要发生的事情的摘要
http:// -> https://my/app
http://my/app -> https://my/app
https:// -> https://my/app
http://other -> no change
如果我将SecuredRedirectHandler直接添加到服务器处理程序(请参见代码中的替代方法),则在访问/ my / app时出现404问题。原因:找不到。
如果我在RewriteHandler后面添加SecuredRedirectHandler,则什么都不会发生(下面的代码)。
private Server createServer() {
Server server = new Server();
configureConnectors(server);
HandlerCollection handlers = getHandlers(server);
RewriteHandler rewriteHandler = createRewriteHandler(server);
server.setHandler(rewriteHandler);
return server;
}
private void configureConnectors(Server server) {
List<ServerConnector> connectors = new ArrayList<>();
ServerConnect httpsConnector = createHttpsConnector(server);
boolean httpsEnabled = httpsConnector != null;
ServerConnector httpConnector = createHttpConnector(server, httpsEnabled);
connectors.add(httpConnector);
connectors.add(httpsConnector);
// ALTERNATIVE METHOD - Results in 404 - Problem access /my/app Reason: Not Found
//addHandlerAsFirst(new SecuredRedirectHandler(), server);
for (ServerConnector connector : connectors) {
server.addConnector(connector);
}
}
private ServerConnector createHttpConnector(Server server, boolean httpsEnabled) {
HttpConfiguration httpConfig = new HttpConfiguration();
if (httpsEnabled) {
Integer httpsPort = getServiceUrl().getPortSsl();
httpConfig.addCustomizer(new SecureRequestCustomizer());
httpConfig.setSecureScheme(HttpScheme.HTTPS.asString());
httpConfig.setSecurePort(httpsPort);
}
Integer port = getServiceUrl().getPort();
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
connector.setPort(port);
configureConnector(connector);
return connector;
}
private ServerConnector createHttpsConnector(Server server) {
Integer httpsPort = getServiceUrl().getPortSsl();
Integer port = httpsPort;
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.addCustomizer(new SecureRequestCustomizer());
SslContextFactory sslContextFactory = null;
try {
sslContextFactory = createSslContextFactory();
} catch (ConfigurationException | NotFoundException e) {
log.err(m, e, "Create SSL Context failed: Perhaps Certificate Policy Bindings are not configured?");
return null;
}
SslConnectionFactory connectionFactory = new SslConnectionFactory(sslContextFactory, HTTP_VERSION);
ServerConnector connector = new ServerConnector(server, connectionFactory, new HttpConnectionFactory(httpConfig));
connector.setPort(port);
configureConnector(connector);
return connector;
}
public RewriteHandler createRewriteContext(HandlerCollection handlerCollection) {
RewriteHandler rewrite = new RewriteHandler();
rewrite.setRewriteRequestURI(true);
rewrite.setRewritePathInfo(false);
rewrite.setOriginalPathAttribute("requestedPath");
rewrite.setHandler(handlerCollection);
String path = AppProductConfig.CONSOLE_CONTEXT_ROOT + AppUI.APP_PATH;
/*
* Redirect "/" to "/console/app". We use redirect instead of rewrite
* because Vaadin needs to access it's widgetset files. The rewrite requires
* multiple rules to accomplish this. The redirect is simpler.
*/
RedirectPatternRule rootRule = new RedirectPatternRule();
rootRule.setPattern(""); // Redirect / (root)
rootRule.setLocation(path);
rewrite.addRule(rootRule);
return rewrite;
}
protected void addHandlerAsFirst(Handler handler, Server server) {
HandlerCollection handlers = handlersMap.get(server);
if (handlers == null) {
handlers = new HandlerCollection();
handlersMap.put(server, handlers);
}
handlers.prependHandler(handler);
}
已解决
我可以使用以下方法来使工作正常进行:Jetty 9.2.1 redirect http to https
答案 0 :(得分:0)
您将有2个重定向,而不是1个。
Server
HandlerList
SecuredRedirectHandler
RewriteHandler
Rule -> "/" to "/my/app"
ServletContextHandler or WebAppContext
DefaultHandler
不要尝试重写路径,也不要重定向到HTTPS。 只是让每个人独立地做自己的事情。
请考虑以下情形:
http://host/my/app
(仅使用SecuredRedirectHandler)https://host/
(仅使用RewriteHandler)http://host/
(2个重定向,一次用于方案,第二次用于路径)