Java tutorial
/* * Copyright 2016. junfu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Licensed under the Apache License, Version 2.0 (the "License"); */ package org.lazulite.boot.autoconfigure.osaam.shiro.web.session.mgt; import org.lazulite.boot.autoconfigure.osaam.shiro.sys.user.entity.UserOnline; import com.google.common.collect.Lists; import org.apache.commons.lang3.time.DateUtils; import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.session.InvalidSessionException; import org.apache.shiro.session.Session; import org.apache.shiro.session.mgt.DefaultSessionKey; import org.apache.shiro.session.mgt.SessionKey; import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.lazulite.boot.autoconfigure.osaam.shiro.ShiroConstants; import org.lazulite.boot.autoconfigure.osaam.shiro.session.mgt.OnlineSession; import org.lazulite.boot.autoconfigure.osaam.shiro.sys.user.service.UserOnlineService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import java.util.Collection; import java.util.Date; import java.util.List; /** * OnlineSessionWeb Session Manager * ?? ? OnlineSessionDao? */ public class OnlineWebSessionManager extends DefaultWebSessionManager { private static final Logger log = LoggerFactory.getLogger(OnlineWebSessionManager.class); @Autowired private UserOnlineService userOnlineService; public void setUserOnlineService(UserOnlineService userOnlineService) { this.userOnlineService = userOnlineService; } @Override public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException { super.setAttribute(sessionKey, attributeKey, value); if (value != null && needMarkAttributeChanged(attributeKey)) { OnlineSession s = (OnlineSession) doGetSession(sessionKey); s.markAttributeChanged(); } } private boolean needMarkAttributeChanged(Object attributeKey) { if (attributeKey == null) { return false; } String attributeKeyStr = attributeKey.toString(); // flash?? if (attributeKeyStr.startsWith("org.springframework")) { return false; } if (attributeKeyStr.startsWith("javax.servlet")) { return false; } if (attributeKeyStr.equals(ShiroConstants.CURRENT_USERNAME)) { return false; } return true; } @Override public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException { Object removed = super.removeAttribute(sessionKey, attributeKey); if (removed != null) { OnlineSession s = (OnlineSession) doGetSession(sessionKey); s.markAttributeChanged(); } return removed; } /** * ?session? session */ @Override public void validateSessions() { if (log.isInfoEnabled()) { log.info("invalidation sessions..."); } int invalidCount = 0; int timeout = (int) getGlobalSessionTimeout(); Date expiredDate = DateUtils.addMilliseconds(new Date(), 0 - timeout); PageRequest pageRequest = new PageRequest(0, 100); Page<UserOnline> page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest); //?? while (page.hasContent()) { List<Long> needOfflineIdList = Lists.newArrayList(); for (UserOnline userOnline : page.getContent()) { try { SessionKey key = new DefaultSessionKey(userOnline.getId()); Session session = retrieveSession(key); //cache db if (session != null) { session.setAttribute(ShiroConstants.ONLY_CLEAR_CACHE, true); } validate(session, key); } catch (InvalidSessionException e) { if (log.isDebugEnabled()) { boolean expired = (e instanceof ExpiredSessionException); String msg = "Invalidated session with id [" + userOnline.getId() + "]" + (expired ? " (expired)" : " (stopped)"); log.debug(msg); } invalidCount++; needOfflineIdList.add(userOnline.getId()); } } if (needOfflineIdList.size() > 0) { try { userOnlineService.batchOffline(needOfflineIdList); } catch (Exception e) { log.error("batch delete db session error.", e); } } pageRequest = new PageRequest(0, pageRequest.getPageSize()); page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest); } if (log.isInfoEnabled()) { String msg = "Finished invalidation session."; if (invalidCount > 0) { msg += " [" + invalidCount + "] sessions were stopped."; } else { msg += " No sessions were stopped."; } log.info(msg); } } @Override protected Collection<Session> getActiveSessions() { throw new UnsupportedOperationException("getActiveSessions method not supported"); } }