Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache Tapestry用户登录功能_Java_Apache_Authentication_Tapestry_User Management - Fatal编程技术网

Java Apache Tapestry用户登录功能

Java Apache Tapestry用户登录功能,java,apache,authentication,tapestry,user-management,Java,Apache,Authentication,Tapestry,User Management,我正在尝试为我的apachetapestry网站创建一个登录功能,在登录后,应该显示登录用户的电子邮件以及“注销”按钮,而不是“登录”和“注册”按钮 有谁能告诉我如何以最好的方式实现这一点 我似乎不知道如何在前端部分检测用户是否已登录,以便显示不同的菜单选项(我是tapestry新手) 向您致意,马吕斯。身份验证(登录是其中的一部分)非常特定于应用程序。您如何定义用户(例如,您是否将其称为“客户”)不是框架所做的事情 通常,您将有一个SessionStateObject代表您的用户。然后,您可以

我正在尝试为我的
apachetapestry
网站创建一个登录功能,在登录后,应该显示登录用户的电子邮件以及“注销”按钮,而不是“登录”和“注册”按钮

有谁能告诉我如何以最好的方式实现这一点

我似乎不知道如何在前端部分检测用户是否已登录,以便显示不同的菜单选项(我是tapestry新手)

向您致意,马吕斯。

身份验证(登录是其中的一部分)非常特定于应用程序。您如何定义用户(例如,您是否将其称为“客户”)不是框架所做的事情

通常,您将有一个SessionStateObject代表您的用户。然后,您可以在布局中使用以下内容:

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

同样,LogoutLink和SignInForm组件是供您实现的

用户可能会从Java代码中暴露为:

@财产 @会话状态(create=false) 私人用户


这表示用户字段链接到HTTP会话中存储的值;此外,当第一次读取字段时将不创建用户;相反,您的SignInfo组件应该分配给它的用户字段。

在对这个问题进行了一些研究之后,我发现了以下方法:

1) 我创建了一个验证器接口

public interface Authenticator {

Users getLoggedUser();
boolean isLoggedIn();
void login(String email, String password) throws AuthenticationException;
void logout();
}
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;
}
}
2) 还创建了一个实现该接口的AuthenticatorImpl.java类

public interface Authenticator {

Users getLoggedUser();
boolean isLoggedIn();
void login(String email, String password) throws AuthenticationException;
void logout();
}
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>

欢迎${user.Name}!
登记
登录
这对我来说没有任何问题。希望它能帮助别人

向你问好,马吕斯