Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to redirect to desired View #264

Open
rupeshsaxena opened this issue Jun 17, 2024 · 2 comments
Open

Not able to redirect to desired View #264

rupeshsaxena opened this issue Jun 17, 2024 · 2 comments

Comments

@rupeshsaxena
Copy link

rupeshsaxena commented Jun 17, 2024

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
Copy link

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.

@mshabarov
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants