java - Spring ModelAttribute 注销后显示旧值

标签 java spring session modelattribute

我正在使用 Spring 开发一个应用程序。我在登录和注销方面遇到了麻烦。我使用登录凭据(例如用户名:john,密码:doe)登录应用程序,然后转到管理页面,然后从应用程序注销。但这次我使用了不同的登录凭据(例如用户名:jack,密码:white)进行登录。当我进入管理页面并调试我的应用程序时,AdminController 处的 @ModelAttribute(value = "myUser") 用户登录用户 显示旧用户值。我不明白为什么会发生这种情况。有人可以帮忙吗?

我的源代码如下:

@Controller
@RequestMapping("/LoginController")
@SessionAttributes({"myUser"})
public class LoginController 
{
    private static String LOGIN_URL         = "login/login_";
    private static String INDEX_URL         = "main/index";

    @Autowired
    private IUserService userService    = null;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam(value="userName", required=false) String argUserName, @RequestParam(value="password", required=false) String argPassword, HttpServletRequest req)
    {

        ModelAndView modelAndView = new ModelAndView();

        // Assume argUserName and argPassword not null
        User loginUser = this.userService.getUser(argUserName, argPassword);

        HttpSession ses = req.getSession();

        // Assume loginUser not null
        ses.setAttribute("myUser", loginUser);

        modelAndView.setViewName(LoginController.INDEX_URL);

        return modelAndView;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest argReq, HttpServletResponse argResp) throws ServletException, IOException
    {
        HttpSession session = argReq.getSession(false);

        Enumeration<?> attributeNames = session.getAttributeNames();
        while(attributeNames.hasMoreElements())
        {
            String attrName = (String)attributeNames.nextElement();

            if(session.getAttribute(attrName) != null)
            {
                session.setAttribute(attrName,null);
                //session.removeAttribute(attrName);
                attributeNames = session.getAttributeNames();
            }
        }
        // close session
        session.invalidate();

        return LoginController.LOGIN_URL;
    }
}

管理 Controller

@Controller
@RequestMapping("/AdminController")
@SessionAttributes({"myUser"})
public class AdminController 
{
    private static String SETTINGS_PAGE = "settings/index";

    @RequestMapping("/index")
    public ModelAndView index(@ModelAttribute(value = "myUser") User loggedInUser, HttpSession ses)
    {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("loggedInUserId", loggedInUser.getUserID());
        map.put("userName", loggedInUser.getUserName());

        modelAndView.addAllObjects(map);

        modelAndView.setViewName(AdminController.SETTINGS_PAGE);
        return modelAndView;
    }

}

最佳答案

删除该注释

@SessionAttributes({"myUser"})

关于java - Spring ModelAttribute 注销后显示旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18569314/

相关文章:

c - 在单独的 session 中运行应用程序?

java.sql.SQLException : ORA-24335: cannot support more than 1000 columns 异常

java - JSON 对象不正确并导致 HttpException

java - 如何在嵌入式 Tomcat 8 中配置 JDBC 资源?

spring - 自定义 Cassandra 转换器

spring - 如何使用 Spring 连接需要身份验证的 MongoDB

php - 在 CodeIgniter 中处理并发请求

java - 使用 Docker 的 Java 开发工作流程/反馈循环缓慢 : do I need to rebuild the JAR and image after every change?

java.lang.NoClassDefFoundError : org/springframework/core/ResolvableTypeProvider- Spring Batch

PHP:删除给定ID的另一个 session