亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java Web之限制用戶多處登錄實(shí)例代碼

 更新時(shí)間:2017年03月11日 09:46:56   作者:BrightLoong  
本篇文章主要介紹了Java Web之限制用戶多處登錄實(shí)例代碼,可以限制單個(gè)用戶在多個(gè)終端登錄。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

最近在項(xiàng)目中遇到一個(gè)需求,要求限制單個(gè)用戶在多個(gè)終端登錄(比如用戶在A處登錄,然后又在B處登錄,此時(shí)A處就應(yīng)該被擠下線)。<!--more-->最開(kāi)始我是想使用spring的security直接通過(guò)配置實(shí)現(xiàn),簡(jiǎn)單又方便。不過(guò)很可惜的是,我所做的項(xiàng)目使用的是公司封裝的框架,依然在使用sprign2.X。好吧,既然這個(gè)方法行不通,那我自己老老實(shí)實(shí)寫代碼實(shí)現(xiàn)吧,想想網(wǎng)上實(shí)現(xiàn)的方法應(yīng)該很多吧,度娘、谷歌走一波,果斷很多,不過(guò)過(guò)去過(guò)來(lái)感覺(jué)都是同一個(gè)。還有就是什么使用application啊,session什么的。最后,我還是自己動(dòng)手,豐衣足食吧。首先我說(shuō)一下自己的思路:

用一個(gè)全局Map在登錄的時(shí)候用來(lái)保存sessionId,Map的key為登錄名,value為sessionID,因?yàn)槭呛髞?lái)的擠掉前面的,所以不用判斷,直接覆蓋Map中的值就OK。

實(shí)現(xiàn)一個(gè)HttpSessionListener,在session銷毀(比如session過(guò)期)的時(shí)候清除Map中對(duì)應(yīng)的值。

實(shí)現(xiàn)一個(gè)Filter,用于攔截請(qǐng)求,判斷改用當(dāng)前的sessionId是否在Map中,如果不在執(zhí)行退出操作。

用來(lái)保存登入用戶SessionID的類

public class LoginUserMap {

  private static Map<String, String> loginUsers = new ConcurrentHashMap<String, String>();

  /**
   * 將用戶和sessionId存入map
   * @param key
   * @param value
   */
  public static void setLoginUsers(String loginId, String sessionId) {
    loginUsers.put(loginId, sessionId);
  }

  /**
   * 獲取loginUsers
   * @return
   */
  public static Map<String, String> getLoginUsers() {
    return loginUsers;
  }

  /**
   * 根據(jù)sessionId移除map中的值
   * @param sessionId
   */
  public static void removeUser(String sessionId) {
    for (Map.Entry<String, String> entry : loginUsers.entrySet()) {
      if (sessionId.equals(entry.getValue())) {
        loginUsers.remove(entry.getKey());
        break;
      }
    }
  }

  /**
   * 判斷用戶是否在loginusers中
   * @param loginId
   * @param sessionId
   * @return
   */
  public static boolean isInLoginUsers(String loginId, String sessionId) {
    return (loginUsers.containsKey(loginId) && sessionId.equals(loginUsers.get(loginId)));
  }

}

在登錄方法中保存sessionID

這里我就不給出具體的實(shí)現(xiàn)了,畢竟不同的項(xiàng)目是不同的,我寫個(gè)大概的步驟,幫助理解:

//登錄方法所在的地方
public void login(ttpServletRequest request) {
  try {  
      ......//一系列登錄的方法
      HttpSession session = request.getSession();
      LoginUserMap.setLoginUsers(username, session.getId());//保存sessionId到map中
    } catch (LoginException ex) {
      throw ex;
    }
}

實(shí)現(xiàn)HttpSessionListener

public class SessionListener implements HttpSessionListener {
  private Log log = LogFactory.getLog(SessionListener.class);

  /**
   * 創(chuàng)建session時(shí)候的動(dòng)作
   * @param event
   */
  @Override
  public void sessionCreated(HttpSessionEvent event) {

  }

  /**
   * 銷毀session時(shí)候的動(dòng)作
   * @param event
   */
  @Override
  public void sessionDestroyed(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    String sessionId = session.getId();
    //移除loginUsers中已經(jīng)被銷毀的session
    LoginUserMap.removeUser(sessionId);
    log.info(sessionId + "被銷毀!");
    }
}

xml配置如下:

  <listener>
    <listener-class>io.github.brightloong.loginlimite.SessionListener</listener-class>
  </listener>

Filter實(shí)現(xiàn)

public class LoginLimitFilter implements Filter{

  private Log log = LogFactory.getLog(LoginLimitFilter.class);

  /**
   * 銷毀時(shí)的方法
   */
  @Override
  public void destroy() {

  }

  /**
   * 過(guò)濾請(qǐng)求
   * @param request
   * @param response
   * @param filterChain
   * @throws IOException
   * @throws ServletException
   */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
     FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest servletRequest = (HttpServletRequest) request;
    HttpServletResponse servletResponse = (HttpServletResponse) response;
    HttpSession session = servletRequest.getSession();

    //獲取項(xiàng)目路徑
    String path = servletRequest.getContextPath(); 
    String basePath = servletRequest.getScheme()+"://"+servletRequest.getServerName()+":"+servletRequest.getServerPort()+path;

    try {
      //獲取用戶信息,如果沒(méi)獲取到會(huì)拋出錯(cuò)誤,我的是這樣,代表用戶還沒(méi)有登錄
      IUser user = UserUtils.getCurrUserInfo();
      String loginId = user.getLoginId();
      //判斷當(dāng)前用戶的sessionId是否在loginUsers中,如果沒(méi)有執(zhí)行if后的操作
      if(!LoginUserMap.isInLoginUsers(loginId, session.getId())) {
        //當(dāng)前用戶logout
        logout();//自己的logout方法
        //調(diào)到登錄頁(yè)面,并表明退出方式為擠下線
        servletResponse.sendRedirect(basePath + "?logoutway=edge");
      }
    } catch (Exception e) {
      log.debug("獲取當(dāng)前用戶信息失敗,用戶未登陸!", e);
    } finally {
      filterChain.doFilter(request, response);
    }
  }

  /**
   * 初始化方法
   * @param arg0
   * @throws ServletException
   */
  @Override
  public void init(FilterConfig arg0) throws ServletException {

  }

}

xml配置如下:

  <filter>
    <filter-name>LoginLimitFilter</filter-name>
    <filter-class>io.github.brightloong.loginlimite.LoginLimitFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginLimitFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

顯示提示信息

當(dāng)用戶點(diǎn)擊的時(shí)候就會(huì)觸發(fā)filter去監(jiān)測(cè),如果監(jiān)測(cè)到已經(jīng)登錄,就會(huì)轉(zhuǎn)到登錄頁(yè)面,這個(gè)時(shí)候要判斷是否是被擠下來(lái)的,我這使用了layer提示框。

window.onload = function(){
  if(window.parent != window){
    window.parent.location.href=window.location.href;
  } else {
    if(GetQueryString('logoutway')) {
      //alert('該用戶已在其他地方登錄,你已下線');
      layer.alert('該賬號(hào)已在其他地方登錄,您已被迫下線,如非本人操作請(qǐng)重新登錄并及時(shí)修改密碼', {
         skin: 'layui-layer-lan', //樣式類名
         title: '提示'
         ,closeBtn: 0
        }, function(){
          var url = window.location.href;
          window.location.href = url.substr(0,url.indexOf('?logoutway=edge'));
        });
   }
  }
}

function GetQueryString(name)
{
   var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
   var r = window.location.search.substr(1).match(reg);
   if(r!=null)return unescape(r[2]); return null;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot集成Druid連接池進(jìn)行SQL監(jiān)控的問(wèn)題解析

    SpringBoot集成Druid連接池進(jìn)行SQL監(jiān)控的問(wèn)題解析

    這篇文章主要介紹了SpringBoot集成Druid連接池進(jìn)行SQL監(jiān)控的問(wèn)題解析,在SpringBoot工程中引入Druid連接池非常簡(jiǎn)單,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • Druid(新版starter)在SpringBoot下的使用教程

    Druid(新版starter)在SpringBoot下的使用教程

    Druid是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池,Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能,DruidDataSource支持的數(shù)據(jù)庫(kù),這篇文章主要介紹了Druid(新版starter)在SpringBoot下的使用,需要的朋友可以參考下
    2023-05-05
  • 使用@Value注入map、List,yaml格式方式

    使用@Value注入map、List,yaml格式方式

    這篇文章主要介紹了使用@Value注入map、List,yaml格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring中@RabbitHandler和@RabbitListener的區(qū)別詳析

    Spring中@RabbitHandler和@RabbitListener的區(qū)別詳析

    @RabbitHandler是用于處理消息的方法注解,它與@RabbitListener注解一起使用,這篇文章主要給大家介紹了關(guān)于Spring中@RabbitHandler和@RabbitListener區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2024-02-02
  • Spring框架十一種常見(jiàn)異常的解決方法匯總

    Spring框架十一種常見(jiàn)異常的解決方法匯總

    今天小編就為大家分享一篇關(guān)于Spring框架十一種常見(jiàn)異常的解決方法匯總,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • Java簡(jiǎn)單數(shù)組排序(冒泡法)

    Java簡(jiǎn)單數(shù)組排序(冒泡法)

    這篇文章主要介紹了Java簡(jiǎn)單數(shù)組排序,實(shí)例分析了基于冒泡法實(shí)現(xiàn)數(shù)組排序的相關(guān)技巧,簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • JAVA進(jìn)階之HashMap底層實(shí)現(xiàn)解析

    JAVA進(jìn)階之HashMap底層實(shí)現(xiàn)解析

    Hashmap是java面試中經(jīng)常遇到的面試題,大部分都會(huì)問(wèn)其底層原理與實(shí)現(xiàn),為了能夠溫故而知新,特地寫了這篇文章,以便時(shí)時(shí)學(xué)習(xí)
    2021-11-11
  • Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能

    Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能

    在前后端分離的Web開(kāi)發(fā)模式中,接口(API)扮演著至關(guān)重要的角色,它是前后端交互的橋梁,創(chuàng)建加法計(jì)算器和用戶登錄功能時(shí),介紹了接口測(cè)試和問(wèn)題解決的一般流程,如使用Postman測(cè)試接口、查看日志、處理緩存問(wèn)題等,確保開(kāi)發(fā)過(guò)程中的高效協(xié)作和問(wèn)題快速定位
    2024-10-10
  • 深入聊聊Java內(nèi)存泄露問(wèn)題

    深入聊聊Java內(nèi)存泄露問(wèn)題

    所謂內(nèi)存泄露就是指一個(gè)不再被程序使用的對(duì)象或變量一直被占據(jù)在內(nèi)存中,下面這篇文章主要給大家介紹了關(guān)于Java內(nèi)存泄露問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 使用feign發(fā)送http請(qǐng)求解析報(bào)錯(cuò)的問(wèn)題

    使用feign發(fā)送http請(qǐng)求解析報(bào)錯(cuò)的問(wèn)題

    這篇文章主要介紹了使用feign發(fā)送http請(qǐng)求解析報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論