【问题标题】:How can I test a URL is SSL secured如何测试 URL 是否受 SSL 保护
【发布时间】:2014-12-13 18:41:11
【问题描述】:

我是 IT 行业的新手。测试场景就像我需要测试我的应用程序的登录页面是否受 SSL 保护?

一般来说,有时我们会访问一些显示 SSL 安全性弹出窗口的网站。所以我需要在我的应用程序中测试相同的场景。

我有一个小型 Web 应用程序,其中有 login.html 页面。基本上,我可以使用 Maven 启动我的 Web 应用程序,并且使用的服务器是 Tomcat。我用来启动的命令是mvn tomcat7:run,URL 使用http://localhost:8080/login.html。效果很好。

但我想将我的 URL 从 http 更改为 https,当我访问我的 URL 时,即更改为 https://localhost:8080/login.html,然后它应该弹出 SSL 安全警报,我应该接受它。

如果我的问题仍然不清楚,请随时发表评论。

在网上搜索后,我做了一些解决方法,但没有奏效。我尝试过的:

我的 HTML 页面

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Login App</h1>
<div id="emptyDiv"></div>
<div id="description"></div>
<!--container start-->
<div id="container">
  <div id="container_body" style="background-color:#BBD700;float:center;">
  <!--Form  start-->
    <div id="form_name">
      <div class="firstnameorlastname">
<form >
     &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
       <div id="errorBox"></div>
         First Name :   <input  id="firstName" type="text" name="Name" value="" >
         &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
         Last name : <input  id="lastName" type="text" name="LastName" value="" >

      </div>
               &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
      <div id="email_form">
        Email Id: <input style="position:right" type="text" name="Email" value="" >
      </div>
      <input id="sub_form" type="submit" value="Submit">
           </form>
    </div>
    <!--form ends-->
  </div>
</div>
<!--container ends-->
</body>
</html>

web.xml

<pre><code><!DOCTYPE web-app PUBLIC <span style="color: red;">"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"</span> <span style="color: red;">"http://java.sun.com/dtd/web-app_2_3.dtd"</span>>
<web-app>
           <!--   <security-constraint>
             <web-resource-collection>
                 <web-resource-name>MyEducationApp</web-resource-name>
                 <url-pattern>/login.html</url-pattern>
              </web-resource-collection>
              <user-data-constraint>
                 <transport-guarantee>CONFIDENTIAL</transport-guarantee>
              </user-data-constraint>
             </security-constraint>

               <security-constraint>
                  <web-resource-collection>
                     <web-resource-name>Non-SecureResource</web-resource-name>
                     <url-pattern>/login.html</url-pattern>
                  </web-resource-collection>
                  <user-data-constraint>
                     <transport-guarantee>NONE</transport-guarantee>
                  </user-data-constraint>
               </security-constraint> -->

  <display-name>Login WebApp</display-name>
</web-app>
</span></code></pre>

使用的 Maven 插件

    <!-- Maven Tomcat Plugin -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>https://localhost:8080/manager/text</url>
                    <server>localhost</server>
                    <path>/</path>
                    <username>admin</username>
                    <password>aRfalah</password>

                </configuration>
                <executions>
                    <execution>
                        <id>tomcat7-run</id>
                        <goals>
                            <goal>run-war-only</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <fork>true</fork>

                        </configuration>
                    </execution>

                    <execution>
                        <id>tomcat7-shutdown</id>
                        <goals>
                            <goal>shutdown</goal>
                        </goals>
                        <phase>post-integration-test</phase>
                    </execution>
                </executions>
            </plugin>

【问题讨论】:

  • 浏览器只接受由证书颁发机构(例如 VeriSign)签署的有效证书。
  • @traianus:感谢您的回复。我怎样才能做到?这不是我可以在 HTML 页面或网络应用程序中做的事情吗?
  • 视情况而定。如果您想在互联网上发布您的网站(以便每个人都可以使用它),则必须从其中一个 CA 购买证书,并且您需要一个修复 IP。如果只是供个人使用,则可以将证书导入浏览器的证书存储区以将其标记为受信任。 PKI 不是一个简单的话题,您可以阅读更多内容here
  • 在Firefox中,您可以在错误页面上轻松添加您的证书。见here,如何。
  • @traianus Re “比必须从 CA 购买证书” - 这不是(完全)正确的。 1.StartSSL免费提供1年SSL/TLS + S/MIME。 2. 您可以使用OpenSSL(因此成为您自己的CA)轻松创建自己的证书,并将此证书与您的https:// 站点相关联。如果您的访问者在其浏览器中弹出的对话框中接受您的证书,则该证书将存储在浏览器的证书存储中,并且在证书到期之前不会再次出现该对话框。

标签: java maven tomcat ssl web-applications


【解决方案1】:

您的网站的 SSL/TLS 加密在您的 Web 应用程序中是无能为力的。这是通过您的网络服务器的配置完成的。

Apache Tomcat 7, SSL Configuration HOW-TO


其他信息(重复我对 OQ 的评论,因为 cmets 不是那么突出和可编辑):

您不必从certification authorities (CA) 之一购买证书即可获得证书。

  1. StartSSL 免费提供 1 年 SSL/TLS + S/MIME。 On a different domain 他们现在提供:

    没有优惠了:

    StartCom 订阅者须知

    StartCom CA 自 2018 年 1 月 1 日起关闭,不再从 StartCom 名称根颁发任何新证书。 如果您想购买可信SSL证书和代码签名证书,请访问https://store.wotrus.com。 如需申请免费邮箱证书,请访问https://www.mesince.com下载密信APP,自动获取免费邮箱证书,自动发送加密邮件。

    但同时可能还有其他公司。

  2. 您可以使用OpenSSL(因此成为您自己的 CA)轻松创建自己的证书,并将此证书与您的https:// 站点相关联。如果您的访问者在其浏览器中弹出的对话框中接受您的证书,则该证书将存储在浏览器的证书存储中,并且在达到证书的到期日期之前不会再次出现该对话框。

【讨论】:

  • 他没有说过任何关于使用 Apache HTTPD 的事情。
  • @EJP 我写了 "for instance" 正是因为这个。我很想听听如何改进我的答案。
  • @EJP 你没有提示吗?
  • 他正在使用 Tomcat。写一个关于 Tomcat 的答案。你确定不需要提示吗?
  • @EJP Thx,这就是我所期待的回复。我相应地调整了我的答案。
【解决方案2】:

通常的做法是通过 request.isSecure() 检查请求是否通过 https 进来。如果不是,则向浏览器发送重定向到相同的 URL,但以 https 协议为前缀。

这是一个执行此操作的示例 servlet 过滤器:

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SecurityFilter implements Filter {

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    HttpServletResponse servletResponse = (HttpServletResponse) response;
    if (!request.isSecure()) {
      HttpServletRequest servletRequest = (HttpServletRequest) request;
      String target = "https://" + request.getLocalName() + servletRequest.getRequestURI();
      servletResponse.sendRedirect(target);
      return;
    }
    // tell the browser to use only https for accessing this domain for the next 30 days
    servletResponse.addHeader("Strict-Transport-Security", "max-age=" + (30 * 24 * 60 * 60));
    chain.doFilter(request, response);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // not needed
  }

  @Override
  public void destroy() {
    // not needed
  }

}

要全局启用过滤器,请将以下内容添加到 web.xml:

<filter>
      <filter-name>securityFilter</filter-name>
      <filter-class>SecurityFilter</filter-class>
</filter>
<filter-mapping>
      <filter-name>securityFilter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>

servletResponse.addHeader("Strict-Transport-Security", ... 行是可选的。如果您将其放入代码中,您的浏览器在接下来的 30 天内将永远不会尝试再次连接到 http,而是会使用 https 本身。如果您的浏览器支持HSTS standard RFC6797,就会发生这种情况。如果您的应用程序只能通过 https 访问,那是有道理的。但是,我认为只有标准的 https 端口 443 才有可能。请参阅下一个。

在您当前的 tomcat 配置中存在一个小缺陷。不能在同一个端口上运行 http 和 https。您需要有两个单独的连接器,一个用于 http,一个用于 https。

要实现这一点,请添加到 maven tomcat 插件配置中:

<!-- Maven Tomcat Plugin -->
<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <configuration>
      <httpsPort>8443</httpsPort>
 . . .

您还需要在 SecurityFilter 代码中为重定向目标添加正确的协议(或使其成为参数):

      String target = "https://" + request.getLocalName() + ":8443" + servletRequest.getRequestURI();

8080 和 8443 端口仅用于实验性本地 Web 服务器,实际应用程序应在端口 80 和 443 上。

就是这样。玩得开心,祝你好运!

【讨论】:

    【解决方案3】:

    要要求 HTTPS 并让您的 servlet 引擎自动重定向到 https,您在使用传输保证的正确轨道上

    所以你可能会想要

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Protected Context</web-resource-name>
            <url-pattern>/login.html</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
     </security-constraint>
    

    以上内容只会将您的 webapp 的 /login.html 重定向到 https。根据需要添加更多 url-pattern。

    更多详情: http://wiki.apache.org/tomcat/FAQ/Security#Q4http://marc.info/?l=tomcat-user&m=104951559722619&w=2

    【讨论】:

    • 嘿..它不工作...在页面上显示“SSL连接错误”。
    【解决方案4】:

    这是你需要做的:

    1. 生成自签名证书并将其安装在 Tomcat 中(Gerold Broser 的帖子有链接)
    2. 默认情况下,Tomcat 中禁用 SSL 端口,启用它(与上面的链接相同)
    3. 将您的 URL 更改为 https://local_host:8443/login.html(Tomcat 的默认 SSL 端口)
    4. 通过浏览器发出请求,您应该会看到一个页面/消息,具体取决于浏览器,告诉您证书不正确

    如果您希望仅通过 SSL 访问此页面,请查看 Tim Funk 的帖子并编辑应用程序的 web.xml

    【讨论】:

      【解决方案5】:

      从上面的原始问题:

      我用来启动的命令是 mvn tomcat7:run 和 URL 使用 http://localhost:8080/login.html。它完美地工作。但是我要 将我的 URL 从 http 更改为 https 以及当我访问我的 URL 时,即 https://localhost:8080/login.html

      你确定'http://localhost:8080''https://localhost:8080'吗?

      这基本上意味着您从同一个端口请求 SSL 和非 SSL 流量。通常 Tomcat 从 8080 执行 HTTP,从 8443 执行 HTTPS。

      这里的大部分答案都对你有用,但首先要确定你是否在server.xml 中启用了 SSL 连接器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-06
        • 1970-01-01
        • 2012-05-22
        • 2020-05-09
        • 1970-01-01
        • 2011-11-07
        • 2017-09-10
        • 2010-12-14
        相关资源
        最近更新 更多