使用shiro,登出的时候报500,sessionid找不到

tomcat错误日志:org.apache.shiro.session.UnknownSessionException: There is no session with id
找了好多解决方法,都不靠谱
后来偶尔看到说先重定向然后再使用 subject 的 logout,试了一试果然不知道为什么就好了
贴上修改后的代码

    @RequestMapping(value = "/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        // 先重定向
        try {
            response.sendRedirect(request.getContextPath() + "/login.htm");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 然后再退出登录,避免sessionid找不到
            Subject subject = SecurityUtils.getSubject();
            if (subject.isAuthenticated()) {
                UserUtils.clearCache();
                subject.logout();
            }
        }
    }

你可能感兴趣的:(java,spring,shiro)