在没有指定用户名的情况下使用CLIENT-CERT for Tomcat

use*_*own 11 ssl tomcat web.xml client-certificates

我正在尝试使Tomcat Web应用程序对传入连接使用客户端证书身份验证.在server.xml中使用clientAuth = true时,一切正常,但由于在同一服务器上运行的其他应用程序,我们无法在生产环境中使用它.

有没有办法形成一个web.xml文档,以便它以与clientAuth = true相同的方式强制应用程序的客户端证书使用?似乎使用CLIENT-CERT设置还要求您为每个要访问系统的证书设置tomcat用户帐户?我们需要能够允许来自指定CA(在服务器信任库中设置)的所有证书,其中主题与特定规则匹配(在实际应用程序中检查).我希望以下的东西能起作用,但还没有运气!

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Everything</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<login-config>
    <auth-method>CLIENT-CERT</auth-method>
</login-config>
Run Code Online (Sandbox Code Playgroud)

Chr*_*ltz 5

首先,听起来像是您想要的,clientAuth=want而不是clientAuth=true:它将允许客户端提供证书,但并非绝对需要证书。

当您使用任何形式的身份验证时,Tomcat(或与此相关的任何servlet容器)都必须能够使用它来构建Principal对象-具有名称(通常是用户名)的对象。然后,容器必须决定用户具有什么角色才能正确授权特定请求。因此,Tomcat将需要事先了解用户才能使授权工作。

另一方面,如果不需要任何授权,则可以设置clientAuth=want然后使用Filter来验证证书。CLIENT-CERT如果您已经在进行自己的检查,则无需使用身份验证。


小智 5

我刚刚研究了上述问题的解决方案,终于找到了解决方案:

  1. 使用连接器 clientAuth="false" 属性配置 tomcat(否则所有到服务器的安全连接都将进行相互、客户端服务器、ssl 身份验证。

  2. 在 web.xml 中添加以下内容(我刚刚在此处显示了示例)

    <security-constraint>
        <web-resource-collection>
            <url-pattern>/LoginTestServlet1</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>manager</role-name>
        </auth-constraint>
        <user-data-constraint>
            <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
     <security-constraint>
        <web-resource-collection>
            <url-pattern>/LoginTestServlet2</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>manager</role-name>
        </auth-constraint>
       <!--  <user-data-constraint>
            transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
        </user-data-constraint> -->
    </security-constraint>
     <login-config>
        <auth-method>CLIENT-CERT</auth-method>
        <realm-name>certificate</realm-name>
      </login-config>
    
    Run Code Online (Sandbox Code Playgroud)

    经理

  3. 在 tomcat users-users.xml 中添加以下内容(请注意,如果信任存储区具有几乎相同的证书,那么您应该清楚地识别您的证书,如下所示)

    <role rolename="manager"/>
    
    Run Code Online (Sandbox Code Playgroud)

    <用户用户名=“EMAILADDRESS=hamzas100@yahoo.com,CN=KS,OU=OFF,O=OFS,L=布哈拉,
    ST=布哈拉,C=UZ”密码=“”角色=“经理”/>

  4. 输入浏览器(或curl)地址行:

    https://yourdomain.com:8443/LoginTest/LoginTestServlet1
    https://yourdomain.com:8443/LoginTest/LoginTestServlet2

  5. 为此,您必须将证书添加到浏览器个人证书列表(如果您正在使用浏览器进行测试)。我已经尝试过使用 Mozilla Firefox,它会很容易地让你做到这一点。(但它只接受 b12 证书,所以建议你应该使用 openssl 和 java keytool)。如果一切配置正确,那么您将收到 mozilla 的提示,要求您从现有证书中选择证书。如果您使用的是curl(它用于自动Web界面测试,则使用以下命令行进行测试(我刚刚在这里给出了一个示例。)请注意,您应该选择导入到信任存储中的证书。

    curl -s -k --cert selfsigned.pem --key key.pem -v --anyauth https://yourdomain.com:8443/LoginTest/LoginTestServlet1 --cacert selfsigned.pem 或curl -s -k --cert selfsigned.pem --key key.pem -v --anyauth http://yourdomain.com:8080/LoginTest/LoginTestServlet2 --cacert selfsigned.pem

注意:我的连接器如下所示:

<Connector port="8443"
       maxThreads="150"  scheme="https" secure="true" SSLEnabled="true"
       sslProtocol="TLS" keystoreType="PKCS12"  truststoreType="PKCS12" clientAuth="false"
               keystoreFile="C:/Program Files/glassfish-3.1.2/glassfish/domains/domain1/config/cacerts.pkcs12"
               truststoreFile= "C:/Program Files/glassfish-3.1.2/glassfish/domains/domain1/config/cacerts.pkcs12"
               truststorePass="changeit"
               keystorePass="changeit"
               protocol="org.apache.coyote.http11.Http11Protocol">
Run Code Online (Sandbox Code Playgroud)