java - 在java servlet中从http重定向到https并返回http

标签 java servlets https

有一堆链接在没有 https 的情况下访问我的 servlet
由于 servlet 是通用形式,并且 http url 是使用随机 id 生成的,因此很难使用 modrewrite 或类似的东西。

因此我用这样的代码修改了我的 servlet:

//redirect to https
        String sec = servletRequest.getParameter("Sec");
        String qString = servletRequest.getQueryString();

        if (StringUtils.isEmpty(sec)){
              try {
                    HttpServletResponse rsp = request.getServletResponse(true);


                         String PORTAL_URL = l_aliasHelper.getPath(request);


              rsp.sendRedirect("https://"+servletRequest.getServerName() +PORTAL_URL+"?" +qString+"&Sec=yes");
              } catch (Exception e) {
              e.printStackTrace();
              }

        }

现在一切正常了!

但是,如果我想返回 http,因为我想避免有关其他页面上不安全元素的烦人警告,该怎么办?

那么用户提交表单后如何再次重定向到http?

如果一切顺利,用户将在他启动的同一 URL 下显示一条包含成功消息的响应。

So the cycle goes like this: 
http://<somedomain>/<anypath>?<anyid>
https://<somedomain>/<anypath>?<anyid>&Sec=yes
and now it should go back maybe with a step inbetween to
http://<somedomain>/<anypath>?<anyid> <- the success message should be
displayed here

显示消息之前的最后一个方法是
sucessmessage.render(请求,响应)

请求和响应都是应用程序服务器组件对所有请求/响应相关事务的特定 View 。他们有这样的方法:

getServletResponse

public HttpServletResponse getServletResponse(boolean answering)

Gets the original servlet response. Note: This should be accessed

in extraordinary cases only. If the parameter is set to true all further content procession of the runtime will be skipped. This is only available, if the request was initiated from a servlet based connection.

那么如何以安全提交表单的方式操作响应,但用户之后可以在网站的其余部分继续使用 http。

最佳答案

您似乎试图在一个地方做太多事情。也许下面的分解会更容易:

  • 在 HTML 表单中的操作参数的 URL 中指定 https。
  • 创建一个使用 ServletRequest.isSecure() 的 ServletFilter 类 确保对您的表单操作的请求确实已收到 https。这也可以在您的操作 Servlet 中,但将其设为过滤器意味着您可以重用它。只需确保安全 servlet 设置了此过滤器即可。
  • 在您的表单操作 Servlet 中,只需将重定向发送到 通过 http 的成功页面

关于java - 在java servlet中从http重定向到https并返回http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6985161/

相关文章:

java - out.write 不需要数据类型双值(Java Servlet)

如果我在 Jersey 的 @Path 注释中使用子资源,则找不到 JavaScript 文件

php - 无法使PHP cURL SSL正常工作

java - Hadoop 中 Mapreduce 程序的意外输出

java - java中如何限制if条件只执行一次

java - 在Java中访问子类中父类(super class)的 "final"字段(并更新它们)

java - Android Recyclerview 的一些项目

java - Spring ModelAndView 设置请求 header 值

java - 使用.cer证书发出HTTPS请求

Delphi XE2 DataSnap - 通过 HTTPS 使用 REST 时为 'Connection Closed Gracefully'