在Tomcat中配置基于url的HTTPS证书使用

发布于 2024-10-03 16:58:59 字数 2654 浏览 2 评论 0原文

我正在开发一个 Web 应用程序,它在使用 Jersey 进行 Web 服务调用期间使用客户端证书对 Tomcat 进行身份验证。到目前为止,效果很好,但我需要一个位于相同上下文的 Web 前端,以便我管理此应用程序。由于 SSL 配置是“每个上下文”,因此让前端使用 https 的唯一选择似乎是向访问浏览器安装客户端证书,该证书也列在 tomcat 的信任库中(要么完全放弃使用 https) 。

为了说明我真正想要的:

1. https://url-to-webapp/ws <- Should use client certificate
2. https://url-to-webapp/web <- Should just use a server certificate

这可以在 Tomcat 配置中甚至在应用程序代码中以某种方式实现吗?

更新

我尝试了 EJP 建议的配置,但现在无论我使用证书如何都无法连接到 Tomcat - 它似乎在查找或其他过程中失败。如果我在 8080 上创建 HTTP 连接器,它会将我重定向到 8443。这是我正在使用的配置。有什么想法吗?

tomcat-users.xml

<tomcat-users>
<role rolename="webservice"/>
<user username="CN=ClientCert,OU=Corp,O=Corp,L=London,S=London,C=UK" password="" roles="webservice"/>
</tomcat-users>

server.xml

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="c:\tomcat\keys\server.jks" keystorePass="password"
truststoreFile="c:\tomcat\keys\client.jks" truststorePass="password"/>

web.xml

[...]
    <security-constraint>
        <display-name>ClientCertificateRequired</display-name>
        <web-resource-collection>
            <web-resource-name>MyWebService</web-resource-name>
            <description/>
            <url-pattern>/webservice/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>webservice</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description/>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>CLIENT-CERT</auth-method>
        <realm-name>tomcat-users</realm-name>
    </login-config>
    <security-role>
        <description/>
        <role-name>webservice</role-name>
    </security-role>
    [...]
    <servlet>
        <display-name>Webservice</display-name>
        <servlet-name>Webservice</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        [...]
            <run-as>
            <role-name>webservice</role-name>
        </run-as>
    </servlet>
    [...]

I am developing a webapp that uses client certificates to authenticate against Tomcat during web service calls with Jersey. This is working great so far, but I need a web frontend on the same context that will let me administer this application. Since the SSL configuration is "per context", the only option to have the frontend use https seems to be installing a client certificate to the accessing browser, that is listed in tomcat's truststore as well (either that or discarding the use of https altogether).

To illustrate what I really want:

1. https://url-to-webapp/ws <- Should use client certificate
2. https://url-to-webapp/web <- Should just use a server certificate

Can this be achieved somehow in the Tomcat configuration, or even in the application code?

Update

I tried the configuration suggested by EJP but now I can't connect to Tomcat regardless of my use of certificates - it seems to fail during the lookup or something. If I create a HTTP connector on 8080 though, it redirects me to 8443. This is the configuration I'm using. Any ideas?

tomcat-users.xml

<tomcat-users>
<role rolename="webservice"/>
<user username="CN=ClientCert,OU=Corp,O=Corp,L=London,S=London,C=UK" password="" roles="webservice"/>
</tomcat-users>

server.xml

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="true" sslProtocol="TLS"
keystoreFile="c:\tomcat\keys\server.jks" keystorePass="password"
truststoreFile="c:\tomcat\keys\client.jks" truststorePass="password"/>

web.xml

[...]
    <security-constraint>
        <display-name>ClientCertificateRequired</display-name>
        <web-resource-collection>
            <web-resource-name>MyWebService</web-resource-name>
            <description/>
            <url-pattern>/webservice/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>webservice</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description/>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>CLIENT-CERT</auth-method>
        <realm-name>tomcat-users</realm-name>
    </login-config>
    <security-role>
        <description/>
        <role-name>webservice</role-name>
    </security-role>
    [...]
    <servlet>
        <display-name>Webservice</display-name>
        <servlet-name>Webservice</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        [...]
            <run-as>
            <role-name>webservice</role-name>
        </run-as>
    </servlet>
    [...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-10-10 16:58:59

您可以将 Tomcat 配置为使用客户端证书重新协商(而不是初始协商),以便是否请求客户端证书取决于所请求的 URL。

为此,您需要在连接器配置中使用 clientAuth="false",然后在配置中使用 CLIENT-CERT您想要使用客户端证书保护的 web 应用程序。

请注意,这使用重新协商,因此您可能必须处理 TLS 重新协商错误问题。简而言之,2009 年 11 月左右发布了一个 TLS 协议缺陷。立即的安全修复是禁用重新协商(除非强制使用非安全选项),然后实施 RFC 5746。请参阅中的第 1 阶段和第 2 阶段修复Oracle Java 传输层安全 (TLS) 重新协商问题自述文件

对于您想要执行的操作,您需要启用重新协商,并且为了保证这一点的安全,您必须使用 JRE 版本 1.6.0_22。

You can configure Tomcat to use client-certificate re-negotiation (as opposed to initial negotiation), so that whether-or-not to ask for a client cert depends on the URL requested.

To do this, you need to use clientAuth="false" in the connector configuration and then <auth-method>CLIENT-CERT</auth-method> in the webapp you want to protect with a client certificate.

Note that this uses re-negotiation and thus you may have to deal with the TLS re-negotiation bug issues. In short, there was a TLS protocol flaw published around November 2009. The immediate security fix was to disable re-negotiation (unless forcing the non-safe option) and then the implementation of RFC 5746. See phase 1 and phase 2 fixes in the Oracle Java Transport Layer Security (TLS) Renegotiation Issue Readme.

For what you're trying to do, you need re-negotiation to be enabled, and for this to be secure, you'd have to use the JRE release 1.6.0_22.

栀子花开つ 2024-10-10 16:58:59

只需将第一个 URL 定义为安全且需要机密性和特定角色,并将 Web 应用程序定义为使用 SSL 客户端身份验证。将第二个 URL 定义为不需要角色。这一切都在 web.XML 中。然后为自己定义一个适当的领域来检查身份并从中获取角色。

Just define the first URL as secured and requiring both confidentiality and a specific role, and define the web app as using SSL client authentication. Define the second URL as not requiring a role. This is all in web.XML. Then define yourself an appropriate Realm to check identities against and get roles from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文