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实现org.apache.tapestry.event.PageValidateListener方法,判断asm对象Visit是否已经登陆
如没visit==null,去cookies找回用户名和密码从cycle中获得Login页面
Login页面实现common.tapestry.IConstructVisit接口。LoginPage登陆时保存用户名密码到cookies中,并对cookies的值加密,
cookies的key是baseUrl+key,取回时对cookies值解密,加密解密的password放到Application的meta信息中
实现代码如下
抽象接口LoginCookiesManager
package common.tapestry.engine;
public interface LoginCookiesManager {
String APPLICATION_META_KEY_DEFAULT_COOKIES_IS_ENCRYPTION = "common.tapestry.default-cookies-encrypt";//是否需要加密,默认加密
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 实现
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;
}
/**
* 判断用户是否已经登陆
*
* @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类必须实现如下接口
package common.tapestry;
import org.apache.tapestry.IRequestCycle;
public interface IConstructVisit {
public IVisit doConstructVisit(IRequestCycle cycle, String username,String password);
}
系原创
其他blog地址 http://oxyc.spaces.live.com/
http://hiok.blog.sohu.com