content: package cn.abel.rest.shiro;
  
  import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
  import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
  import org.apache.shiro.web.util.WebUtils;
  import org.springframework.util.StringUtils;
  
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import java.io.Serializable;
  
  public class HttpHeaderSessionManager extends DefaultWebSessionManager {
      public static final String LOGIN_TOKEN_KEY = "sid";
      private static final String REFERENCED_SESSION_ID_SOURCE = "header";
  
      @Override
      protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
          String id = WebUtils.toHttp(request).getHeader(LOGIN_TOKEN_KEY);
          if (!StringUtils.isEmpty(id)) {
              request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, REFERENCED_SESSION_ID_SOURCE);
              request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
              request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
              return id;
          }
          return null;
      }
  }
  