您好,欢迎来到微智科技网。
搜索
您的当前位置:首页Shiro源码分析(九)——对Session进行操作

Shiro源码分析(九)——对Session进行操作

来源:微智科技网

2021SC@SDUSC

一.Demo源码

Session session = currentUser.getSession(); // 1
session.setAttribute("someKey", "aValue");  // 2
String value = (String) session.getAttribute("someKey"); // 3
if (value.equals("aValue")) {
    log.info("Retrieved the correct value! [" + value + "]");
}

二.源码分析

2

public class DelegatingSession implements Session, Serializable {
	// 将指定的值绑定到此会话,由指定的键名惟一地标识。如果键名下已经绑定了一个对象,则该现有对象将被新值替换。
	// 如果value参数为空,它的效果与调用removeAttribute相同。
    public void setAttribute(Object attributeKey, Object value) throws InvalidSessionException {
        if (value == null) {
            removeAttribute(attributeKey); // 2.1
        } else {
            sessionManager.setAttribute(this.key, attributeKey, value); // 2.2
        }
    }
}

2.1

public class DelegatingSession implements Session, Serializable {
	// 移除(解除绑定)指定键名下绑定到会话的对象。
    public Object removeAttribute(Object attributeKey) throws InvalidSessionException {
        return sessionManager.removeAttribute(this.key, attributeKey);
    }
}

public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager, EventBusAware {
    public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException {
    	// 根据sessionKey查找Session
        Session s = lookupRequiredSession(sessionKey); // 2.1.1
        // 根据attributeKey移除相应的属性
        Object removed = s.removeAttribute(attributeKey);
        if (removed != null) {
        	// 对session进行更新
            onChange(s);
        }
        return removed;
    }
}

2.1.1

public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager, EventBusAware {
    private Session lookupRequiredSession(SessionKey key) throws SessionException {
        Session session = lookupSession(key);
        if (session == null) {
            String msg = "Unable to locate required Session instance based on SessionKey [" + key + "].";
            throw new UnknownSessionException(msg);
        }
        return session;
    }
    
    private Session lookupSession(SessionKey key) throws SessionException {
        if (key == null) {
            throw new NullPointerException("SessionKey argument cannot be null.");
        }
        return doGetSession(key);
    }
    
    protected abstract Session doGetSession(SessionKey key) throws InvalidSessionException;

}

public abstract class AbstractValidatingSessionManager extends AbstractNativeSessionManager
        implements ValidatingSessionManager, Destroyable {
    protected final Session doGetSession(final SessionKey key) throws InvalidSessionException {
        enableSessionValidationIfNecessary();

        log.trace("Attempting to retrieve session with key {}", key);

        Session s = retrieveSession(key);
        if (s != null) {
            validate(s, key);
        }
        return s;
    }
}

2.2

public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager, EventBusAware {
	// 将指定的值绑定到由attributeKey唯一标识的关联会话。如果在attributeKey下已经绑定了一个会话属性,那么该现有对象将被新值替换。
	// 如果value参数为空,它的效果与调用removeAttribute(SessionKey SessionKey, Object attributeKey)方法相同。
    public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException {
        if (value == null) {
            removeAttribute(sessionKey, attributeKey); // 2.1
        } else {
            Session s = lookupRequiredSession(sessionKey); // 2.1.1
            s.setAttribute(attributeKey, value);
            onChange(s);
        }
    }
}

3

public class DelegatingSession implements Session, Serializable {
	// 返回由指定键标识的绑定到此会话的对象。如果键下没有绑定对象,则返回null
    public Object getAttribute(Object attributeKey) throws InvalidSessionException {
        return sessionManager.getAttribute(this.key, attributeKey);
    }
}

public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager, EventBusAware {
	// 返回绑定到由指定属性键标识的关联会话的对象。如果给定会话的属性键下没有对象绑定,则返回null。
    public Object getAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException {
        return lookupRequiredSession(sessionKey).getAttribute(attributeKey);
    }
}

(完)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务