2014-08-28 37 views
0

我想爲我的apache tapestry網站創建一個登錄功能,在登錄後,應該顯示已登錄用戶的電子郵件,而不是「登錄」和「註冊」按鈕,以及「註銷」按鈕。Apache Tapestry用戶登錄功能

任何人都可以請告訴如何實現這個最好的方式?

我似乎無法弄清楚我應該如何檢測用戶是否登錄,在前端部分,爲了顯示不同的菜單選項(我是新的掛毯)。

最好的問候,馬呂斯。

回答

2

認證(其中登錄是一個部分)是非常特定於應用程序的。你如何定義一個用戶(或者你是否稱之爲「客戶」)並不是框架的功能。

通常,您將有一個代表您的用戶的SessionStateObject。然後,您可以使用這樣的事情在你的佈局:

<t:if test="user"> 
    <t:logoutLink/> 
    <p:else> 
    <t:signInForm/> 
</t:if> 

再次,組件​​LogoutLink和SignInForm是爲你實現。

用戶可以從Java代碼公開爲:

@Property @sessionState(創建=假) 私人用戶的用戶;

這表示用戶字段鏈接到存儲在HTTP會話中的值;此外,當字段第一次被讀取時,用戶不會被創建;相反,您的SignInForm組件應該分配給它的用戶字段。

0

關於這個問題的一點點研究後,我發現了以下方法:

1)我創建了一個Authenticator接口

public interface Authenticator { 

Users getLoggedUser(); 
boolean isLoggedIn(); 
void login(String email, String password) throws AuthenticationException; 
void logout(); 
} 

2)還創建了實現,一個AuthenticatorImpl.java類接口

public class AuthenticatorImpl implements Authenticator { 

public static final String AUTH_TOKEN = "authToken"; 

@Inject 
private StartDAO dao; 

@Inject 
private Request request; 

public void login(String email, String password) throws AuthenticationException 
{ 

    Users user = dao.findUniqueWithNamedQuery("from Users u where u.Email = '" + email + "' and u.Password = '" + password + "'"); 

    if (user == null) { throw new AuthenticationException("The user doesn't exist"); } 

    request.getSession(true).setAttribute(AUTH_TOKEN, user); 
} 

public boolean isLoggedIn() 
{ 
    Session session = request.getSession(false); 
    if (session != null) { return session.getAttribute(AUTH_TOKEN) != null; } 
    return false; 
} 

public void logout() 
{ 
    Session session = request.getSession(false); 
    if (session != null) 
    { 
     session.setAttribute(AUTH_TOKEN, null); 
     session.invalidate(); 
    } 
} 

public Users getLoggedUser() 
{ 
    Users user = null; 

    if (isLoggedIn()) 
    { 
     user = (Users) request.getSession(true).getAttribute(AUTH_TOKEN); 
    } 
    return user; 
} 
} 

3)創建在AppModule.java類對應的結合

public static void bind(ServiceBinder binder) 
{ 
    binder.bind(StartDAO.class, StartDAOImpl.class); 
    binder.bind(Authenticator.class, AuthenticatorImpl.class); 
} 

4)和我Layout.java頁面上,我用它通過以下方式

@Property 
private Users user; 

@Inject 
private Authenticator authenticator; 

void setupRender() 
{ 
    if(authenticator.getLoggedUser().getAccountType().equals("Administrator")){ 
     administrator = authenticator.getLoggedUser(); 
    } 

    user = authenticator.getLoggedUser(); 

} 

Object onLogout(){ 
    authenticator.logout(); 
    return Login.class; 
} 

Layout.tml

<t:if test="user"> 
     <span class="navbar-right btn navbar-btn" style="color: white;"> 
      Welcome ${user.Name}! <a t:type="eventLink" t:event="Logout" href="#">(Logout)</a> 
     </span> 
    </t:if> 
    <t:if negate="true" test="user"> 
     <span class="navbar-right"> 
       <t:pagelink page="user/create" class="btn btn-default navbar-btn">Register</t:pagelink> 
       <t:pagelink page="user/login" class="btn btn-default navbar-btn">Sign in</t:pagelink> 
     </span> 
    </t:if> 

這個工作對我來說沒有任何問題。希望它能幫助別人。

最好的問候,馬呂斯。