customer do此目的URL存 Session,將 request轉導至登入畫面( Signon. screen), 要求使用者進行登入動作。 第一階段驗證 口說無憑·我們可加入偵察程式碼來驗證程式是否如筆者所迒般運行·請在 SignOn Filter initi0加人兩行程式 public void init( FilterConfig config) throws Servlet Exception i URL protected ResourcesURL =null /讀取 Signon- config. xml protectedResourcesURL config. get ServletContext getResource("/WEB-INF/signon-config xml") SignOnDAO dao- new SignOnDAO(protectedResourcesURL) /讀取登廴失敗畫面( (signon error, screen signOn ErrorPage-dao getSignOnErrorPageO 讀取登入畫面( Signon. screen) signOnPage = dao. getSignOnPage0; /加入偵察程式碼 System. out. printIn("signOnPage="+signOnPage); System. out printIn(" sign ErrorPage="+signOn ErrorPage); /讀取所有欲保護畫面’組成 HashMap protectedResources= dao. getProtectedResourceso i catch (java. net. MalformedURLException ex)i System. err printIn("Signon Filter: malformed URL exception: ex) do Filter亦加入偵察程式碼 public void doFilter( Servlet Request request, ServletResponse response Filter Chain chain) throws IOException, ServletException i HttpServletrequesthreq=(httpServletrequest)requEst String currentURI - hreq getRequestURLO toString 0; String currentURL=hreq getRequestURIO l get everything after the context root int firstSlash= currentURL. indexof("/", 1); //jump past the starting slash
customer.do 此目的 URL 存入 Session,將 request 轉導至登入畫面(signon.screen), 要求使用者進行登入動作。 第一階段驗證 口說無憑,我們可加入偵察程式碼來驗證程式是否如筆者所述般運行,請在 SignOnFilter.init()加入兩行程式: 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 (firstSlashI=-1)targetURL= currentURL Substring(firstSlash+1 currentURL. length); /請加入偵察程式碼 System.out. printIn("targetURL="+targetURL); /判斷使用者從登入畫面( (signon. screen)行驗證工作 if(targetURL null)&& targetURL equals(FORM SIGNON URL)( validate SignOn(request, response, chain) //jump out of this method return // check if the user is signed ∥檢查使用者是否登入過·從 Session取出登入標記,作判斷之用 boolean signedon= false if(hreq getSession(. getAttribute(SIGNED ON USER)!=null)( Boolean) hreq. getSession( get Attribute(SIGNED ON USER). boolean value i else i hreq getSession(). setAttribute(SIGNED ON USER, new Boolean(false)); // jump to the resource if signed on 若已登入過’則結束此 Filter工作,進入 Filter chain,以本例來說,它 駑 Filter chain中最後一個 Filter·所以就是不做任何事·讓使用者進入 他的目的畫面 if(signedOn)i chain. doFilter(request, response ); return l find out if the patterns match the target URL ∥将使用者欲前往之URL與所有保護畫面URL做比對·若符合則導入登 入畫面( signonscreen) Iterator it- protected Resources. key Seto). iterator; while(it has Nexto)( String protectedName =(String)it. next(; Protected Resource resource
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)).boolean Value(); } 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 =