国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Petstore源碼追蹤記(3)-商業邏輯處理(二)

2019-11-18 16:21:54
字體:
來源:轉載
供稿:網友

  使用者基本數據瀏覽流程
=======================
現在讓我們進入主題-Petstore商業邏輯,筆者以使用者基本資料瀏覽流程為例,請激活cloudscape數據庫
cloudscape –start

圖1 激活數據庫

接著激活RI application Server(應用服務器):
j2ee –verbose

圖2 激活RI

Petstore系統激活無誤后,請開啟瀏覽器,輸入
http://localhost:8080/petstore/index.jsp

圖3 進入Petstore系統

進入系統看到那大大的鸚鵡頭,請點選右上角的”Account”連結,
進入使用者登入流程:

圖4 Petstore首頁

我們會看到登入畫面,直接使用預設的使用者(j2ee)及密碼(j2ee),
點選”Sign In”鈕:

圖5 登入畫面

看到以下顯示個人信息畫面就表示我們已登入成功啦!

圖6 個人信息畫面

若此時我們按瀏覽器之”上一頁”鈕返回首頁:

圖7 再返回首頁

再按右上角”Account”連結,會發現畫面直接跳至個人信息畫面:

圖8 個人信息畫面

     請注意圖4 Petstore首頁畫面左下角出現的

URL:http://localhost:8080/petstore/customer.do,它其實就是圖6個人信息畫面,但系統并沒有直接從圖4跳至圖6,而先換成圖5登入畫面,要求我們做登入動作,輸入帳號及密碼,驗證成功后才跳至圖6;若是再次從首頁進入個人信息,系統并不會再次要求登入,在這里有兩個重點要提:
1.SignOnFilter:若使用者進入的頁面是受到保護的,則系統會先將畫面轉至登入畫面,要求登入。
2.customer.do:它代表的是一個動作加一個畫面的組合,以本例來說,從數據庫讀取個人信息,組成完整Html畫面顯示。

SignOnFilter
     筆者將使用者進入使用者基本資料瀏覽畫面的流程分為三個階段:
1.使用者欲進入使用者基本資料瀏覽畫面(customer.do),因未登入過,被SignOnFilter攔截,轉至登入畫面(signon.screen)。
2.使用者輸入帳號及密碼按”sumit”后,再度由SignOnFilter攔截,SignOnFilter亦負責帳號、密碼檢核工作,確認無誤后,則將網頁轉導(forward)至第一階段使用者欲進入之使用者基本資料瀏覽畫面(customer.do)。
3.重復第一階段動作,SignOnFilter檢查使用者已登入過,放行轉導至使用者基本資料瀏覽畫面(customer.do)。

第一階段
欲觀察Servlet Filter,先要了解它的影響范圍,請開啟deploytool(注2),鼠標點選PetstoreWAR,選擇右邊Filter Mapping頁,會發現此Filter的影響范圍是所有網頁。

圖9 Filter影響范圍

也可在web.xml看到設定,請參考前面敘述,接下來請開啟SignOnFilter.java
,它的源碼位置在
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);
    }
}
它在初始化時會先讀取
Petstore_home/src/apps/petstore/src/docroot/WEB-INF/signon-config.xml
,并組成Data access Object(DAO),以方便后續程序存取(注3),此xml檔案主
要功用記錄登入畫面、登入失敗畫面及所有需登入才能使用的畫面之URL,以下是signon-config.xml片段:
<
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>

接著請看SignOnFilter實際運作的主要函式doFilter(),約在107列:

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);
}

SignOnFilter先取得使用者的目的URL(customer.do),判斷使用者并未登入,開始比對目的URL是否在保護畫面中,發現customer.do為保護畫面,將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 (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

bill-轉自:csdn

(出處:http://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 琼结县| 虞城县| 灵川县| 任丘市| 柳州市| 阜新市| 略阳县| 永宁县| 东乌珠穆沁旗| 邵武市| 梅河口市| 海城市| 和林格尔县| 安新县| 驻马店市| 延川县| 高淳县| 增城市| 都匀市| 临潭县| 米脂县| 邵东县| 康马县| 皮山县| 海林市| 东宁县| 保靖县| 家居| 吕梁市| 大荔县| 霍林郭勒市| 普格县| 神木县| 紫金县| 高安市| 广东省| 安新县| 祁东县| 吉木乃县| 福州市| 黎平县|