如何知道会话已过期?

时间:2016-11-03 07:52:16

标签: spring spring-mvc spring-4

登录成功后,我在控制器方法中将值设置为session对象:

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ModelAndView processLogin(Model model, HttpServletRequest request, HttpSession session, @RequestParam String login, @RequestParam String pwd) {

        if ( utilisateurDao.verifierLoginUser(login) ) {

            if ( utilisateurDao.verifierUser(login, pwd) ) {

                HashMap<String, String> criteres = new HashMap<String, String>();
                criteres.put("user_login", login);
                criteres.put("user_passwd", pwd);
                List<Utilisateur> users = utilisateurDao.lireParCritere(criteres);
                session.setAttribute("user_code", ((Utilisateur)users.get(0)).getUser_code());

                session.setAttribute("menu", menuDao.afficherMenuParUtilisateur((Integer)session.getAttribute("user_code"), env, request, session));

                criteres.clear();
                users.clear();

                criteres.put("user_code", String.valueOf(session.getAttribute("user_code")));
                users = utilisateurDao.lireParCritere(criteres);
                session.setAttribute("user_names", ((Utilisateur)users.get(0)).getNoms());

                session.setAttribute("logout_menu", env.getProperty("menu.logout"));

                return new ModelAndView("redirect:/accueil");

            } else {

                ModelAndView modelViewLogin = new ModelAndView("redirect:/");

                modelViewLogin.addObject("e", "p").addObject("l", login);

                return modelViewLogin;

            }

        } else {

            ModelAndView modelViewLogin = new ModelAndView("redirect:/");

            modelViewLogin.addObject("e", "l");

            return modelViewLogin;

        }

    }

然后我打开了应用程序非活动状态几分钟。之后我去了“accueil”路。然后菜单不再显示了!菜单来自session。那么如何知道session已过期以及测试它的方便位置在哪里?

3 个答案:

答案 0 :(得分:3)

默认情况下,spring安全会话存储在SessionRegistry

使用SecurityContext,您可以在控制器代码中获取此信息。

  SecurityContext context = SecurityContextHolder.getContext();
  Authentication authentication = context.getAuthentication();

如果您希望在会话过期或注销时收到通知,您可以随时在SessionDestroyedEvent - documentation注册听众。

示例:

    @Component
    public class LogoutListener implements ApplicationListener<SessionDestroyedEvent> {

        @Override
        public void onApplicationEvent(SessionDestroyedEvent event) {
            //do your stuff here
        }
    }

对于该主题,也值得参考spring docs

答案 1 :(得分:0)

你可以制作拦截器,

@Component
public class RequestInterceptor extends HandlerInterceptorAdapter

在这个拦截器中,您可以控制HttpServletRequest 并检查obj是否存在于它们中然后你可以抛出一个新的SessionExpiredException并捕获@ExceptionMapper(https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    if (request.getSession().getAttribute("user")==null) {
        throw new SessionExpiredException();
   }
        return true;
    }

答案 2 :(得分:0)

我检查如下。我认为这可能会有所帮助。

public boolean isUserLoggedIn(HttpServletRequest request) throws IOException {
    SecurityContext securityContext = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
    if(securityContext != null) {
        Authentication authentication =  securityContext.getAuthentication();
        if(null != authentication && authentication.isAuthenticated() != true) 
            return false;
        else 
            return true;
    } else {
        return false;
    }
}