vaadin / flow-crm-tutorial

Demo app for the Java Web App tutorial series
The Unlicense
170 stars 183 forks source link

Not able to redirect to desired View #264

Open rupeshsaxena opened 2 weeks ago

rupeshsaxena commented 2 weeks ago

I tried as per the tutorial instructions on how to embed LoginForm in custom LoginView, despite all the configurations I set, but I am not able to redirect to the desired view , in my case it was serving on route => "home", instead it returns back to the login view.

My Security Config file -

@EnableWebSecurity`
@Configuration
public class SecurityConfig extends VaadinWebSecurity {

    @Autowired
    private GetUserUseCase userUseCase;

    @Bean
    public UserDetailsService users() {
        List<Users> validUsers = userUseCase.getUsers();
        Collection<UserDetails> userDetails = new ArrayList<>();
        validUsers.forEach((posUser) -> {
            String username = String.valueOf(posUser.getUserId());
            String password = posUser.getUserPass();
            String role = posUser.getUserType().getPName();

            UserDetails user = User.builder()
                    .username(username)
                    .password(password)
                    .roles(role)
                    .build();
            userDetails.add(user);
        });
        return new InMemoryUserDetailsManager(userDetails);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        setLoginView(http, LoginView.class);
    }

    @Override
    protected void configure(WebSecurity web) throws Exception {
        super.configure(web);
        web.ignoring()
                .requestMatchers("/**")
                .requestMatchers("/VAADIN")
                .requestMatchers("/VAADIN/**");
    }
}

My LoginView

@Route("login")
@PageTitle("CloudPOS")
@AnonymousAllowed
public class LoginView extends VerticalLayout implements BeforeEnterObserver {

    private final LoginForm login = new LoginForm();
    private final AuthService authService;

    public LoginView(@Autowired AuthService authService) {
        this.authService = authService;
        addClassName("login-view");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);

        login.setAction("login");
        login.addLoginListener(this::authenticateAndRedirect);

        add(new H1("CloudPOS"), login);
    }

    private void authenticateAndRedirect(AbstractLogin.LoginEvent event) {
        boolean isAuthenticated = authService.isAuthenticated(event.getUsername(), event.getPassword());
        if (isAuthenticated) {
            UI.getCurrent().navigate(PageRoutes.HOME_VIEW_ROUTE); // should navigate to "/home" after login success
        } else {
            login.setError(true);
        }
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        if (
                beforeEnterEvent.getLocation()
                        .getQueryParameters()
                        .getParameters()
                        .containsKey("error")) {
            login.setError(true);
        }
    }
}
TatuLund commented 2 weeks ago

VaadinWebSecurity is caching the request with SpringSecurity and upon successful login will redirect to location based on cached request. Which means that in normal circumstances redirect you are doing in your code is not required.