Shiro:会话已失效

发布于 2024-12-27 09:49:49 字数 1260 浏览 1 评论 0原文

我在 Web 应用程序中使用 Apache Shiro。登录和身份验证检查工作正常,但我在实现注销/重新登录机制时遇到问题:注销是在 servlet 中完成的:

    private void logout(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
            log.debug("do logout");
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            resp.sendRedirect("end.html");
    }

但是在注销并重新登录后,我收到以下错误:

org.apache.shiro.session.InvalidSessionException: java.lang.IllegalStateException:
  getAttribute: Session already invalidated
  at org.apache.shiro.web.session.HttpServletSession.removeAttribute(HttpServletSession.java:167)
at org.apache.shiro.session.ProxiedSession.removeAttribute(ProxiedSession.java:135)
at org.apache.shiro.subject.support.DelegatingSubject.clearRunAsIdentities(DelegatingSubject.java:424)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:246)

登录完成于以下方式(在 UI 组件的方法中,我使用 ZK 作为 UI 框架):

  private void tryLogin(UsernamePasswordToken token) {
        Subject subject = SecurityUtils.getSubject();
        try {
              subject.login(token);
              ...

我不理解异常,因为从 shiro 注销会使会话无效,重新登录应该访问新会话。

I am using Apache Shiro in a web-application. The login and authentication check works well, but I have a problem to implement a logout / re-login mechanism: The logout is done in a servlet:

    private void logout(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
            log.debug("do logout");
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            resp.sendRedirect("end.html");
    }

But after a logout and re-login I get the following error:

org.apache.shiro.session.InvalidSessionException: java.lang.IllegalStateException:
  getAttribute: Session already invalidated
  at org.apache.shiro.web.session.HttpServletSession.removeAttribute(HttpServletSession.java:167)
at org.apache.shiro.session.ProxiedSession.removeAttribute(ProxiedSession.java:135)
at org.apache.shiro.subject.support.DelegatingSubject.clearRunAsIdentities(DelegatingSubject.java:424)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:246)

The login is done in the following way (in a method of a UI component, I use ZK as UI framework):

  private void tryLogin(UsernamePasswordToken token) {
        Subject subject = SecurityUtils.getSubject();
        try {
              subject.login(token);
              ...

I do not understand the exception as the logout from shiro invalidates the session and the re-login should access a new session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耶耶耶 2025-01-03 09:49:49

在 1.2 版本之前的 Shiro 中,如果有人(或其他东西)在调用 Subject.logout() 之前使会话无效(例如 httpSession.invalidate() ,然后 <代码>subject.logout())。

这已作为 SHIRO-298 中的错误提出,并且已经在 1.2.0-SNAPSHOT 版本中已解决。您可以使用当前快照版本之一,也可以在 Shiro 1.2.0 发布后使用它。

This will occur in Shiro before version 1.2 if someone (or something else) invalidates the session before Subject.logout() is invoked (e.g. httpSession.invalidate() and then subject.logout()).

This has been raised as a bug in SHIRO-298 and it has already been resolved in 1.2.0-SNAPSHOT builds. You can use one of the current snapshot builds or use Shiro 1.2.0 when it is released.

神爱温柔 2025-01-03 09:49:49

看起来您的 UI 框架在注销后没有重新生成会话。
您可以尝试在登录调用之前调用 subject.getSession() 强制创建新会话。像这样的东西:

private void tryLogin(UsernamePasswordToken token) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        try {
              subject.login(token);

it looks like your UI framework is not regenerating the session after logout.
You can try to force a new the session calling subject.getSession() just before the login call. Something like this:

private void tryLogin(UsernamePasswordToken token) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        try {
              subject.login(token);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文