简体   繁体   English

春季会议管理如何工作?

[英]How does session managment work in spring?

I can't really understand the concept of this. 我真的不明白这个概念。 Take a look what I have: 看看我有什么:

@PostMapping("/login")
public ModelAndView login( @ModelAttribute UserLoginDTO userDto, HttpSession session) {
    if (authenticateService.loginCheck(userDto.getUsername(), userDto.getPassword())) {
        session.setAttribute("sessionid",123);
        return new ModelAndView("redirect:/profile");
    } else {
        return new ModelAndView("signin","error","Invalid username or password combination, or the user does not exist.");
    }
}

I have set a sessionID to the session. 我已经为会话设置了sessionID。 When the user navigates around the website, how do I know that it is the same user? 当用户浏览网站时, 我怎么知道它是同一用户?

Do I have to store the sessionID on server side in a ConcurrentHashMap? 我是否必须将SessionID存储在服务器端的ConcurrentHashMap中? And when there is a page switch I should do this? 当有页面切换时,我应该这样做吗?

if (conHashMap[...] == session.getId()) {...}
else //redirect to login page 

Also on logout, do I just remove the element from the hashmap and call for session.invalidate()? 同样在注销时,我是否只是从哈希图中删除该元素并调用session.invalidate()?

Or is there a way of doing this without using hashmaps at all? 还是有一种完全不使用哈希图的方式?

You know the session is from the same user if the id is the same, yes. 如果ID相同,则您知道会话来自同一用户,是的。 You can eventually store informations on the session. 您最终可以在会话中存储信息。 Alternativelly, you can create session scoped beans : 或者,您可以创建会话范围的bean:

@Component
@Scope(value="session")
public class MyComponent {
    // ...
}

All you will store in this kind of objects are only accessible by one user. 您将存储在此类对象中的所有内容只能由一个用户访问。

Figured it out. 弄清楚了。

After invalidating, the browser will visit the site with a new session. 无效后,浏览器将通过新会话访问该站点。 The new session won't have the "sessionid" attribute bound to it. 新会话将不会绑定“ sessionid”属性。 This way, I could determine which session is a valid one, without using hashmaps. 这样,我可以确定哪个会话是有效的会话,而无需使用哈希图。

if (session.getAttribute("sessionid")==null){
        return new ModelAndView("signin","error","Session expired, please log in again.");

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

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