flow-crm-tutorial icon indicating copy to clipboard operation
flow-crm-tutorial copied to clipboard

Not able to redirect to desired View

Open rupeshsaxena opened this issue 1 year ago • 2 comments
trafficstars

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);
        }
    }
}

rupeshsaxena avatar Jun 17 '24 20:06 rupeshsaxena

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.

TatuLund avatar Jun 18 '24 05:06 TatuLund

Do you have any access annotation on top of your "home" view class? E.g. @PermitAll or others ? Redirecting to login view may mean that you don't have an access to this view and, thus, Vaadin redirects back to login view.

mshabarov avatar Sep 03 '24 10:09 mshabarov