首页 文章

用户主体除经过特定角色授权外,不适用于经过身份验证的用户

提问于
浏览
2

我有一个在Tomcat中运行的Web应用程序 . 我想允许服务器信任的有效证书的任何用户访问Web应用程序,但我想从证书中读取用户的DN . 我使用clientAuth =“true”配置了SSL连接器(“想要”也可以工作),web.xml有一个登录约束,要求提供客户端证书 . 我配置了一个信任库,其中包含用于签署用户证书的CA证书 .

客户端提供证书,服务器接受它,允许访问应用程序 . 我的问题是,当我在HttpServletRequest实例上调用getUserPrincipal()时,它为null . 如果我向web.xml添加auth-constraint,要求用户处于某个角色,并且用户配置为处于指定角色,则用户主体不为null .

HttpServletRequest的API表示如果用户未经过身份验证,则用户主体将为null . 我认为用户需要进行身份验证,因为他们已经提供了有效且受信任的证书,但显然在此上下文中的身份验证包括授权?

有没有办法可以从有效的受信任证书中访问用户主体,而无需将用户分配给特定角色?

这是web.xml片段,它导致主体为null:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>My App</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
</security-constraint>
<login-config>
    <auth-method>CLIENT-CERT</auth-method>
    <realm-name>My Realm</realm-name>
</login-config>

这里是web.xml片段,它使我能够在为指定角色配置用户时获取用户主体:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>My App</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>Some Role</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>CLIENT-CERT</auth-method>
    <realm-name>My Realm</realm-name>
</login-config>
<security-role>
    <role-name>Some Role</role-name>
</security-role>

2 回答

  • 3

    我发现尽管 HttpServletRequest#getUserPrincipal() 对于我描述的情况(用户已验证,但不需要或未尝试授权)返回null, ServletRequest#getAttribute("javax.servlet.request.X509Certificate") 返回一个证书数组,其中包含经过身份验证的用户的证书 .

    这解决了我的问题,但在我看来, HttpServletRequest#getUserPrincipal() 的行为仍然不符合API,即"Returns a java.security.Principal object containing the name of the current authenticated user" . 没有必要提及显式授权约束也必须应用和饱和,但这似乎是Tomcat实现它的方式 .

  • 0

    Tomcat不会要求提供证书,除非它被配置为要求所有连接,或者请求的资源需要特定的角色 .

    因此,您声明用户提供证书的说法不正确 .

相关问题