java - 在java中使用session作为登录模块有困难

标签 java session jakarta-ee

嗨.. stackoverflow 团队成员。

我正在尝试使用java中的HttpSession来设置一些值,例如userid,因此我可以使用该变量直到 session 保持存在。

我使用 spring3.0 进行请求映射。下面给出了我的登录检查代码

@RequestMapping(value = "/login/GetLoginCheck.action")
    public @ResponseBody
    Map<String, Object> loginCheck(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        List<Employee> emplist = null;
        try {

            GlobalController.session = request.getSession();
            if (!GlobalController.session.isNew()) {
                this.setUsername(request.getParameter("username"));
                this.setPassword(request.getParameter("password"));

                emplist = loginservice.getEmployee(this.getUsername(),this.getPassword());

                if(emplist.size()>0)
                {
                    for(Employee employee: emplist)
                    {
                        this.setEmployeeid(employee.getId());

                        synchronized (GlobalController.session) {
                              GlobalController.session.setAttribute("userid", employee.getId());
                            }
                    }

                }else
                {
                    return getModelMapError("The username or password you entered is incorrect.");
                }



            }else{
                System.out.println("already session created");
                System.out.println("SESSION ID ::"+GlobalController.session.getId());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        logingview = this.loginView(this.getUsername(),this.getPassword());
        return getMapUser(emplist,logingview);
    }

下面给出了我的 GlobalController 类代码

public class GlobalController implements HttpSessionListener{


    private static int totalActiveSessions = 0;
    public static HttpSession session = null;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        // TODO Auto-generated method stub
        synchronized (this) {
            totalActiveSessions++;
        }
        System.out.println("Session Created: " + event.getSession().getId());
        System.out.println("Total Sessions: " + totalActiveSessions);

    }
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        // TODO Auto-generated method stub
        synchronized (this) {
            totalActiveSessions--;
        }
        System.out.println("Session Destroyed: " + event.getSession().getId());
        System.out.println("Total Sessions: " + totalActiveSessions);
    }


}

当用户登录时,我的“/login/GetLoginCheck.action”将执行,并且我将属性 empoyeeId: 2 设置为 session 。

但是我在局域网中的另一台电脑上打开相同的网址,并使用另一个名为 empoyeeId: 1 的用户登录。

我遇到的问题是,当我看到日志或打开具有employeeId:1 的网格面板时,它会向我显示属于employeeId:2 的数据。

sessionId 也会被新登录的用户 session 覆盖。

请建议我一些方法,我可以尝试以正确的方式实现 session ,这样我就可以获得仅属于目标员工的数据和 session 。

最佳答案

  • 与类相关的静态变量,每个类(而不是每个实例)都有一个值。这就是为什么你有

Also the sessionId get override with the newly logged user session.

  • 每个 servlet 都是单例的,因此当两个用户同时尝试登录时可能会出现问题。

关于java - 在java中使用session作为登录模块有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780695/

相关文章:

java - 如何在 Java 中将 hashmap 的值转换为 String

java - 检查明确的许可?

java - Eclipse Swing 到 JavaC?

session - Paypal 重定向让我退出我的网站?

eclipse - 在 Eclipse 中导入 javax.enterprise.context

java - 无法找到要链接到的源外部参照

session - Redis 不删除 session key

java - 在spring mvc中正确使用session

java - 如何在 CDI 环境中管理 EntityManager 生命周期(使用 Tomcat)

java - 强制先执行 "WebFilter"