在Shiro注销期间销毁SessionScoped CDI bean

问题是会话作用域bean在达到会话超时之前不会被销毁。

因此,我有两个有关以下注销程序的问题:

  • 这是使用shiro注销的正确方法(请参阅下面的注销())
  • 在注销期间销毁CDI会话范围的bean的正确方法是什么?
  • page.xhtml:

    <p:commandLink ajax="false" actionListener="#{myOtherBean.logout}" />
    

    豆:

    @Named
    @SessionScoped
    public class mySessionBean implements Serializable {
    }
    
    @Named
    @SessionScoped
    public class myOtherBean extends Observable implements Serializable {
        @Inject
        private Subject subject;
    
        public void logout(){
    
          subject.logout();
    
    // this line throws the exception
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    
          FacesContext.getCurrentInstance().getExternalContext()
                .redirect(servlet.getContextPath() + "/logout");
        }
    }
    

    shiro.ini:

    [main]
    sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
    securityManager.sessionManager = $sessionManager
    sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
    securityManager.sessionManager.sessionDAO = $sessionDAO
    ....
    logout=org.apache.shiro.web.filter.authc.LogoutFilter
    logout.redirectUrl = /login.xhtml
    
    ....
    [urls]
    /logout = logout
    

    例外:

    当我调用FacesContext.getCurrentInstance().getExternalContext().invalidateSession();时,会引发以下异常FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

     java.lang.IllegalStateException:
     org.apache.shiro.session.UnknownSessionException:
     There is no session with id [e5939658-c033-4e67-984f-23cadfbc06fb]
    

    其他信息:我正在运行Wildfly 8.2.0.Final。

    谢谢。


    这是我在我的项目中使用的代码,可能是因为你的bean是SessionScoped,而我的ViewScoped是?

    @Named
    @ViewScoped
    public class Authenticator implements Serializable {
    
        public void logout() {
            SecurityUtils.getSubject().logout();
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            FacesContext.getCurrentInstance().getExternalContext().redirect(LOGIN_URL);
        }
    }
    
    链接地址: http://www.djcxy.com/p/49119.html

    上一篇: Destroy SessionScoped CDI beans during Shiro logout

    下一篇: Password Encryption Algorithm in Glassfish 4