java.lang.IllegalStateException: getAttribute: Session already invalidated
程序员文章站
2024-02-28 19:43:10
...
错误代码:
java.lang.IllegalStateException: getAttribute: Session already invalidated
at org.apache.catalina.session.StandardSession.getAttribute(StandardSession.java:1137) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.catalina.session.StandardSessionFacade.getAttribute(StandardSessionFacade.java:102) ~[tomcat-embed-core-9.0.41.jar:9.0.41]
at org.apache.shiro.web.session.HttpServletSession.getAttribute(HttpServletSession.java:146) ~[shiro-web-1.4.1.jar:1.4.1]
at org.apache.shiro.session.ProxiedSession.getAttribute(ProxiedSession.java:121) ~[shiro-core-1.4.1.jar:1.4.1]
at org.apache.shiro.subject.support.DelegatingSubject.getRunAsPrincipalsStack(DelegatingSubject.java:473) ~[shiro-core-1.4.1.jar:1.4.1]
at org.apache.shiro.subject.support.DelegatingSubject.getPrincipals(DelegatingSubject.java:157) ~[shiro-core-1.4.1.jar:1.4.1]
at org.apache.shiro.subject.support.DelegatingSubject.getPrincipal(DelegatingSubject.java:153) ~[shiro-core-1.4.1.jar:1.4.1]
at org.apache.shiro.web.servlet.ShiroHttpServletRequest.getSubjectPrincipal(ShiroHttpServletRequest.java:96) ~[shiro-web-1.4.1.jar:1.4.1]
at org.apache.shiro.web.servlet.ShiroHttpServletRequest.getUserPrincipal(ShiroHttpServletRequest.java:112) ~[shiro-web-1.4.1.jar:1.4.1]
at org.springframework.web.servlet.FrameworkServlet.getUsernameForRequest(FrameworkServlet.java:1160) ~[spring-webmvc-5.3.2.jar:5.3.2]
at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1145) ~[spring-webmvc-5.3.2.jar:5.3.2]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1023) ~
后台代码:
//退出
@RequestMapping("logout")
public String logout(HttpSession session){
session.invalidate();//清空session
return "system/index/login";
}
报错原因:因为执行了session().invalidate()这句代码,致使session销毁。
解决方案:
@RequestMapping("logout")
public String logout(HttpServletRequest request){
// 清除session
Enumeration<String> attributeNames = request.getSession().getAttributeNames();
while (attributeNames.hasMoreElements()) {
String key = attributeNames.nextElement().toString();
request.getSession().removeAttribute(key);
}
return "system/index/login";
}