使用CLIENT-CERT保护tomcat 8.5中的webresource

时间:2017-01-03 07:34:27

标签: tomcat spring-security tomcat8

我是tomcat的新手,我使用tomcat 8.5使用Spring框架编写了一些web服务。我想用auth类型的CLIENT-CERT保护特定的webresource。我已将server.xml配置为

        <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
        maxThreads="150" scheme="https" secure="true"
        clientAuth="false" sslProtocol="TLS"
        keystoreFile="<path>/TestServerp12.pfx"
        truststoreFile="<path>/truststore.jks"
        truststorePass="****"
        keystoreType="PKCS12"
        truststoreType="JKS"
        keystorePass="******" />

在web.xml

中添加如下
    <security-constraint>
            <web-resource-collection>
                <web-resource-name>App</web-resource-name>
                <url-pattern>/authenticate/*</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>cert</role-name>
            </auth-constraint>
            <user-data-constraint>
                <transport-guarantee>CONFIDENTIAL</transport-guarantee>
            </user-data-constraint>
        </security-constraint>

        <login-config>
            <auth-method>CLIENT-CERT</auth-method>
        </login-config>

我已经关注了几个博客,例如https://twoguysarguing.wordpress.com/2009/11/03/mutual-authentication-with-client-cert-tomcat-6-and-httpclient/和stackoverflow问题。

使用上面的配置,受保护的URL会抛出403,所以我必须在web.xml中添加安全角色

    <security-role>
            <role-name>cert</role-name>
        </security-role>

    and below in tomcat users

    <role rolename="cert"/>
        <user username="EMAILADDRESS=testclient3@email.com, CN=TestClient3, OU=Test, O=MyO, L=TestL, ST=TestST, C=LA" password="null" roles="cert"/>

添加此项后,SSL握手成功,但用户名(客户端证书的区分名称)是硬编码的,这实际上意味着其他用户无法访问它。

在没有硬编码用户的情况下,在tomcat中启用CLIENT-CERT auth的方法是什么?

1 个答案:

答案 0 :(得分:1)

您无需任何外部工具即可完成此操作。从你的问题中可以清楚地知道你正在尝试做什么。您真正想要做的是将所有用户视为一个组,而不必将每个用户明确地放入该角色(例如,使用用户数据库)。

这可以使用Tomcat的allRolesMode来完成,您可以在Realm section of the Tomcat users guide中阅读。

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
    allRolesMode="authOnly"
    resourceName="UserDatabase" />

现在在WEB-INF/web.xml,您需要将<security-constraint>设置为要求&#34;任何角色&#34;:

<security-constraint>
  <web-resource-collection>
    <web-resource-name>My Secure Area</web-resource-name>
    <url-pattern>/secure</url-pattern>
  </web-resource-collection>
  <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>
  <auth-constraint>
      <role-name>*</role-name>
  </auth-constraint>
</security-constraint>

此技术适用于所有当前支持的Apache Tomcat版本(6.0 - 9.0)。