[英]How to avoid adding principal in glassfish-web.xml file when using auth-method=CLIENT-CERT?
我刚刚开始为Glassfish 4.1编写Web应用程序,并且从安全性方面入手。
访问该应用程序的用户将使用客户端证书标识自己。 我创建了一个自签名的根CA。 将此根CA添加到domain-dir /config/cacerts.jks。 创建了一个客户端证书,并用根CA对其进行了签名并将其添加到我的Web浏览器中。
web.xml中:
<web-app>
<!-- other stuff -->
<security-constraint>
<web-resource-collection>
<web-resource-name>foo</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>operator</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>
<security-role>
<role-name>operator</role-name>
</security-role>
</web-app>
GlassFish的-web.xml中:
<glassfish-web-app>
<context-root>/foo</context-root>
<security-role-mapping>
<role-name>operator</role-name>
<principal-name>CN=user</principal-name>
</security-role-mapping>
</glassfish-web-app>
这可行,但是我不想在glassfish-web.xml中为每个新用户添加一个主体名称。 我想从数据库中获取该信息以及每个用户的组信息。
我已经研究并尝试编写自己的扩展com.sun.appserv.security.AppservRealm的领域。我以适当的方式添加了该领域,但是只有在将web.xml中的auth-method设置为BASIC时才能使用它。 使用CLIENT-CERT将忽略realm-name元素:
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>my-realm</realm-name>
</login-config>
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>my-realm</realm-name>
</login-config>
在第二种情况下,将使用预定义的领域“证书”。 使用BASIC显然不会使用客户端证书。
还有另一种方法,那就是修改domain-dir /config/domain.xml并替换
...
<config name="server-config">
...
<security-service>
...
<auth-realm classname="com.sun.enterprise.security.auth.realm.certificate.CertificateRealm" name="certificate"></auth-realm>
同
...
<config name="server-config">
...
<security-service>
...
<auth-realm classname="org.foo.MyCertificateRealm" name="certificate"></auth-realm>
但这也无济于事。 我在日志中收到以下警告:“证书身份验证需要证书领域。” Googling告诉我,使用的领域必须是CertificateRealm的实例,我认为这意味着com.sun.enterprise.security.auth.realm.certificate.CertificateRealm ,但是该类是最终的,因此我无法对其进行扩展。
除了在glassfish-web.xml中,没有其他方法可以定义您的主体及其组吗? 我应该抛弃Glassfish并使用其他东西,例如JBoss吗?
编辑2015-03-31:
问了这个问题后不久,我尝试了Jetty,在那里我做了自己想做的事。 我什至可以将CLIENT-CERT身份验证与FORM身份验证结合使用,如果客户端不提供有效证书,则该用户将被重定向到基于表单的登录页面,并可以使用用户名进行身份验证和密码。
然后在身份验证时将角色添加到用户,以便Jetty可以控制对资源的访问。
如果可以在CLIENT-CERT模式下完成对Glassfish中用户的角色分配控制,而不必将每个用户都添加到glassfish-web.xml中,但是能够从例如数据库中提取此信息,并允许Glassfish控制对资源的访问仍然是一个有效的问题。 但是我不急于得到答案。
我遇到了同样的问题,并通过以下链接解决了此问题: 此处
只需使用domain.xml文件中的assign-groups属性,如下所示:
<auth-realm classname="com.sun.enterprise.security.auth.realm.certificate.CertificateRealm" name="certificate"> <property name="assign-groups" value="allusers"></property> </auth-realm>
allusers将是为所有成功使用有效证书登录的用户动态创建的组。
将组名标签和值替换为主体标签和值,并作为allusers。 因此,您只需要像这样修改glassfish-web.xml即可:
<glassfish-web-app>
<context-root>/foo</context-root>
<security-role-mapping>
<role-name>operator</role-name>
<group-name>allusers</group-name>
</security-role-mapping>
</glassfish-web-app>
无需在web.xml中添加主体。 您可以在Servlet中检入连接了哪个证书后,可以使用以下方法检索它:
X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
并使用以下方法检查DN部分:
String dn = certs[0].getSubjectX500Principal().getName();
希望对您有所帮助。谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.