shiro——Session 系列源码

目录

 

1 shiro 会话的类图

1.1 Session 接口

2. DelegatingSession 类(一种类型的会话类)

3. ProxiedSession 类(一种类型的会话类)

4. SimpleSession类 (常用的 会话类)

5. StoppingAwareProxiedSession 类(很重要的会话类)


1 shiro 会话的类图

1.1 Session 接口

public interface Session {
    Serializable getId(); //获取会话Id
    Date getStartTimestamp(); //获取会话创建时间
    Date getLastAccessTime(); //获取上一次访问会话的时间
    long getTimeout() throws InvalidSessionException; //获取会话过期时间
    void setTimeout(long var1) throws InvalidSessionException; //设置会话的过期时间
    String getHost(); //获取此会话用户的主机地址
    void touch() throws InvalidSessionException; //访问一次会话,并不做任何
    void stop() throws InvalidSessionException; //停止会话,就是删除
    Collection<Object> getAttributeKeys() throws InvalidSessionException; //会话中是可以存储一些key-value的,此方法获取这些key
    Object getAttribute(Object var1) throws InvalidSessionException; //获取key为var1的value
    void setAttribute(Object var1, Object var2) throws InvalidSessionException; //设置键值对
    Object removeAttribute(Object var1) throws InvalidSessionException; //删除键值对
}

2. DelegatingSession 类(一种类型的会话类)

可以看到,大部分都是实现Session接口的方法,通过看到源码,可以得到的领悟是:Session中只是对自我属性的一些维护,SessionManager 会话管理器,就是提供对Session的各种操作方法,就称之为管理器。

DelegatingSession实现了Serializable接口,也是可以直接用于存储 和 网络传输的,但是没有显示指定serialVersionUID,就是JDK默认生成,如果(JDK版本不一样,很可能会出现反序列化问题:https://blog.csdn.net/dgh112233/article/details/100573649

public class DelegatingSession implements Session, Serializable {
    private final SessionKey key; //定义一个接口,此接口的作用是提供获取sessionId的方法,默认由DefaultSessionKey实现
    private Date startTimestamp = null; //会话创建时间
    private String host = null; //会话用户主机
    private final transient NativeSessionManager sessionManager; //会话管理器

    public DelegatingSession(NativeSessionManager sessionManager, SessionKey key) {
        if (sessionManager == null) { //每一个Session都必须与会话管理器挂钩
            throw new IllegalArgumentException("sessionManager argument cannot be null.");
        } else if (key == null) { //sessionKey 对象不能为空
            throw new IllegalArgumentException("sessionKey argument cannot be null.");
        } else if (key.getSessionId() == null) { //sessionId不能为空
            String msg = "The " + DelegatingSession.class.getName() + " implementation requires that the SessionKey argument returns a non-null sessionId to support the Session.getId() invocations.";
            throw new IllegalArgumentException(msg);
        } else {
            this.sessionManager = sessionManager;
            this.key = key;
        }
    }
    public Serializable getId() { //获取会话Id
        return this.key.getSessionId();
    }
    public Date getStartTimestamp() { //获取会话创建时间
        if (this.startTimestamp == null) {
            this.startTimestamp = this.sessionManager.getStartTimestamp(this.key);
        }
        return this.startTimestamp;
    }
    public Date getLastAccessTime() { //获取上一次访问时间
        return this.sessionManager.getLastAccessTime(this.key);
    }
    public long getTimeout() throws InvalidSessionException { //获取过期时间
        return this.sessionManager.getTimeout(this.key);
    }
//后面的get set方法就不多说了
    public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
        this.sessionManager.setTimeout(this.key, maxIdleTimeInMillis);
    }
    public String getHost() {
        if (this.host == null) {
            this.host = this.sessionManager.getHost(this.key);
        }
        return this.host;
    }
    public void touch() throws InvalidSessionException {
        this.sessionManager.touch(this.key);
    }
    public void stop() throws InvalidSessionException {
        this.sessionManager.stop(this.key);
    }
    public Collection<Object> getAttributeKeys() throws InvalidSessionException {
        return this.sessionManager.getAttributeKeys(this.key);
    }
    public Object getAttribute(Object attributeKey) throws InvalidSessionException {
        return this.sessionManager.getAttribute(this.key, attributeKey);
    }
    public void setAttribute(Object attributeKey, Object value) throws InvalidSessionException {
        if (value == null) {
            this.removeAttribute(attributeKey);
        } else {
            this.sessionManager.setAttribute(this.key, attributeKey, value);
        }
    }
    public Object removeAttribute(Object attributeKey) throws InvalidSessionException {
        return this.sessionManager.removeAttribute(this.key, attributeKey);
    }
}

3. ProxiedSession 类(一种类型的会话类)

proxied 英语意思就是 “代理”,何为代理,个人理解:就是在某个会话类的基础上再封装一次,通过操作这个代理的各种方法来达到操作某个会话类的目的,就好像代理商和厂家一样,你直接去厂家买,厂家不会卖给你的,你只有去代理商那里说你要什么东西,用户是直接跟代理商交互,代理商跟厂家直接交互,以此来达到,用户间接地与厂家交互。

public class ProxiedSession implements Session { 
    protected final Session delegate; //被代理的Session
    public ProxiedSession(Session target) { //构造函数
        if (target == null) {
            throw new IllegalArgumentException("Target session to proxy cannot be null.");
        } else {
            this.delegate = target;
        }
    }
    public Serializable getId() { //获取会话Id
        return this.delegate.getId();
    }
    public Date getStartTimestamp() { //获取会话创建时间
        return this.delegate.getStartTimestamp();
    }
    public Date getLastAccessTime() { //获取会话上一次访问时间
        return this.delegate.getLastAccessTime();
    }
    public long getTimeout() throws InvalidSessionException { //获取过期时间
        return this.delegate.getTimeout();
    }
    public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
        this.delegate.setTimeout(maxIdleTimeInMillis); //设置会话过期时间
    }
    public String getHost() { //设置会话用户的主机
        return this.delegate.getHost();
    }
    public void touch() throws InvalidSessionException { //空访问方法
        this.delegate.touch(); 
    }
    public void stop() throws InvalidSessionException { //停止会话
        this.delegate.stop();
    }
    public Collection<Object> getAttributeKeys() throws InvalidSessionException {
        return this.delegate.getAttributeKeys(); //获取会话的keys
    }
    public Object getAttribute(Object key) throws InvalidSessionException {
        return this.delegate.getAttribute(key); //根据key获取 value
    }
    public void setAttribute(Object key, Object value) throws InvalidSessionException {
        this.delegate.setAttribute(key, value); //设置key-value
    }
    public Object removeAttribute(Object key) throws InvalidSessionException {
        return this.delegate.removeAttribute(key); //删除key-value
    }
}

4. SimpleSession类 (常用的 会话类)

这个会话类是shiro提供的,属性比较全,方法也比较全,一般默认是使用的这个会话类。

5. StoppingAwareProxiedSession 类(很重要的会话类)

StoppingAwareProxiedSession 类是DelegatingSubject类的一个内部类,继承了ProxiedSession类,这个类的作用是:将我们的会话 与 用户关联起来,这样就知道哪个用户用的会话是哪个。

private class StoppingAwareProxiedSession extends ProxiedSession {
        private final DelegatingSubject owner; //此会话的用户
        // 构造函数 
        private StoppingAwareProxiedSession(Session target, DelegatingSubject owningSubject) {
            super(target);
            this.owner = owningSubject;
        }
        public void stop() throws InvalidSessionException { //停止会话
            super.stop();
            this.owner.sessionStopped();
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值