【轉(zhuǎn)自】http://caterpillar.onlyfun.net/Gossip/JSPServlet/JSPServlet.htm

Http Basic Authentication 會讓瀏覽器出現(xiàn)對話方塊,以供使用者輸入名稱與密碼,無法自行設(shè)計登入畫面,若要結(jié)束目前會話階段,則要關(guān)閉瀏覽器。
您可以在web.xml中設(shè)定基於表單的驗(yàn)證方式,以
使用宣告式安全(Http Basic Authentication) 中的例子來說,可以修改為:

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<security-role>
<role-name>foo</role-name>
</security-role>

<security-constraint>
<display-name>SecurityConstraint</display-name>
<web-resource-collection>
<web-resource-name>Secret Information</web-resource-name>
<url-pattern>/secure/info.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>foo</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/pages/login.jsp</form-login-page>
<form-error-page>/pages/error.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>

在<login-config>中,修改驗(yàn)證方式為FORM,並設(shè)定了登入表單的頁面所在,您可以設(shè)計一個login.jsp:
  • login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>請登入</title>
</head>
<body>
<h2>請登入</h2><br>
<form action="j_security_check" method="POST">
Username: <input type="text" name="j_username" value="" /> <br />
Password: <input type="password" name="j_password" value="" /> <br />
<input type="submit" value="login" />
</form>
</body>
</html>

當(dāng)使用者請求受保護(hù)的URL時,會轉(zhuǎn)至login.jsp,發(fā)送表單的動作網(wǎng)址為j_security_check,而使用者 名稱與密碼,則必須使用j_username與j_password請求參數(shù)發(fā)送,若驗(yàn)證正確,則會將流程轉(zhuǎn)至原先請求的位址,若失敗,則流程會轉(zhuǎn)至所設(shè) 定的錯誤網(wǎng)頁。
如果要令此次登入失效,則設(shè)計一個令HttpSession執(zhí)行invaldate()方法的請求即可。
如果您的Web伺服器作好支援SSL的設(shè)定,則您可以在web.xml中設(shè)定使用SSL,以上例來說,您可以在web.xml中增加設(shè)定:
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<security-role>
<role-name>foo</role-name>
</security-role>

<security-constraint>
<display-name>SecurityConstraint</display-name>
<web-resource-collection>
<web-resource-name>Secret Information</web-resource-name>
<url-pattern>/secure/info.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>foo</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/pages/login.jsp</form-login-page>
<form-error-page>/pages/error.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>

如此,當(dāng)流程轉(zhuǎn)發(fā)至表單網(wǎng)頁,直至使用者名稱、密碼送出,都會以SSL的方式進(jìn)行加密傳送。

文章來源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!788.entry