国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Spring mvc 分步式session的實例詳解

2024-09-05 00:18:52
字體:
來源:轉載
供稿:網友

Session是服務器和瀏覽器之間的會話,Session信息可以保存在服務器,接下來錯新技術頻道小編就為大家介紹Spring mvc 分步式session的實例詳解,一起來看看吧!

Spring mvc 分步式session的實例詳解

Session代表服務器與瀏覽器的一次會話過程,它的信息是保存在服務器端的。在Servlet中,session指的是HttpSession類的對象。服務器在創建session后,會把sessionid以cookie的形式回寫給客戶端。只要客戶端的瀏覽器不關,每一次訪問服務器都會帶上這個sessionid。這樣就可以在每次請求的時候獲取到session的信息。

下面以spring MVC以例來說明如果創建分步式session.

1、login – 登錄頁

login表示用戶跳轉到登錄頁面,這個時候可以生成唯一key為sessionid的sesssionid。保存sessionid到response中,當用戶真正登錄的時候獲取sessionid,因為是分步式所以統一的管理sessionid,把它保存在Redis緩存中。

public class LoginController{  private static final String SESSION_ID_COOKIE = "sessionId";  @Autowired  private RedisClient client;  @RequestMapping(value="/login/index")  public ModelAndView login(HttpServletRequest request, HttpServletResponse response){    ModelAndView modelAndView = new ModelAndView("/login");    String sessionId = CookieUtils.getCookieValue(request, SESSION_ID_COOKIE);    if(StringUtils.isEmptyOrNull(sessionId)) {      sessionId = getUniqueSessionId();      addCookie(response, SESSION_ID_COOKIE, sessionId);    }    // 其它業務相關信息    return modelAndView;  }  // 添加cookie到response  private void addCookie(HttpServletResponse response, String cookieName, String cookieValue) {    Cookie cookie = new Cookie(cookieName, cookieValue);    cookie.setPath("/");    cookie.setMaxAge(-1);    response.addCookie(cookie);  }  // 獲取唯一sessionid  private String getUniqueSessionId() {    String sessionId = "";    while(true) {      String uuid = UUID.randomUUID().toString();      String[] jsessionIdArray = uuid.split("-");      StringBuilder jsessionIdBuilder = new StringBuilder();      for(String str: jsessionIdArray) {        jsessionIdBuilder.append(str);      }      sessionId = jsessionIdBuilder.toString();      if(!redisClient.exists(sessionId)) {        break;      }    }    return sessionId;  }}

2、authenticate – 身份認證/登錄

用戶輸入用戶名密碼,需要在后臺進行登錄。此時可以從cookie中獲取到sessionid,然后再從redis里面獲取到它的值sessionidvalue。并且把需要保存的session信息以sessionidvalue為key,保存在redis里面。

?

public class LoginController{  private static final String SESSION_ID_COOKIE = "sessionId";  private static final Integer REDIS_SESSION_TIME_SECONDS = 2 * 60 * 60;  @Autowired  private RedisClient client;  @RequestMapping(value="/authenticate")  @ResponseBody  public ResponseInfo login(String username, String password, HttpServletRequest request){    ResponseInfo<Object> responseInfo = new ResponseInfo<Object>();    try {      Account account = authenticate(username, password);      saveSession(account);      responseInfo.setStatus(0);      responseInfo.setMessage("success");    } catch(Exceptioin e){      responseInfo.setStatus(1);      responseInfo.setMessage("服務器正忙,請稍候重試")    }    return responseInfo;  }  // 保存session信息  private void saveSession(Account account) {    String sessionId = CookieUtils.getCookieValue(request, SESSION_ID_COOKIE);    redisClient.set(sessionId, account);    redisClient.expire(sessionId, REDIS_SESSION_TIME_SECONDS);  }}

3、logout – 登出

當用戶選擇退出系統時,需要把頁面跳轉到登錄頁面也就是第一步并且需要刪除在redis里面的session信息以及在Cookie里面的sessionid.

public class LoginController{  private static final String SESSION_ID_COOKIE = "sessionId";  @Autowired  private RedisClient client;  @RequestMapping(value="/logout", method = RequestMethod.GET)  public void logout(HttpServletRequest request, HttpServletResponse response) {    // 獲取到重定向位置也就是登錄頁    String redirectUrl = this.getLoginPageRedirectUrl(request);    clearSession(request);    clearSessionCookie(request, response);    response.sendRedirect(redirectUrl);  }  // 刪除session在redis里面的信息  private void clearSession(HttpServletRequest request) {    String sessionId = CookieUtils.getCookieValue(request, SESSION_ID_COOKIE);    redisClient.del(sessionId);  }  // 刪除session在cookie中的sessionid  private void clearSessionCookie(HttpServletRequest request, HttpServletResponse response) {    Cookie sessionCookie = CookieUtils.getCookie(request, SESSION_ID_COOKIE);    sessionCookie.setMaxAge(0);    response.addCookie(sessionCookie);  }}

4、獲取session

可以把獲取session提取到公共的Controller中,這樣需要使用session信息的類就可以繼承這個類。

public class BaseController{  @Autowired  private RedisClient redisClient;  protected Account getAccountFromSession(HttpServletRequest request) {    String sessionCookieValue = CookieUtils.getCookieValue(request, "sessionid");    if(StringUtils.isEmptyOrNull(sessionCookieValue)) {      return null;    } else {      String accountJson = this.redisClient.get(sessionCookieValue);      Account account = null;      if(!StringUtils.isEmpty(accountJson)) {        account = (Account)JSON.parseObject(casInfoJson, Account.class);      }      return account;    }  }}

以上就是錯新技術頻道小編為各位朋友講解的Spring mvc 分步式session的實例詳解,希望小編的介紹對各位朋友有幫助,錯新技術頻道將為大家推薦更多的專業資訊。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克陶县| 曲靖市| 贵德县| 华蓥市| 迭部县| 闽侯县| 黄山市| 德昌县| 张家川| 建湖县| 宜兰市| 宜兴市| 锡林郭勒盟| 山东省| 巩义市| 邳州市| 旬阳县| 绩溪县| 贵州省| 临夏县| 青冈县| 许昌县| 垣曲县| 马关县| 西藏| 鄂尔多斯市| 彝良县| 楚雄市| 台山市| 乐昌市| 成武县| 鸡东县| 晋中市| 肥东县| 吴江市| 上高县| 宿迁市| 孙吴县| 定陶县| 永昌县| 大方县|