Petstore_home/src/components/signon/src/com/sun/j2ee/bluePRints/signon/web/SignOnFilter.java它在初始化時會先讀取
。
先看SignOnFilter初始動作,約在87列:
public void init(FilterConfig config) throws ServletException {
this.config = config;
URL protectedResourcesURL = null;
try {
//謮取signon-config.xml
protectedResourcesURL =
config.getServletContext().getResource("/WEB-INF/signon-config.xml");
SignOnDAO dao = new SignOnDAO(protectedResourcesURL);
//讀取登入失敗畫面(signon_error.screen)
signOnErrorPage = dao.getSignOnErrorPage();
//讀取登入畫面(signon.screen)
signOnPage = dao.getSignOnPage();
//讀取所有欲保護畫面,組成HashMap
protectedResources = dao.getProtectedResources();
} catch (java.net.MalformedURLException ex) {
System.err.println("SignonFilter: malformed URL exception: " + ex);
}
}
signon-config>
<!-- Form Sign On Page(登入畫面)-->
<signon-form-login-page>
signon.screen
</signon-form-login-page>
<!-- Error Page When Sign On fails(登入失敗畫面)-->
<signon-form-error-page>
signon_error.screen
</signon-form-error-page>
<!-- A Protected Resource-->
<security-constraint>
<web-resource-collection>
<web-resource-name>Customer Screen</web-resource-name>
<url-pattern>customer.screen</url-pattern>
</web-resource-collection>
</security-constraint>
<!-- A Protected Resource(本例之保護畫面)-->
<security-constraint>
<web-resource-collection>
<web-resource-name>Customer Action</web-resource-name>
<url-pattern>customer.do</url-pattern>
</web-resource-collection>
</security-constraint>
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest)request;
String currentURI = hreq.getRequestURL().toString();
String currentURL = hreq.getRequestURI();
// get everything after the context root
int firstSlash = currentURL.indexOf("/",1); // jump past the starting slash
String targetURL = null;
//取得使用者欲前往之URL,以本例來說,即是customer.do
if (firstSlash != -1) targetURL = currentURL.substring(firstSlash + 1,
currentURL.length());
//判斷使用者從登入畫面(signon.screen)進行驗證工作
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {
validateSignOn(request, response, chain);
// jump out of this method
return;
}
// check if the user is signed on
//檢查使用者是否登入過,從session取出登入標記,作為判斷之用
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入過,則結束此Filter工作,進入Filter chain,以本例來說,它為Filter chain中最后一個Filter,所以就是不做任何事,讓使用者進入他的目的畫面
if (signedOn) {
chain.doFilter(request,response);
return;
}
// find out if the patterns match the target URL
//將使用者欲前往之URL與所有保護畫面URL做比對,若符合則導入登入畫面
(signon.screen)
Iterator it = protectedResources.keySet().iterator();
while (it.hasNext()) {
String protectedName = (String)it.next();
ProtectedResource resource =
(ProtectedResource)protectedResources.get(protectedName);
String urlPattern = resource.getURLPattern();
// now check agains the targetURL
//若符合則將目的URL存入Session,并轉導至登入畫面,結束Filter工作
if (urlPattern.equals(targetURL)) {
// put the orginal url in the session so others can access
hreq.getSession().setAttribute(ORIGINAL_URL, targetURL);
config.getServletContext().getRequestDispatcher("/" +
signOnPage).forward(request, response);
// Jump out of the filter and go to the next page
return;
}
}
// No matches if we made it to here
chain.doFilter(request,response);
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
URL protectedResourcesURL = null;
try {
//謮取signon-config.xml
protectedResourcesURL =
config.getServletContext().getResource("/WEB-INF/signon-config.xml");
SignOnDAO dao = new SignOnDAO(protectedResourcesURL);
//讀取登入失敗畫面(signon_error.screen)
signOnErrorPage = dao.getSignOnErrorPage();
//讀取登入畫面(signon.screen)
signOnPage = dao.getSignOnPage();
//請加入偵察程序代碼
System.out.println("signOnPage="+signOnPage);
System.out.println("signErrorPage="+signOnErrorPage);
//讀取所有欲保護畫面,組成HashMap
protectedResources = dao.getProtectedResources();
} catch (java.net.MalformedURLException ex) {
System.err.println("SignonFilter: malformed URL exception: " + ex);
}
}
doFilter()亦加入偵察程序代碼:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq = (HttpServletRequest)request;
String currentURI = hreq.getRequestURL().toString();
String currentURL = hreq.getRequestURI();
// get everything after the context root
int firstSlash = currentURL.indexOf("/",1); // jump past the starting slash
String targetURL = null;
//取得使用者欲前往之URL,以本例來說,即是customer.do
if (firstSlash != -1) targetURL = currentURL.substring(firstSlash + 1,
currentURL.length());
//請加入偵察程序代碼
System.out.println("targetURL="+targetURL);
//判斷使用者從登入畫面(signon.screen)進行驗證工作
if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) {
validateSignOn(request, response, chain);
// jump out of this method
return;
}
// check if the user is signed on
//檢查使用者是否登入過,從Session取出登入標記,作為判斷之用
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) !
= null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入過,則結束此Filter工作,進入Filter chain,以本例來說,它為
Filter chain中最后一個Filter,所以就是不做任何事,讓使用者進入他的目的畫面
if (signedOn) {
chain.doFilter(request,response);
return;
}
// find out if the patterns match the target URL
//將使用者欲前往之URL與所有保護畫面URL做比對,若符合則導入登入畫面
(signon.screen)
Iterator it = protectedResources.keySet().iterator();
while (it.hasNext()) {
String protectedName = (String)it.next();
ProtectedResource resource =
(ProtectedResource)protectedResources.get(protectedName);
String urlPattern = resource.getURLPattern();
// now check agains the targetURL
//若符合則將目的URL存入Session,并轉導至登入畫面,結束Filter工作
if (urlPattern.equals(targetURL)) {
//請加入偵察程序代碼
System.out.println("URL Matched! urlPattern="+urlPattern);
// put the orginal url in the session so others can access
hreq.getSession().setAttribute(ORIGINAL_URL, targetURL);
config.getServletContext().getRequestDispatcher("/" +
signOnPage).forward(request, response);
// Jump out of the filter and go to the next page
return;
}
}
// No matches if we made it to here
chain.doFilter(request,response);
}接著請重新編譯及部署新的程序代碼,在命令模式下:切換至Petstore_home/ src/webservices/apps/petstore/src目錄如:cd D:/petstore1.3.1/src/webservices/apps/petstore/src(出處:http://m.survivalescaperooms.com)
新聞熱點
疑難解答