ShiroHttpSession类

ShiroHttpSession类主要定义了,它实现了HttpSession接口,现对其解析如下:

1.HttpSession接口

HttpSession接口主要定义了对于session的最后一次访问时间,获取创建时间,获取Id,获取servlet上下文环境,最大的超时时间,获取属性等操作。

2.ShiroHttpSession类

2.1.数据属性

public static final String DEFAULT_SESSION_ID_NAME = "JSESSIONID";//默认的sessionId名称

private static final Enumeration EMPTY_ENUMERATION = new Enumeration() {
        public boolean hasMoreElements() {
            return false;
        }

        public Object nextElement() {
            return null;
        }
    };//空枚举类型

private static final javax.servlet.http.HttpSessionContext HTTP_SESSION_CONTEXT =
            new javax.servlet.http.HttpSessionContext() {
                public HttpSession getSession(String s) {
                    return null;
                }

                public Enumeration getIds() {
                    return EMPTY_ENUMERATION;
                }
            };//session上下文

protected ServletContext servletContext = null;//servlet上下文
protected HttpServletRequest currentRequest = null;//当前的request
protected Session session = null; //session信息

2.2.构造方法

public ShiroHttpSession(Session session, HttpServletRequest currentRequest, ServletContext servletContext) {
        if (session instanceof HttpServletSession) {
            String msg = "Session constructor argument cannot be an instance of HttpServletSession.  This is enforced to " +
                    "prevent circular dependencies and infinite loops.";
            throw new IllegalArgumentException(msg);
        }
        this.session = session;
        this.currentRequest = currentRequest;
        this.servletContext = servletContext;
    }

2.3.获取session信息

public Session getSession() {
        return this.session;
}

2.4.获取创建时间(它实现了HttpSession接口的方法)

public long getCreationTime() {
        try {
            return getSession().getStartTimestamp().getTime();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
}

2.5.获取id信息(它实现了HttpSession接口的方法)

public String getId() {
        return getSession().getId().toString();
}

2.6.获取最后一次访问时间(它实现了HttpSession接口的方法)

public long getLastAccessedTime() {
        return getSession().getLastAccessTime().getTime();
}

2.7.获取servlet上下文路径(它实现了HttpSession接口的方法)

public ServletContext getServletContext() {
        return this.servletContext;
}

2.8.设置session的超时时间(它实现了HttpSession接口的方法)

public void setMaxInactiveInterval(int i) {
        try {
            getSession().setTimeout(i * 1000);
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
}

2.9.获取最大的超时时间(它实现了HttpSession接口的方法)

public int getMaxInactiveInterval() {
        try {
            return (new Long(getSession().getTimeout() / 1000)).intValue();
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
    }

2.10.获取会话上下文(它实现了HttpSession接口的方法)

public javax.servlet.http.HttpSessionContext getSessionContext() {
        return HTTP_SESSION_CONTEXT;
}

2.11.根据属性名获取属性值信息(它实现了HttpSession接口的方法)

public Object getAttribute(String s) {
        try {
            return getSession().getAttribute(s);
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
    }

2.12.获取属性信息(它实现了HttpSession接口的方法)

public Object getValue(String s) {
        return getAttribute(s);
    }

2.13.获取session所有的属性键集合(它实现了HttpSession接口的方法)

protected Set<String> getKeyNames() {
        Collection<Object> keySet;
        try {
            keySet = getSession().getAttributeKeys();
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
        Set<String> keyNames;
        if (keySet != null && !keySet.isEmpty()) {
            keyNames = new HashSet<String>(keySet.size());
            for (Object o : keySet) {
                keyNames.add(o.toString());
            }
        } else {
            keyNames = Collections.EMPTY_SET;
        }
        return keyNames;
    }

2.14.获取session所有的属性名称(枚举类型,它实现了HttpSession接口的方法)

public Enumeration getAttributeNames() {
        Set<String> keyNames = getKeyNames();
        final Iterator iterator = keyNames.iterator();
        return new Enumeration() {
            public boolean hasMoreElements() {
                return iterator.hasNext();
            }

            public Object nextElement() {
                return iterator.next();
            }
        };
    }

2.15.获取所有的属性名称(转换成数组类型,它实现了HttpSession接口的方法)

public String[] getValueNames() {
        Set<String> keyNames = getKeyNames();
        String[] array = new String[keyNames.size()];
        if (keyNames.size() > 0) {
            array = keyNames.toArray(array);
        }
        return array;
    }

2.16.在监听器上绑定事件

protected void afterBound(String s, Object o) {
        if (o instanceof HttpSessionBindingListener) {
            HttpSessionBindingListener listener = (HttpSessionBindingListener) o;
            HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, s, o);
            listener.valueBound(event);
        }
    }

2.17.在监听器上绑定事件

protected void afterUnbound(String s, Object o) {
        if (o instanceof HttpSessionBindingListener) {
            HttpSessionBindingListener listener = (HttpSessionBindingListener) o;
            HttpSessionBindingEvent event = new HttpSessionBindingEvent(this, s, o);
            listener.valueUnbound(event);
        }
    }

2.18.在session中设置值(如果object为监听器,并将事件绑定到监听器上,它实现了HttpSession接口的方法)

public void setAttribute(String s, Object o) {
        try {
            getSession().setAttribute(s, o);
            afterBound(s, o);
        } catch (InvalidSessionException e) {
            //noinspection finally
            try {
                afterUnbound(s, o);
            } finally {
                //noinspection ThrowFromFinallyBlock
                throw new IllegalStateException(e);
            }
        }
    }

2.19.在session中设置属性(它实现了HttpSession接口的方法)

public void putValue(String s, Object o) {
        setAttribute(s, o);
    }

2.20.从session中移除属性信息(如果属性值是监听器,则从监听器中移除监听事件,它实现了HttpSession接口的方法)

public void removeAttribute(String s) {
        try {
            Object attribute = getSession().removeAttribute(s);
            afterUnbound(s, attribute);
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
    }

2.21.从session中移除属性信息(它实现了HttpSession接口的方法)

public void removeValue(String s) {
        removeAttribute(s);
}

2.22.暂停session(它实现了HttpSession接口的方法)

public void invalidate() {
        try {
            getSession().stop();
        } catch (InvalidSessionException e) {
            throw new IllegalStateException(e);
        }
    }
2.23.session是否是新建的(它实现了HttpSession接口的方法)

public boolean isNew() {
        Boolean value = (Boolean) currentRequest.getAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_IS_NEW);
        return value != null && value.equals(Boolean.TRUE);
    }

猜你喜欢

转载自yansxjl.iteye.com/blog/2335880