这LoginForm
对我有用。您的问题可能在您在此处显示的代码之外。我们需要知道您是如何实例化和显示LoginForm
.
示例应用
这是一个使用 Vaadin 14.1.2 的示例应用程序。我使用了由Vaadin starter page生成的新“Plain Java Servlet”风格项目。我没有修改 POM 文件。我使用捆绑的 Jetty 服务器运行该应用程序,并使用适用于 macOS 的 Google Chrome 版本 79.0.3945.79(官方构建)(64 位)。
我创建了一个简单的User
类。
package work.basil.example;
import java.util.Objects;
import java.util.UUID;
public class User
{
private UUID id;
private String username;
public User ( UUID id , String username )
{
this.id = Objects.requireNonNull( id );
this.username = Objects.requireNonNull( username );
}
// ------------| Object |---------------------------------
@Override
public boolean equals ( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
User user = ( User ) o;
return id.equals( user.id );
}
@Override
public int hashCode ( )
{
return Objects.hash( id );
}
@Override
public String toString ( )
{
return "User{ " +
"id=" + id +
" | username='" + username + '\'' +
" }";
}
}
和一Authenticator
堂课。对于这个简单的演示案例,注释掉或始终通过用户或始终拒绝用户的行。
package work.basil.example;
import java.util.Optional;
import java.util.UUID;
public class Authenticator
{
Optional < User > authenticate ( String username , String password )
{
User user = new User( UUID.fromString( "76317e14-20a5-11ea-978f-2e728ce88125" ) , username );
return Optional.of( user );
// return Optional.empty();
}
}
用户成功通过身份验证后显示的虚拟内容视图。
package work.basil.example;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
public class ContentView extends VerticalLayout
{
public ContentView ( )
{
this.add( new H1( "Content goes here" ) );
}
}
MainView
将它们与此布局结合在一起。
package work.basil.example;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.login.AbstractLogin;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.server.VaadinSession;
import java.util.Objects;
import java.util.Optional;
/**
* The main view contains a button and a click listener.
*/
@Route ( "" )
@PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
public class MainView extends VerticalLayout
{
public MainView ( )
{
this.display();
}
private void display ( )
{
if ( Objects.isNull( VaadinSession.getCurrent().getAttribute( User.class ) ) )
{
this.removeAll();
this.add( this.makeLoginForm() );
} else
{ // Else we have a User, so must be authenticated.
this.removeAll();
this.add( new ContentView() );
}
}
private LoginForm makeLoginForm ( )
{
Authenticator authenticator = new Authenticator();
LoginForm component = new LoginForm();
component.addLoginListener( ( AbstractLogin.LoginEvent loginEvent ) -> {
Optional < User > user = authenticator.authenticate( loginEvent.getUsername() , loginEvent.getPassword() );
if ( user.isPresent() )
{
VaadinSession.getCurrent().setAttribute( User.class , user.get() );
this.display();
} else
{
component.setError( true );
}
} );
return component;
}
}
对于“退出”功能,您的代码只需User
从会话中删除对象,然后MainView::display
再次调用。