gpt4 book ai didi

java - 如何使用带有默认 java 构建包的 Tomcat 服务器在 Ibm Bluemix 中强制实现 Https 连接?

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:23 25 4
gpt4 key购买 nike

我有一个 Spring Mvc 应用程序,使用托管在 ibm bluemix 上的 hibernate,并使用 tomcat 服务器在 godaddy 中注册了域,使用 blue mix 为 tomcat 提供的 java_buildpack。目前,我已经在 blue mix 中注册的 godaddy 中购买了一个 ssl 证书。我的应用程序现在可以在 http 和 https 上运行。但是现在我需要强制只对我的应用程序进行 https 连接。我实现了 Spring Security。我已使用安全配置来强制执行 https 并使用以下代码https 重定向。

requiresChannel().anyRequest().requiresSecure()

但它在浏览器中给我以下错误

Too many redirects occurred trying to open “https://website-name”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

现在,我通过网络跟踪了一些链接,以便强制执行 https,他们告诉我添加一些参数,我在应用程序的 blue mix 运行时环境变量中添加了这些参数。

server.tomcat.internal-proxies:.*

我也尝试添加

server.tomcat.remote_ip_header:x-forwarded-for

server.tomcat.protocol_header:x-forwarded-proto

应用程序的流程首先是去爸爸查找,然后转到蓝色混合应用程序,我怎样才能只启用 https

但我仍然遇到同样的错误。各位能帮我解决这个问题吗?

我添加了自定义过滤器

@Component
public class CustomFilter implements Filter {
private static final Logger logger = Logger.getLogger(CartController.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request1, ServletResponse response1, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) request1;
HttpServletResponse response = (HttpServletResponse) response1;
if (!request.isSecure()) {
logger.info("Not secure");
// generate full URL to https
StringBuilder newUrl = new StringBuilder("https://");
newUrl.append(request.getServerName());
if (request.getRequestURI() != null) {
newUrl.append(request.getRequestURI());
}
if (request.getQueryString() != null) {
newUrl.append("?").append(request.getQueryString());
}

response.sendRedirect(newUrl.toString());
} else {
// already a secure connection, no redirect to https required.
logger.info("Else");
if (chain != null) {
logger.info("Chain Null");
chain.doFilter(request, response);
}
}

}


}

最佳答案

我建议两个选择:1. 过去我手动实现了一个过滤器,如果收到非http请求则重定向到https。我没有按照您尝试的方式使用 Spring 安全性。2. 在 Spring 表单上向 Spring 安全主管 Rob Winch 发布问题,并交叉链接到该问题,以便 Bluemix 平台上的人员可以看到他的回复。

我最初的想法是手动过滤器是可行的方法,但我真的很想知道 Rob 和团队是否在 CF 平台上遇到过这种情况。

关于java - 如何使用带有默认 java 构建包的 Tomcat 服务器在 Ibm Bluemix 中强制实现 Https 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com