我通过以下想法解决了这个问题:
用户登录时拳头输出LoginUserMap
和LoginUserKeyMap
会话范围。
LoginUserKeyMap
是用系统知道密钥存储userId。
例如:[键:user1,值:user120131010154566]
LoginUserMap
是存储登录的用户信息列表。
例如:[键:user120131010154566,值:对象]
/**
* Bijection Login User Map.
*/
@In(required = false, scope = ScopeType.APPLICATION, value = "loginUserMap")
@Out(required = false, scope = ScopeType.APPLICATION, value = "loginUserMap")
private Map<String, UserInfoBean> loginUserMap;
/**
* Bijection Login User Key Map.
*/
@In(required = false, scope = ScopeType.APPLICATION, value = "loginUserKeyMap")
@Out(required = false, scope = ScopeType.APPLICATION, value = "loginUserKeyMap")
private Map<String, String> loginUserKeyMap;
public void doLogin() {
// Generate User ID Key for duplicate user control.
String key = CommonUtil.convertDateToString(new Date(), KEY_PATTERN);
String userId = getCredentials().getUsername();
String userIdKey = userId + key;
if (getLoginUserMap() == null || getLoginUserKeyMap() == null) {
// Initialize the Login User Map.
setLoginUserMap(new HashMap());
// Initialize the Login User Key Map.
setLoginUserKeyMap(new HashMap());
}
// Check login User id is already login or not.
if (getLoginUserKeyMap().containsKey(userId)) {
log.info("Duplicate Login");
// Get Current logged in User's Key.
String CurrentUserKey = getLoginUserKeyMap().get(userId);
// Get Current logged in User Information.
UserInfoBean currentUserInfoBean = getLoginUserMap().get(CurrentUserKey);
if (currentUserInfoBean != null) {
// Set Duplicate flag true to Current logged User.
currentUserInfoBean.setDuplicate(true);
// Overwrite Current logged User Information.
getLoginUserMap().put(CurrentUserKey, currentUserInfoBean);
}
}
// Set New Login User Information.
getUserInfoBean().setUserId(userId);
getUserInfoBean().setUserIdKey(userIdKey);
getUserInfoBean().setDuplicate(false);
getUserInfoBean().setServiceStop(false);
// Set New Login User Information and Key to Application Scope.
getLoginUserKeyMap().put(userId, userIdKey);
getLoginUserMap().put(userIdKey, getUserInfoBean());
}
然后,创建checkStatus()
方法
public void checkStatus() throws DuplicateLoginException, UserServiceStopException {
if (getUserInfoBean() != null && getLoginUserMap() != null) {
UserInfoBean currentUser = getLoginUserMap().get(getUserInfoBean().getUserIdKey());
if (currentUser != null) {
if (currentUser.isServiceStop()) {
log.error("throw new UserServiceStopException()");
throw new UserServiceStopException();
} else if (currentUser.isDuplicate()) {
log.error("throw new DuplicateLoginException()");
throw new DuplicateLoginException();
}
}
}
}
checkStatus()
并从每一页调用该方法。
<page view-id="/view/*">
<action execute="#{UserStatusChecker.checkStatus()}" />
现在问题解决了!!!!