Java Code Examples for javax.servlet.http.HttpSessionBindingEvent#getName()

The following examples show how to use javax.servlet.http.HttpSessionBindingEvent#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoginSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
	String name = event.getName();

	// 登录
	if (name.equals("personInfo")) {

		PersonInfo personInfo = (PersonInfo) event.getValue();

		if (map.get(personInfo.getAccount()) != null) {

			// map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
			HttpSession session = map.get(personInfo.getAccount());
			PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");

			logger.debug("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");

			session.removeAttribute("personInfo");
			session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
		}

		// 将session以用户名为索引,放入map中
		map.put(personInfo.getAccount(), event.getSession());
		logger.debug("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");
	}
}
 
Example 2
Source File: LoginSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
	String name = event.getName();

	// 没有注销的情况下,用另一个帐号登录
	if (name.equals("personInfo")) {

		// 移除旧的的登录信息
		PersonInfo oldPersonInfo = (PersonInfo) event.getValue();
		map.remove(oldPersonInfo.getAccount());

		// 新的登录信息
		PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");

		// 也要检查新登录的帐号是否在别的机器上登录过
		if (map.get(personInfo.getAccount()) != null) {
			// map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
			HttpSession session = map.get(personInfo.getAccount());
			session.removeAttribute("personInfo");
			session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
		}
		map.put("personInfo", event.getSession());
	}
}
 
Example 3
Source File: WebSystemHttpSessionListener.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
	try {
		// --------------------------------------------------
		// 2017-12-22 add
		// --------------------------------------------------
		String name=event.getName();
		if (Constants.SESS_ACCOUNT.equals(name)) {
			sessionsMap.put(event.getSession().getId(), event.getSession());
		}
		// --------------------------------------------------
		
		this.cleanBeforeLoginUser(event);
		this.writeLoginUser(event);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: WebSystemHttpSessionListener.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
/**
 * 清除之前已有相同帳戶登入的 sessionId 紀錄
 * 
 * @param arg0
 * @throws Exception
 */
private void cleanBeforeLoginUser(HttpSessionBindingEvent event) throws Exception {
	if (!"Y".equals(invalidateSameAccountSession)) {
		return;
	}
	String name=event.getName();
	Object value=event.getValue();
	if (Constants.SESS_ACCOUNT.equals(name)) {
		String account=((AccountObj)value).getAccount();
		List<String> sessionIdList=uSessLogHelper.findSessionIdByAccount(account);
		for (int ix=0; sessionIdList!=null && ix<sessionIdList.size(); ix++) {
			String sessId=sessionIdList.get(ix);
			HttpSession beforeUserHttpSession=sessionsMap.get(sessId);
			beforeUserHttpSession.invalidate();
			sessionsMap.remove(sessId);
			//uSessLogHelper.delete(sessId); invalidate 時 sessionDestroyed 有做刪除紀錄的功能
			log.warn("invalidate before session:"+sessId + " account:"+account);
		}
	}
}
 
Example 5
Source File: HttpFlexSession.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
/**
 * HttpSessionAttributeListener callback; processes the addition of an attribute to an HttpSession.
 *
 * NOTE: Callback is not made against an HttpFlexSession associated with a request
 * handling thread.
 * @param event the HttpSessionBindingEvent
 */
public void attributeAdded(HttpSessionBindingEvent event)
{
    if (!event.getName().equals(SESSION_ATTRIBUTE))
    {
        // Accessing flexSession via map because it may have already been unbound from httpSession.
        Map httpSessionToFlexSessionMap = getHttpSessionToFlexSessionMap(event.getSession());
        HttpFlexSession flexSession = (HttpFlexSession)httpSessionToFlexSessionMap.get(event.getSession().getId());
        if (flexSession != null)
        {
            String name = event.getName();
            Object value = event.getValue();
            flexSession.notifyAttributeBound(name, value);
            flexSession.notifyAttributeAdded(name, value);
        }
    }
}
 
Example 6
Source File: HttpFlexSession.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
/**
 * HttpSessionAttributeListener callback; processes the removal of an attribute from an HttpSession.
 *
 * NOTE: Callback is not made against an HttpFlexSession associated with a request
 * handling thread.
 * @param event the HttpSessionBindingEvent
 */
public void attributeRemoved(HttpSessionBindingEvent event)
{
    if (!event.getName().equals(SESSION_ATTRIBUTE))
    {
        // Accessing flexSession via map because it may have already been unbound from httpSession.
        Map httpSessionToFlexSessionMap = getHttpSessionToFlexSessionMap(event.getSession());
        HttpFlexSession flexSession = (HttpFlexSession)httpSessionToFlexSessionMap.get(event.getSession().getId());
        if (flexSession != null)
        {
            String name = event.getName();
            Object value = event.getValue();
            flexSession.notifyAttributeUnbound(name, value);
            flexSession.notifyAttributeRemoved(name, value);
        }
    }
}
 
Example 7
Source File: HttpFlexSession.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
/**
 * HttpSessionAttributeListener callback; processes the replacement of an attribute in an HttpSession.
 *
 * NOTE: Callback is not made against an HttpFlexSession associated with a request
 * handling thread.
 * @param event the HttpSessionBindingEvent
 */
public void attributeReplaced(HttpSessionBindingEvent event)
{
    if (!event.getName().equals(SESSION_ATTRIBUTE))
    {
        // Accessing flexSession via map because it may have already been unbound from httpSession.
        Map httpSessionToFlexSessionMap = getHttpSessionToFlexSessionMap(event.getSession());
        HttpFlexSession flexSession = (HttpFlexSession)httpSessionToFlexSessionMap.get(event.getSession().getId());
        if (flexSession != null)
        {
            String name = event.getName();
            Object value = event.getValue();
            Object newValue = flexSession.getAttribute(name);
            flexSession.notifyAttributeUnbound(name, value);
            flexSession.notifyAttributeReplaced(name, value);
            flexSession.notifyAttributeBound(name, newValue);
        }
    }
}
 
Example 8
Source File: MyHttpSessionAttributeListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
	HttpSession session = se.getSession();
	String name = se.getName();
	Object oldValue = se.getValue();
	logger.info("修改session属性:" + name + ", 原值:" + oldValue + ", 新值:" + session.getAttribute(name));
}
 
Example 9
Source File: LoginSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
	String name = event.getName();

	// 注销
	if (name.equals("personInfo")) {
		// 将该session从map中移除
		PersonInfo personInfo = (PersonInfo) event.getValue();
		map.remove(personInfo.getAccount());
		logger.debug("帐号" + personInfo.getAccount() + "注销。");
	}
}
 
Example 10
Source File: WebSystemHttpSessionListener.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private void writeLoginUser(HttpSessionBindingEvent event) throws Exception {
	String sessionId=event.getSession().getId();
	String name=event.getName();
	Object value=event.getValue();
	if (Constants.SESS_ACCOUNT.equals(name)) {
		if (uSessLogHelper.count(sessionId)>0) {
			uSessLogHelper.delete(sessionId);
		}
		uSessLogHelper.insert(sessionId, ((AccountObj)value).getAccount());
	}		
}
 
Example 11
Source File: WebSystemHttpSessionListener.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
private void clearLoginUser(HttpSessionBindingEvent event) throws Exception {
	String sessionId=event.getSession().getId();
	String name=event.getName();
	if (Constants.SESS_ACCOUNT.equals(name)) {
		if (uSessLogHelper.count(sessionId)>0) {
			uSessLogHelper.delete(sessionId);
		}
	}			
}
 
Example 12
Source File: MyHttpSessionAttributeListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
	HttpSession session = se.getSession();
	String name = se.getName();
	logger.info("新建session属性:" + name + ", 值为:" + se.getValue());
}
 
Example 13
Source File: MyHttpSessionAttributeListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
	HttpSession session = se.getSession();
	String name = se.getName();
	logger.info("删除session属性:" + name + ", 值为:" + se.getValue());
}