I have placed a button in my login view (outside of the login form) which shows a facial recognition dialog. This part has now been implemented and I am at the stage where I can successfully recognise a face and link it to my user data table. My problem is that I’ve been trying to navigate off the login view to the “main view” of the application just based on the facial recognition and not relying on the username and password. I have implemented the function below which I thought would authenticate the Vaadin session but I’m not sure if:
The last line should not be needed, replace that with your typical UI.navigate() call or you can use Page::reload to ensure all parts of the UI get rerendered
Thanks for the quick reply. I tried your solution with UI.getCurrent().navigate(“”) but it just stays on the login screen. I’ve also tried reload(). How would I navigate to my root view?
If you used reload - did you check that the user is available afterwards? I expected a redirect in your code that once somebody that is logged-in is opening /login gets automatically redirected to the main screen
Did you try to instantiate the UsernamePasswordAuthenticationToken using the three args constructor (Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities)?
This constructor sets the authenticated flag to true.
Wow thanks Marco and Christian, your help has been phenomenal. Indeed calling
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userConfig, null, null);
did the trick. One question, as you can see I am passing null for both the credentials and authorities. Is this okay in my scenario? (I get all the necessary “user rights” using my own security methods for access to menus, screens and entities).
Wow thanks Christian and Marco, your help has been phenomenal. Indeed calling
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userConfig, null, null);
did the trick. One question, as you can see I am passing null for both the credentials and authorities. Is this okay in my scenario? (I get all the necessary “user rights” using my own security methods for access to menus, screens and entities).
We don’t know your setup to answer that with confidence, personally I would always pass at least one authority like USER… but I’m also using spring’s security where authorities are necessary
As Christian said. Passing null authorities ends up in an empty collection.
This will, of course, not work well with Vaadin AuthenticationContext utility methods or if you apply @RolesAllowed annotations on your views. But since you have custom logics, we can’t say whether there could be any issues or not.
I am using spring security at a basic level, but I “think?” I’m okay passing null because I have a UserConfig which overrides the authorities granted to the user as shown below:
public class UserConfig implements UserDetails {
private final User user;
public User getUser() {
return user;
}
public UserConfig(User user) {
this.user = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return user.getRoles().stream().map(r -> (GrantedAuthority) new SimpleGrantedAuthority(r.getRole().getName())).toList();
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}