2021SC@SDUSC
一.Demo源码
这是在shiro源码的quickstart目录下有QuickStart.java文件,截取了其中对session进行的操作演示。
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);
}
}
(完)