Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Dropwizard中将http连接重定向到https的首选方法_Java_Redirect_Https_Http Redirect_Dropwizard - Fatal编程技术网

Java 在Dropwizard中将http连接重定向到https的首选方法

Java 在Dropwizard中将http连接重定向到https的首选方法,java,redirect,https,http-redirect,dropwizard,Java,Redirect,Https,Http Redirect,Dropwizard,我想在Dropwizard中将传入的http连接重定向到https,最好在配置文件中切换(例如,与其他连接属性一样,使用YAML文件)。 [我已经看到了,我有理由相信这不是一个解决方案] 我在中找到的一个解决方案涉及挂接一个检查模式的链接,如果它找到“http”,则使用修改的URL调用sendRedirect。这涉及到对行为进行硬编码,使其始终发生 如果扩展,似乎可以在YAML中添加配置,以确定是否需要重定向。然而,我不清楚在不破坏其他代码的情况下添加属性有多复杂 这似乎是一项共同的任务;是否有

我想在Dropwizard中将传入的http连接重定向到https,最好在配置文件中切换(例如,与其他连接属性一样,使用YAML文件)。 [我已经看到了,我有理由相信这不是一个解决方案]

我在中找到的一个解决方案涉及挂接一个检查模式的链接,如果它找到“http”,则使用修改的URL调用sendRedirect。这涉及到对行为进行硬编码,使其始终发生

如果扩展,似乎可以在YAML中添加配置,以确定是否需要重定向。然而,我不清楚在不破坏其他代码的情况下添加属性有多复杂


这似乎是一项共同的任务;是否有一个标准的“首选”方法来做到这一点?我本来希望Dropwizard具有优雅的内置支持,但我找不到它。

我不知道是否有“首选”的方法来实现这一点,但是类似这样的东西如何(对于Dropwizard 0.7.0):

void addHttpsForward(ServletContextHandler){
handler.addFilter(新过滤器文件夹(新过滤器(){
public void init(FilterConfig FilterConfig)抛出ServletException{}
public void doFilter(ServletRequest请求、ServletResponse响应、FilterChain链)
抛出IOException、ServletException{
StringBufferURI=((HttpServletRequest)请求).getRequestURL();
if(uri.toString().startsWith(“http:/”){
String location=“https://”+uri.substring(“http://”.length());
((HttpServletResponse)响应).sendRedirect(位置);
}否则{
链式过滤器(请求、响应);
}
}
public void destroy(){}
}),“/*”,EnumSet.of(DispatcherType.REQUEST));
}
@凌驾
公共无效运行(例如配置、环境)引发异常{
//...
if(configuration.forwardHttps()){
addHttpsForward(environment.getApplicationContext());
}
//...      
}

您只需要在应用程序配置中添加一个布尔值,然后就可以轻松地使用YAML切换https转发。

您可以在

@覆盖
公共无效初始化(最终引导引导引导){
addBundle(新重定向bundle(新httpsredict(false));
上面的HttpsRedirect是用false为allowPrivateIps构建的,这使得在本地测试成为可能。HttpsRedirect文档对此有很多信息

@Override
public void initialize(final Bootstrap<PrmCatchConfiguration> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(new HttpsRedirect(false)));