天空是藍(lán)色的

          做好軟件為中國(guó) #gcc -c helloworld.c -o helloworld.o //編譯目標(biāo)文件 #gcc helloworld.o -o helloworld //編譯成可執(zhí)行exe #helloworld //運(yùn)行exe
          數(shù)據(jù)加載中……
          How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl

          How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl

          思路
          首先BasePage是需要登陸的 protected ,
          BasePage實(shí)現(xiàn)org.apache.tapestry.event.PageValidateListener方法,判斷asm對(duì)象Visit是否已經(jīng)登陸
          如沒(méi)visit==null,去cookies找回用戶名和密碼從cycle中獲得Login頁(yè)面
          Login頁(yè)面實(shí)現(xiàn)common.tapestry.IConstructVisit接口。LoginPage登陸時(shí)保存用戶名密碼到cookies中,并對(duì)cookies的值加密,
          cookies的key是baseUrl+key,取回時(shí)對(duì)cookies值解密,加密解密的password放到Application的meta信息中

          實(shí)現(xiàn)代碼如下
          抽象接口LoginCookiesManager

          package common.tapestry.engine;

          public interface LoginCookiesManager {
           
           String APPLICATION_META_KEY_DEFAULT_COOKIES_IS_ENCRYPTION = "common.tapestry.default-cookies-encrypt";//是否需要加密,默認(rèn)加密
           String APPLICATION_META_KEY_DEFAULT_COOKIES_ENCRYPTION_KEY = "common.tapestry.default-cookies-encryption-key";//加密的password

           public static final String COOKIES_KEY_LOGIN_USERNAME = "USERNAME";
           public static final String COOKIES_KEY_LOGIN_PASSWORD = "PASSWORD";
           
           /**
            * 將用戶名和密碼寫入cookies
            * @param username
            * @param password
            * @param maxAge
            */
           void writeCookies(String username,String password,int maxAge);
           /**
            * 清除用戶名和密碼cookies
            */
           void removeCookies();
           /**
            * 從cookies中獲得用戶名
            * @return
            */
           String getUsername();
           /**
            * 從cookies中獲得密碼
            * @return
            */
           String getPassword();
          }

          LoginCookiesManagerImpl 實(shí)現(xiàn)

          package common.tapestry.engine;

          import org.apache.hivemind.ApplicationRuntimeException;
          import org.apache.tapestry.engine.IPropertySource;
          import org.apache.tapestry.services.AbsoluteURLBuilder;
          import org.apache.tapestry.services.CookieSource;
          import org.apache.tapestry.web.WebRequest;
          import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
          import org.jasypt.exceptions.EncryptionOperationNotPossibleException;

          public class LoginCookiesManagerImpl implements LoginCookiesManager {
           private static StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
           private IPropertySource applicationPropertySource;
           private CookieSource cookieSource;
           private WebRequest request;
           private AbsoluteURLBuilder absoluteURLBuilder;

           public void setApplicationPropertySource(
             IPropertySource applicationPropertySource) {
            this.applicationPropertySource = applicationPropertySource;
            String encrypKey = getCookiesEncryptionKey();
            standardPBEStringEncryptor.setPassword(encrypKey);
           }

           private String decrypt(String value) {
            String tmp = value;
            try {
             tmp = standardPBEStringEncryptor.decrypt(value);
            } catch (EncryptionOperationNotPossibleException e) {
             throw new ApplicationRuntimeException("please clear your cookies");
            }
            return tmp;
           }

           private String encrypt(String value) {
            return standardPBEStringEncryptor.encrypt(value);
           }

           private String getCookiesEncryptionKey() {
            String temp = applicationPropertySource
              .getPropertyValue(APPLICATION_META_KEY_DEFAULT_COOKIES_ENCRYPTION_KEY);
            return (temp != null) ? temp : "crypassword";
           }

           private boolean isCookiesEncryption() {
            String temp = applicationPropertySource
              .getPropertyValue(APPLICATION_META_KEY_DEFAULT_COOKIES_IS_ENCRYPTION);
            return (temp != null) ? Boolean.valueOf(temp).booleanValue() : true;
           }

           public void removeCookies() {
            String cookiesKeyLogin_username = generateApplicationCookiesKey_Login__Username();
            String cookiesKeyLogin_password = generateApplicationCookiesKey_Login__Password();
            cookieSource.removeCookieValue(cookiesKeyLogin_username);
            cookieSource.removeCookieValue(cookiesKeyLogin_password);
           }

           public void writeCookies(String username, String password, int maxAge) {
            writeCookiesValue_Login__Username(username, maxAge);
            writeCookiesValue_Login__Password(password, maxAge);
           }

           public String getUsername() {
            String value = cookieSource
              .readCookieValue(generateApplicationCookiesKey_Login__Username());
            if (isCookiesEncryption()) {
             value = decrypt(value);
            }
            return value;
           }

           public String getPassword() {
            String value = cookieSource
              .readCookieValue(generateApplicationCookiesKey_Login__Password());
            if (isCookiesEncryption()) {
             value = decrypt(value);
            }
            return value;
           }

           private void writeCookiesValue_Login__Username(String username, int maxAge) {
            String key = generateApplicationCookiesKey_Login__Username();
            String value = username;
            if (isCookiesEncryption()) {
             value = encrypt(username);
            }
            cookieSource.writeCookieValue(key, value, maxAge);
           }

           private void writeCookiesValue_Login__Password(String password, int maxAge) {
            String key = generateApplicationCookiesKey_Login__Password();
            String value = password;
            if (isCookiesEncryption()) {
             value = encrypt(password);
            }
            cookieSource.writeCookieValue(key, value, maxAge);
           }

           private String generateApplicationCookiesKey_Login__Username() {
            return generateApplicationCookiesKey(COOKIES_KEY_LOGIN_USERNAME);
           }

           private String generateApplicationCookiesKey_Login__Password() {
            return generateApplicationCookiesKey(COOKIES_KEY_LOGIN_PASSWORD);
           }

           private String generateApplicationCookiesKey(String key) {
            String contextPath = request.getContextPath();
            return absoluteURLBuilder.constructURL(contextPath + "/") + key;
           }

           public void setCookieSource(CookieSource cookieSource) {
            this.cookieSource = cookieSource;
           }

           public void setRequest(WebRequest request) {
            this.request = request;
           }

           public void setAbsoluteURLBuilder(AbsoluteURLBuilder absoluteURLBuilder) {
            this.absoluteURLBuilder = absoluteURLBuilder;
           }

          }

          hivemodule.xml配置

          <?xml version="1.0"?>
          <module id="common.tapestry" version="1.0.0">
          <service-point id="LoginCookiesManager" interface="common.tapestry.engine.LoginCookiesManager">
          <invoke-factory>
             <construct class="common.tapestry.engine.LoginCookiesManagerImpl" >
               <set-object property="cookieSource" value="infrastructure:cookieSource"/>
               <set-service property="absoluteURLBuilder" service-id="tapestry.request.AbsoluteURLBuilder"/>
                        <set-service property="request" service-id="tapestry.globals.WebRequest"/>  
                        <set-object property="applicationPropertySource" value="service:tapestry.props.ApplicationPropertySource"/>             
                </construct>
            </invoke-factory> 
          </service-point>

          <contribution configuration-id="tapestry.Infrastructure">
              <property name="loginCookiesManager" object="service:common.tapestry.LoginCookiesManager"/>
          </contribution>
          </module>

          使用

          在BasePage中獲得該service

          public LoginCookiesManager getLoginCookiesManager() {
            LoginCookiesManager svc = (LoginCookiesManager) getRequestCycle().getInfrastructure().getProperty("loginCookiesManager");
            return svc;
           }
           
          /**
            * 判斷用戶是否已經(jīng)登陸
            *
            * @return
            */
           private boolean isUserLoggedIn() {
            Object asmVisit = this.getRequestCycle().getInfrastructure().getApplicationStateManager().get(ASM_VISIT);

            if (!(asmVisit instanceof IVisit)) {
             throw new ApplicationRuntimeException(
               "The visit class must implements " + IVisit.class);
            }
            IVisit visit = (IVisit) asmVisit;
            if (visit == null || (visit != null && !visit.isLoggedIn())) {
             IPage loginPage = getRequestCycle().getPage(PAGE_LOGIN);
             if (loginPage instanceof IConstructVisit) {
              //從cookies中找回用戶名和密碼
              String cookiesLoginUsername = getLoginCookiesManager().getUsername();
              String cookiesLoginPassword = getLoginCookiesManager().getPassword();
              IConstructVisit constructVisit = (IConstructVisit) loginPage;
              visit = constructVisit.doConstructVisit(this.getRequestCycle(),
                cookiesLoginUsername, cookiesLoginPassword);
             } else {
              throw new ApplicationRuntimeException("The " + PAGE_LOGIN
                + " class must implements " + IConstructVisit.class);
             }
            }
            if (visit == null)
             return Boolean.FALSE.booleanValue();
            return visit.isLoggedIn();
           } 


          Login類必須實(shí)現(xiàn)如下接口
          package common.tapestry;

          import org.apache.tapestry.IRequestCycle;

          public interface IConstructVisit {
           public IVisit doConstructVisit(IRequestCycle cycle, String username,String password);
          }

           
          系原創(chuàng)
          其他blog地址  http://oxyc.spaces.live.com/
                                   http://hiok.blog.sohu.com

          posted on 2008-03-24 15:26 bluesky 閱讀(3868) 評(píng)論(5)  編輯  收藏 所屬分類: 工作總結(jié)

          評(píng)論

          # re: How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl[未登錄](méi) 2008-09-11 20:30 kelly

          是不是不全啊!能不能也寫全一下!謝謝!

          # re: How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl 2008-09-12 21:05 bluesky

          # re: How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl[未登錄](méi) 2008-09-16 09:37 kelly

          ASM_VISIT,PAGE_LOGIN是什么東東?。縄Visit又是什么東東???IConstructVisit這個(gè)接口又有什么作用?。磕芊窠o予一個(gè)具體一點(diǎn)是列?。傃芯?!請(qǐng)賜教!非常感激!

          # re: How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl[未登錄](méi) 2008-09-16 09:52 kelly

          能否寫個(gè)簡(jiǎn)單的登陸的示例給我?非常感謝!

          # re: How to use cookies in tapestry4? my Tapestry4.1.5`LoginCookiesManagerImpl 2008-09-19 22:03 bluesky

          你需要自己擴(kuò)展org.apache.tapestry.html.BasePage基類并實(shí)現(xiàn) org.apache.tapestry.event.PageValidateListener
          void pageValidate(PageEvent event);方法內(nèi)通過(guò)
          org.apache.tapestry.services.CookieSource把用戶名和密碼從cookies中找回來(lái)然后調(diào)用登錄業(yè)務(wù)邏輯,然后繼續(xù)決定是否跳轉(zhuǎn)登錄Login還是繼續(xù)Render該P(yáng)age
          主站蜘蛛池模板: 呼伦贝尔市| 绍兴市| 大新县| 岳阳县| 化德县| 民丰县| 六枝特区| 长泰县| 洪雅县| 浦东新区| 克拉玛依市| 南溪县| 连南| 阜南县| 沐川县| 科技| 泉州市| 隆昌县| 个旧市| 海兴县| 汾阳市| 多伦县| 安新县| 定襄县| 区。| 赤水市| 大渡口区| 社旗县| 石门县| 阿拉善左旗| 得荣县| 金寨县| 田阳县| 乐清市| 秀山| 新余市| 宁远县| 永年县| 永城市| 江北区| 江达县|