简体   繁体   English

使用spring登录vaadin后如何执行代码

[英]How to execute code after vaadin login with spring

The user is setting the language and then login from a vaadin login view.用户正在设置语言,然后从 vaadin 登录视图登录。 I need the save the language the user set in a database.我需要将用户设置的语言保存在数据库中。

I tryed using the addLoginListener function of the vaadin LoginForm.我尝试使用 vaadin LoginForm 的 addLoginListener function。 The problem with this is that it is triggered before the authentication finished.这个问题是它在身份验证完成之前触发。 So someone could change the language of someone who he know the username of.所以有人可以更改他知道用户名的人的语言。

I also tryed Spring Authentication Events .我还尝试了 Spring 身份验证事件 The Problem with this is that i dont have access to the vaadin session wher I saved the language that should be written in the database.这个问题是我无法访问 vaadin session 我保存了应该在数据库中编写的语言。

You can find the VaadinSession you want in the authentication success handler using the current request:您可以使用当前请求在身份验证成功处理程序中找到您想要的VaadinSession

public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
​
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        Optional<VaadinSession> maybeSession = VaadinSession.getAllSessions(
                request.getSession()).stream().findFirst();
​
        maybeSession.ifPresent(session -> {
            session.accessSynchronously(/* do something with session */);
        });
​
        super.onAuthenticationSuccess(request, response, authentication);
    }
​
}

This only works if you already have an existing VaadinSession ;这仅在您已经有一个现有的VaadinSession if your login mechanism uses something else than a Vaadin UI (such as a SSO), then the session doesn't exist yet and things become a bit more complicated.如果您的登录机制使用的不是 Vaadin UI(例如 SSO),那么 session 还不存在,事情会变得有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM