我目前是一名初出茅庐的 Java 开发人员,想进入 Vaadin 开发,目前正在尝试为我的应用程序实现用户会话登录。我已经阅读了有关使用 VaadinServlets 这样做的内容:https ://vaadin.com/docs/v10/flow/advanced/tutorial-application-lifecycle.html 。
在无情地挖掘 API 文档和示例代码之后,我仍然无法理解如何为登录到我的平台的特定用户实现用户会话。据我了解,我可以使用我在下面实现的内容来初始化我的用户会话。
但是我对应用程序的目标略有不同:
[用例]
1.用户使用他们的特定凭据登录。
2.Gets Redirected to a SecuredPage(这将创建一个用户会话存储用户的用户名并检索令牌?)
3. 2-3分钟不活动后,用户将被强制退出SecuredPage并且会话关闭?
@WebServlet(urlPatterns = "/*", name = "VaadinFlowServlet", asyncSupported = true)
@VaadinServletConfiguration(heartbeatInterval = 5, productionMode = false)
public class LoginServlet extends VaadinServlet implements SessionInitListener, SessionDestroyListener {
private static final Logger LOGGER = LoggerFactory.getLogger(LoginServlet.class);
// <Method> ::servletInitialized():: -> handles most of the servlet customization. (write my servlet customization under this function.
// ::getService():: -> returns a VaadinServletService type?
// ::addSessionInitListener(this):: -> An event listener that can be registered to a VaadinService to get an event -> when a new Vaadin service session is initialized for that service.
// ::addSessionDestroyListener(this):: -> A listener that gets notified when a Vaadin service session is no longer used.
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(this);
getService().addSessionDestroyListener(this);
}
// <Method> ::sessionInit:: -> Starts Session?
// <Parameter> ::SessionInitEvent:: -> Event gets fired when a new Vaadin service session is initialized for a Vaadin service.
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException{
// Do Session start stuff here
// Creates a Session?
LOGGER.info("session init() "
+ " Session-ID: " + event.getSession().getSession().getId()
+ " CSRF: " + event.getSession().getCsrfToken());
}
// <Method> ::sessionDestroy:: -> Stops Session?
// <Parameter> ::SessionDestroyEvent:: -> Event fired when a Vaadin service session is no longer in use.
@Override
public void sessionDestroy(SessionDestroyEvent event) {
// Do session end stuff here
LOGGER.info("session destory()");
}
}
1 所以我想知道是否有人可以帮助我更好地理解这件事?充分赞赏