您可以在應用程序的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作爲替代登錄方法。