akr*_*roy 5 java https redirect http-redirect dropwizard
我想将传入的http连接重定向到Dropwizard中的https,最好在配置文件中切换(例如,使用YAML文件,就像其他连接属性一样).[我已经看到了这个问题,我有理由相信它不是解决方案]
我在几个 地方找到的解决方案涉及挂钩检查架构的过滤器,如果找到"http",则调用sendRedirect并修改URL.这涉及对行为进行硬编码以使其始终发生.
如果我扩展HttpConnectorFactory,似乎我可以在YAML中添加配置,以确定是否需要重定向.但是,我不清楚在不破坏其他代码的情况下添加属性会有多复杂.
这似乎是一项共同的任务; 这是一种标准的"首选"方式吗?我本来期望Dropwizard有优雅的内置支持,就像Jetty一样,但我找不到它.
我不知道有一种"首选"方式可以做到这一点但是这样的事情(对于Dropwizard 0.7.0):
void addHttpsForward(ServletContextHandler handler) {
handler.addFilter(new FilterHolder(new Filter() {
public void init(FilterConfig filterConfig) throws ServletException {}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
StringBuffer uri = ((HttpServletRequest) request).getRequestURL();
if (uri.toString().startsWith("http://")) {
String location = "https://" + uri.substring("http://".length());
((HttpServletResponse) response).sendRedirect(location);
} else {
chain.doFilter(request, response);
}
}
public void destroy() {}
}), "/*", EnumSet.of(DispatcherType.REQUEST));
}
@Override
public void run(ExampleConfiguration configuration, Environment environment) throws Exception {
//...
if (configuration.forwardHttps()) {
addHttpsForward(environment.getApplicationContext());
}
//...
}
Run Code Online (Sandbox Code Playgroud)
您只需要在应用程序配置中添加一个布尔值,然后就可以轻松地使用YAML切换https转发.
归档时间: |
|
查看次数: |
2025 次 |
最近记录: |