使用 Java EE 中基于表单的身份验证来保护 Web 应用程序,
我们可以指定登录和错误 html 页面。http://docs.oracle.com/javaee/5/tutorial/doc/bncbe.html http://docs.oracle.com/javaee/5/tutorial/doc/bncbe.html
Example:
<login-config>
<auth-method>FORM</auth-method>
<realm-name>file</realm-name>
<form-login-config>
<form-login-page>/logon.jsp</form-login-page>
<form-error-page>/logonError.jsp</form-error-page>
</form-login-config>
</login-config>
使用 HTTP Basic 身份验证,我们无法指定登录页面
因为客户端 Web 浏览器有责任获取用户的登录名/密码(通常使用弹出窗口)。
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
但是,我们仍然需要指定一个错误页面。那可能吗?如何?
也就是说,使用 HTTP-Basic 身份验证(和 Java EE),如果提供的登录/密码不正确(如基于表单的身份验证),我们需要显示特定的错误页面。
不,在 BASIC 身份验证中您无法配置loginpage/errorpage
。这是不同身份验证的工作原理:
表格登录:
- 客户端向安全页面发出请求
/application/securedpag
- 服务器发送
200 Status code along with login.jsp
在登录配置中配置
- 客户填写
j_username
and j_password
并提交给j_security_check
servlet
- Servlet
j_security_check
在服务器端调用,验证j_username
and j_password
. If authenication is successful, the request is forwarded/redirected to the secured page
. If the authentication fails, the error page is sent
(在登录配置中配置)。
基本登录:
-
客户端向安全页面发出请求/application/securedpage
-
服务器发送401 status code
要求客户发送Authorization header with value containing Base64 encoded username and passowrd
.
-
浏览器将显示弹出窗口询问用户名和密码。
-
浏览器将再次向安全页面发出请求/application/securedpage
随着Authorization header with value containing Base64 encoded username and passowrd
-
If authenication is successful, the request is forwarded/redirected to the secured page
. If the authentication fails, again the challange i.e,
401状态代码`被发送到浏览器。
-
浏览器将再次显示弹出窗口询问用户名和密码。
BASIC 登录流程清楚地表明没有响应正文发送到的位置。服务器仅发送401 status code
用于首次询问凭据或身份验证失败时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)