您可以在应用程序的web.xml定义一个安全约束:
<security-constraint>
<web-resource-collection>
<web-resource-name>SecureConnection</web-resource-name>
<url-pattern>*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint/>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
适应的url-pattern包含您的登录页和其他所有安全网页。 https的使用由用户数据约束来定义。
从Java EE tutorial:
如果指定的机密或 INTEGRAL作为安全限制,它 通常意味着使用SSL需要 并适用于匹配URL模式的所有请求 web 资源集合,而不仅仅是登录对话框的 。
如果你写了自己的登录()方法,使用的是GlassFish,你可以看看基于容器的身份验证与JDBCRealm作为替代登录方法。