Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓƵ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Modeler   Code  
»áÔ±   
 
   
 
 
     
   
 ¶©ÔÄ
  ¾èÖú
ͨ¹ýSpring SessionʵÏÖÐÂÒ»´úµÄSession¹ÜÀí
 
×÷ÕߣºAdib Saikali À´Ô´£ºInfoQ ·¢²¼ÓÚ 2016-2-18
  1474  次浏览      20
 

³¤ÆÚÒÔÀ´£¬session¹ÜÀí¾ÍÊÇÆóÒµ¼¶JavaÖеÄÒ»²¿·Ö£¬ÒÔÖÂÓÚÎÒÃÇDZÒâʶ¾ÍÈÏΪËüÊÇÒѾ­½â¾öµÄÎÊÌ⣬ÔÚ×î½üµÄ¼ÇÒäÖУ¬ÎÒÃÇûÓп´µ½Õâ¸öÁìÓòÓкܴóµÄ¸ïС£

µ«ÊÇ£¬ÏÖ´úµÄÇ÷ÊÆÊÇ΢·þÎñÒÔ¼°¿ÉˮƽÀ©Õ¹µÄÔ­ÉúÔÆÓ¦Óã¨cloud native application£©£¬ËüÃÇ»áÌôÕ½¹ýÈ¥20¶àÄêÀ´ÎÒÃÇÉè¼ÆºÍ¹¹½¨session¹ÜÀíÆ÷ʱµÄÇ°Ìá¼ÙÉ裬²¢ÇÒ±©Â¶ÁËÏÖ´ú»¯session¹ÜÀíÆ÷µÄ²»×ã¡£

±¾ÎĽ«»á²ûÊö×î½ü·¢²¼µÄSpring Session APIÈçºÎ°ïÖúÎÒÃÇ¿Ë·þÑÛÏÂsession¹ÜÀí·½Ê½ÖеÄһЩ²»×㣬ÔÚÆóÒµ¼¶JavaÖУ¬´«Í³É϶¼»á²ÉÓÃÕâÖ־ɵķ½Ê½¡£ÎÒÃÇÊ×ÏÈ»á¼òµ¥²ûÊöһϵ±Ç°session¹ÜÀíÖеÄÎÊÌ⣬ȻºóÉîÈë½éÉÜSpring SessionÊÇÈçºÎ½â¾öÕâЩÎÊÌâµÄ¡£ÔÚÎÄÕµÄ×îºó£¬½«»áÏêϸչʾSpring SessionÊÇÈçºÎÔËÐеģ¬ÒÔ¼°ÔÚÏîÄ¿ÖÐÔõÑùʹÓÃËü¡£

Spring SessionΪÆóÒµ¼¶JavaÓ¦ÓõÄsession¹ÜÀí´øÀ´Á˸ïУ¬Ê¹µÃÒÔÏµĹ¦Äܸü¼ÓÈÝÒ×ʵÏÖ£º

±àд¿ÉˮƽÀ©Õ¹µÄÔ­ÉúÔÆÓ¦Óá£

½«sessionËù±£´æµÄ״̬жÔص½Ìض¨µÄÍⲿsession´æ´¢ÖУ¬ÈçRedis»òApache GeodeÖУ¬ËüÃÇÄܹ»ÒÔ¶ÀÁ¢ÓÚÓ¦Ó÷þÎñÆ÷µÄ·½Ê½Ìṩ¸ßÖÊÁ¿µÄ¼¯Èº¡£

µ±Óû§Ê¹ÓÃWebSocket·¢ËÍÇëÇóµÄʱºò£¬Äܹ»±£³ÖHttpSession´¦ÓÚ»îԾ״̬¡£

ÔÚ·ÇWebÇëÇóµÄ´¦Àí´úÂëÖУ¬Äܹ»·ÃÎÊsessionÊý¾Ý£¬±ÈÈçÔÚJMSÏûÏ¢µÄ´¦Àí´úÂëÖС£

Ö§³Öÿ¸öä¯ÀÀÆ÷ÉÏʹÓöà¸ösession£¬´Ó¶øÄܹ»ºÜÈÝÒ׵ع¹½¨¸ü¼Ó·á¸»µÄÖÕ¶ËÓû§ÌåÑé¡£

¿ØÖÆsession idÈçºÎÔÚ¿Í»§¶ËºÍ·þÎñÆ÷Ö®¼ä½øÐн»»»£¬ÕâÑùµÄ»°¾ÍÄܺÜÈÝÒ׵رàдRestful API£¬ÒòΪËü¿ÉÒÔ´ÓHTTP Í·ÐÅÏ¢ÖлñÈ¡session id£¬¶ø²»±ØÔÙÒÀÀµÓÚcookie¡£

ÐèҪ˵Ã÷µÄºÜÖØÒªµÄÒ»µã¾ÍÊÇ£¬Spring SessionµÄºËÐÄÏîÄ¿²¢²»ÒÀÀµÓÚSpring¿ò¼Ü£¬ËùÒÔ£¬ÎÒÃÇÉõÖÁÄܹ»½«ÆäÓ¦ÓÃÓÚ²»Ê¹ÓÃSpring¿ò¼ÜµÄÏîÄ¿ÖС£

´«Í³session¹ÜÀíµÄÎÊÌâ

´«Í³µÄJavaEE session¹ÜÀí»áÓи÷ÖÖÎÊÌ⣬ÕâÇ¡ºÃÊÇSpring SessionËùÒªÊÔͼ½â¾öµÄ¡£ÕâЩÎÊÌâÔÚÏÂÃæÒÔÑùÀýµÄÐÎʽ½øÐÐÁ˲ûÊö¡£

¹¹½¨¿ÉˮƽÀ©Õ¹µÄÔ­ÉúÔÆÓ¦ÓÃ

ÔÚÔ­ÉúµÄÔÆÓ¦Óüܹ¹ÖУ¬»á¼ÙÉèÓ¦ÓÃÄܹ»½øÐÐÀ©Õ¹£¬ÕâÊÇͨ¹ýÔÚLinuxÈÝÆ÷ÖÐÔËÐиü¶àµÄÓ¦ÓóÌÐòʵÀýʵÏֵģ¬ÕâЩÈÝÆ÷»áλÓÚÒ»¸ö´óÐ͵ÄÐéÄâ»ú³ØÖС£ÀýÈ磬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ׵ؽ«Ò»¸ö¡°.war¡±Îļþ²¿Êðµ½Î»ÓÚCloud Foundry»òHerokuµÄTomcatÖУ¬È»ºóÔÚ¼¸ÃëÖÓµÄʱ¼äÄÚ¾ÍÄÜÀ©Õ¹µ½100¸öÓ¦ÓÃʵÀý£¬Ã¿¸öʵÀý¿ÉÒÔ¾ßÓÐ1GB RAM¡£ÎÒÃÇ»¹¿ÉÒÔÅäÖÃÔÆƽ̨£¬»ùÓÚÓû§µÄÐèÇó×Ô¶¯Ôö¼ÓºÍ¼õÉÙÓ¦ÓÃʵÀýµÄÊýÁ¿¡£

ÔںܶàµÄÓ¦Ó÷þÎñÆ÷ÖУ¬¶¼»á½«HTTP session״̬±£´æÔÚJVMÖУ¬Õâ¸öJVMÓëÔËÐÐÓ¦ÓóÌÐò´úÂëµÄJVMÊÇͬһ¸ö£¬ÒòΪÕâÑùÒ×ÓÚʵÏÖ£¬²¢ÇÒËٶȺܿ졣µ±ÐµÄÓ¦Ó÷þÎñÆ÷ʵÀý¼ÓÈë»òÀ뿪¼¯ÈºÊ±£¬HTTP session»á»ùÓÚÏÖÓеÄÓ¦Ó÷þÎñÆ÷ʵÀý½øÐÐÖØÐÂƽºâ¡£ÔÚµ¯ÐÔµÄÔÆ»·¾³ÖУ¬ÎÒÃÇ»áÓµÓÐÉÏ°Ù¸öÓ¦Ó÷þÎñÆ÷ʵÀý£¬²¢ÇÒʵÀýµÄÊýÁ¿¿ÉÄÜÔÚÈÎÒâʱ¿ÌÔö¼Ó»ò¼õÉÙ£¬ÕâÑùµÄ»°£¬ÎÒÃǾͻáÓöµ½Ò»Ð©ÎÊÌ⣺

ÖØƽºâHTTP session¿ÉÄÜ»á³ÉΪÐÔÄÜÆ¿¾±¡£

ΪÁË´æ´¢´óÁ¿µÄsession£¬»áÐèÒªºÜ´óµÄ¶Ñ¿Õ¼ä£¬Õâ»áµ¼ÖÂÀ¬»øÊÕ¼¯£¬´Ó¶ø¶ÔÐÔÄܲúÉú¸ºÃæÓ°Ïì¡£

ÔÆ»ù´¡Éèʩͨ³£»á½ûÖ¹TCP¶à²¥£¨multicast£©£¬µ«ÊÇsession¹ÜÀíÆ÷³£³£»áʹÓÃÕâÖÖ»úÖÆÀ´·¢ÏÖÄÄÒ»¸öÓ¦Ó÷þÎñÆ÷ʵÀý¼ÓÈë»òÀ뿪Á˼¯Èº¡£

Òò´Ë£¬¸üΪ¸ßЧµÄ°ì·¨Êǽ«HTTP session״̬±£´æÔÚ¶ÀÁ¢µÄÊý¾Ý´æ´¢ÖУ¬Õâ¸ö´æ´¢Î»ÓÚÔËÐÐÓ¦ÓóÌÐò´úÂëµÄJVMÖ®Íâ¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½«100¸öTomcatʵÀýÅäÖÃΪʹÓÃRedisÀ´´æ´¢session״̬£¬µ±TomcatʵÀýÔö¼Ó»ò¼õÉÙµÄʱºò£¬RedisÖÐËù´æ´¢µÄsession²¢²»»áÊܵ½Ó°Ï졣ͬʱ£¬ÒòΪRedisÊÇʹÓÃCÓïÑÔ±àдµÄ£¬ËùÒÔËü¿ÉÒÔʹÓÃÉÏ°ÙGBÉõÖÁTB¼¶±ðµÄRAM£¬Ëü²»»áÉæ¼°µ½À¬»øÊÕ¼¯µÄÎÊÌâ¡£

¶ÔÓÚÏñTomcatÕâÑùµÄ¿ªÔ´·þÎñÆ÷£¬ºÜÈÝÒ×ÕÒµ½session¹ÜÀíÆ÷µÄÌæ´ú·½°¸£¬ÕâЩÌæ´ú·½°¸¿ÉÒÔʹÓÃÍⲿµÄÊý¾Ý´æ´¢£¬ÈçRedis»òMemcached¡£µ«ÊÇ£¬ÕâЩÅäÖùý³Ì¿ÉÄÜ»á±È½Ï¸´ÔÓ£¬¶øÇÒÿÖÖÓ¦Ó÷þÎñÆ÷¶¼ÓÐËù²î±ð¡£¶ÔÓÚ±ÕÔ´µÄ²úÆ·£¬ÈçWebSphereºÍWeblogic£¬Ñ°ÕÒËüÃǵÄsession¹ÜÀíÆ÷Ìæ´ú·½°¸²»½ö·Ç³£À§ÄÑ£¬ÔÚÓÐЩʱºò£¬ÉõÖÁÊÇÎÞ·¨ÊµÏֵġ£

Spring SessionÌṩÁËÒ»ÖÖ¶ÀÁ¢ÓÚÓ¦Ó÷þÎñÆ÷µÄ·½°¸£¬ÕâÖÖ·½°¸Äܹ»ÔÚServlet¹æ·¶Ö®ÄÚÅäÖÿɲå°ÎµÄsessionÊý¾Ý´æ´¢£¬²»ÒÀÀµÓÚÈκÎÓ¦Ó÷þÎñÆ÷µÄÌض¨API¡£Õâ¾ÍÒâζ×ÅSpring SessionÄܹ»ÓÃÓÚʵÏÖÁËservlet¹æ·¶µÄËùÓÐÓ¦Ó÷þÎñÆ÷Ö®ÖУ¨Tomcat¡¢Jetty¡¢ WebSphere¡¢WebLogic¡¢JBossµÈ£©£¬ËüÄܹ»·Ç³£±ãÀûµØÔÚËùÓÐÓ¦Ó÷þÎñÆ÷ÖÐÒÔÍêÈ«ÏàͬµÄ·½Ê½½øÐÐÅäÖá£ÎÒÃÇ»¹¿ÉÒÔÑ¡ÔñÈÎÒâ×îÊÊÓ¦ÐèÇóµÄÍⲿsessionÊý¾Ý´æ´¢¡£ÕâʹµÃSpring Session³ÉΪһ¸öºÜÀíÏëµÄǨÒƹ¤¾ß£¬°ïÖúÎÒÃǽ«´«Í³µÄJavaEEÓ¦ÓÃתÒƵ½ÔÆÖУ¬Ê¹Æä³ÉΪÂú×ã 12-factorµÄÓ¦Óá£

ÿ¸öÓû§Óжà¸öÕ˺Å

¼ÙÉèÎÒÃÇÔÚexample.comÉÏÔËÐÐÃæÏò¹«ÖÚµÄWebÓ¦Óã¬ÔÚÕâ¸öÓ¦ÓÃÖÐÓÐЩÓû§»á´´½¨¶à¸öÕ˺š£ÀýÈ磬Óû§Jeff Lebowski¿ÉÄÜ»áÓÐÁ½¸öÕË»§thedude@example.comºÍlebowski@example.com¡£ºÍÆäËûJava WebÓ¦ÓÃÒ»Ñù£¬ÎÒÃÇ»áʹÓÃHttpSessionÀ´¸ú×ÙÓ¦ÓõÄ״̬£¬È統ǰµÇ¼µÄÓû§¡£ËùÒÔ£¬µ±Óû§Ï£Íû´Óthedude@example.comÇл»µ½lebowski@example.comʱ£¬Ëû±ØÐëÒªÊ×ÏÈÍ˳ö£¬È»ºóÔÙÖØеǼ»ØÀ´¡£

½èÖúSpring Session£¬ÎªÃ¿¸öÓû§ÅäÖöà¸öHTTP session»á·Ç³£ÈÝÒ×£¬ÕâÑùÓû§ÔÚthedude@example.comºÍlebowski@example.comÖ®¼äÇл»µÄʱºò£¬¾Í²»ÐèÒªÍ˳öºÍÖØеǼÁË¡£

¶à¼¶±ðµÄ°²È«Ô¤ÀÀ

¼ÙÉèÎÒÃÇÕýÔÚ¹¹½¨µÄWebÓ¦ÓÃÓÐÒ»¸ö¸´ÔÓ¡¢×Ô¶¨ÒåµÄȨÏÞ¹¦ÄÜ£¬ÆäÖÐÓ¦ÓõÄUI»á»ùÓÚÓû§ËùÊÚÓèµÄ½ÇÉ«ºÍȨÏÞʵÏÖ×ÔÊÊÓ¦¡£

ÀýÈ磬¼ÙÉèÓ¦ÓÃÓÐËĸö°²È«¼¶±ð£ºpublic¡¢confidential¡¢secretºÍtop secret¡£µ±Óû§µÇ¼ӦÓÃÖ®ºó£¬ÏµÍ³»áÅжÏÓû§Ëù¾ßÓеÄ×î¸ß°²È«¼¶±ð²¢ÇÒÖ»»áÏÔʾ¸Ã¼¶±ðºÍ¸Ã¼¶±ð֮ϵÄÊý¾Ý¡£ËùÒÔ£¬¾ßÓÐpublicȨÏÞµÄÓû§Ö»ÄÜ¿´µ½public¼¶±ðµÄÎĵµ£¬¾ßÓÐsecretȨÏÞµÄÓû§Äܹ»¿´µ½public¡¢confidentialºÍsecret¼¶±ðµÄÎĵµ£¬ÖîÈç´ËÀࡣΪÁ˱£Ö¤Óû§½çÃæ¸ü¼ÓÓѺã¬Ó¦ÓóÌÐòÓ¦¸ÃÔÊÐíÓû§Ô¤ÀÀÔڽϵ͵ݲȫ¼¶±ðÌõ¼þÏÂÒ³ÃæÊÇʲôÑù×ӵġ£ÀýÈ磬top secretȨÏÞµÄÓû§Äܹ»½«Ó¦ÓôÓtop secretģʽÇл»µ½secretģʽ£¬ÕâÑù¾ÍÄÜÕ¾ÔÚ¾ßÓÐsecretȨÏÞÓû§µÄÊÓ½ÇÉÏ£¬²é¿´Ó¦ÓÃÊÇʲôÑù×ӵġ£

µäÐ͵ÄWebÓ¦ÓûὫµ±Ç°Óû§µÄ±êʶ¼°Æä½ÇÉ«±£´æÔÚHTTP sessionÖУ¬µ«ÒòΪÔÚWebÓ¦ÓÃÖУ¬Ã¿¸öµÇ¼µÄÓû§Ö»ÄÜÓÐÒ»¸ösession£¬Òò´Ë³ýÁËÓû§Í˳ö²¢ÖØеǼ½øÀ´£¬ÎÒÃDz¢Ã»Óа취ÔÚ½ÇÉ«Ö®¼ä½øÐÐÇл»£¬³ý·ÇÎÒÃÇΪÿ¸öÓû§×ÔÐÐʵÏÖ¶à¸ösessionµÄ¹¦ÄÜ¡£

½èÖúSpring Session£¬¿ÉÒÔºÜÈÝÒ×µØΪÿ¸öµÇ¼Óû§´´½¨¶à¸ösession£¬ÕâЩsessionÖ®¼äÊÇÍêÈ«¶ÀÁ¢µÄ£¬Òò´ËʵÏÖÉÏÊöµÄÔ¤ÀÀ¹¦ÄÜÊǷdz£ÈÝÒ׵ġ£ÀýÈ磬µ±Ç°Óû§ÒÔtop secret½ÇÉ«½øÐÐÁ˵Ǽ£¬ÄÇôӦÓÿÉÒÔ´´½¨Ò»¸öеÄsession£¬Õâ¸ösessionµÄ×î¸ß°²È«½ÇÉ«ÊÇsecret¶ø²»ÊÇtop secret£¬ÕâÑùµÄ»°£¬Óû§¾Í¿ÉÒÔÔÚsecretģʽԤÀÀÓ¦ÓÃÁË¡£

µ±Ê¹ÓÃWeb SocketµÄʱºò±£³ÖµÇ¼״̬

¼ÙÉèÓû§µÇ¼ÁËexample.comÉϵÄWebÓ¦Óã¬ÄÇôËûÃÇ¿ÉÒÔʹÓÃHTML5µÄchat¿Í»§¶ËʵÏÖÁÄÌìµÄ¹¦ÄÜ£¬Õâ¸ö¿Í»§¶Ë¹¹½¨ÔÚwebsocketÖ®ÉÏ¡£°´ÕÕservlet¹æ·¶£¬Í¨¹ýwebsocket´«ÈëµÄÇëÇó²¢²»Äܱ£³ÖHTTP session´¦ÓÚ»îԾ״̬£¬ËùÒÔµ±Óû§ÔÚÁÄÌìµÄ¹ý³ÌÖУ¬HTTP sessionµÄµ¹Êý¼ÆʱÆ÷»áÔÚ²»¶ÏµØÁ÷ÊÅ¡£¼´±ãÕ¾ÔÚÓû§µÄÁ¢³¡ÉÏ£¬ËûÃÇÒ»Ö±ÔÚʹÓÃÓ¦ÓóÌÐò£¬HTTP session×îÖÕÒ²¿ÉÄÜ»á³öÏÖ¹ýÆÚ¡£µ±HTTP session¹ýÆÚʱ£¬websocketÁ¬½Ó½«»á¹Ø±Õ¡£

½èÖúSpring Session£¬¶ÔÓÚϵͳÖеÄÓû§£¬ÎÒÃÇÄܹ»ºÜÈÝÒ×µØʵÏÖwebsocketÇëÇóºÍ³£¹æµÄHTTPÇëÇó¶¼Äܱ£³ÖHTTP session´¦ÓÚ»îԾ״̬¡£

·ÇWebÇëÇó·ÃÎÊSessionÊý¾Ý

¼ÙÉèÎÒÃǵÄÓ¦ÓÃÌṩÁËÁ½ÖÖ·ÃÎÊ·½Ê½£ºÒ»ÖÖʹÓûùÓÚHTTPµÄREST API£¬¶øÁíÒ»ÖÖʹÓûùÓÚRabbitMQµÄAMQPÏûÏ¢¡£Ö´ÐÐÏûÏ¢´¦Àí´úÂëµÄÏ߳̽«ÎÞ·¨·ÃÎÊÓ¦Ó÷þÎñÆ÷µÄHttpSession£¬ËùÒÔÎÒÃDZØÐëÒªÒÔÒ»ÖÖ×Ô¶¨ÒåµÄ·½°¸À´»ñÈ¡HTTP sessionÖеÄÊý¾Ý£¬ÕâҪͨ¹ý×Ô¶¨ÒåµÄ»úÖÆÀ´ÊµÏÖ¡£

ͨ¹ýʹÓÃSpring Session£¬Ö»ÒªÎÒÃÇÄܹ»ÖªµÀsessionµÄid£¬¾Í¿ÉÒÔÔÚÓ¦ÓõÄÈÎÒâÏß³ÌÖзÃÎÊSpring Session¡£Òò´Ë£¬Spring Session¾ß±¸±ÈServlet HTTP session¹ÜÀíÆ÷¸üΪ·á¸»µÄAPI£¬Ö»ÒªÖªµÀÁËsession id£¬ÎÒÃǾÍÄÜ»ñÈ¡ÈÎÒâÌض¨µÄsession¡£ÀýÈ磬ÔÚÒ»¸ö´«ÈëµÄÏûÏ¢ÖпÉÄÜ»á°üº¬Óû§idµÄheaderÐÅÏ¢£¬½èÖúËü£¬ÎÒÃǾͿÉÒÔÖ±½Ó»ñÈ¡sessionÁË¡£

Spring SessionÊÇÈçºÎÔËÐеÄ

ÎÒÃÇÒѾ­ÌÖÂÛÁËÔÚ´«Í³µÄÓ¦Ó÷þÎñÆ÷ÖУ¬HTTP session¹ÜÀí´æÔÚ²»×ãµÄ¸÷ÖÖ³¡¾°£¬½ÓÏÂÀ´¿´Ò»ÏÂSpring SessionÊÇÈçºÎ½â¾öÕâЩÎÊÌâµÄ¡£

Spring SessionµÄ¼Ü¹¹

µ±ÊµÏÖsession¹ÜÀíÆ÷µÄʱºò£¬ÓÐÁ½¸ö±ØÐëÒª½â¾öµÄºËÐÄÎÊÌâ¡£Ê×ÏÈ£¬ÈçºÎ´´½¨¼¯Èº»·¾³Ï¸߿ÉÓõÄsession£¬ÒªÇóÄܹ»¿É¿¿²¢¸ßЧµØ´æ´¢Êý¾Ý¡£Æä´Î£¬²»¹ÜÇëÇóÊÇHTTP¡¢WebSocket¡¢AMQP»¹ÊÇÆäËûµÄЭÒ飬¶ÔÓÚ´«ÈëµÄÇëÇó¸ÃÈçºÎÈ·¶¨¸ÃÓÃÄĸösessionʵÀý¡£ÊµÖÊÉÏ£¬¹Ø¼üÎÊÌâÔÚÓÚ£ºÔÚ·¢ÆðÇëÇóµÄЭÒéÉÏ£¬session id¸ÃÈçºÎ½øÐд«Ê䣿

Spring SessionÈÏΪµÚÒ»¸öÎÊÌ⣬Ҳ¾ÍÊÇÔڸ߿ÉÓÿÉÀ©Õ¹µÄ¼¯ÈºÖд洢Êý¾ÝÒѾ­Í¨¹ý¸÷ÖÖÊý¾Ý´æ´¢·½°¸µÃµ½Á˽â¾ö£¬ÈçRedis¡¢GemFireÒÔ¼°Apache GeodeµÈµÈ£¬Òò´Ë£¬Spring Session¶¨ÒåÁËÒ»×é±ê×¼µÄ½Ó¿Ú£¬¿ÉÒÔͨ¹ýʵÏÖÕâЩ½Ó¿Ú¼ä½Ó·ÃÎʵײãµÄÊý¾Ý´æ´¢¡£Spring Session¶¨ÒåÁËÈçϺËÐĽӿڣºSession¡¢ExpiringSessionÒÔ¼°SessionRepository£¬Õë¶Ô²»Í¬µÄÊý¾Ý´æ´¢£¬ËüÃÇÐèÒª·Ö±ðʵÏÖ¡£

org.springframework.session.Session½Ó¿Ú¶¨ÒåÁËsessionµÄ»ù±¾¹¦ÄÜ£¬ÈçÉèÖúÍÒƳýÊôÐÔ¡£Õâ¸ö½Ó¿Ú²¢²»¹ØÐĵײ㼼Êõ£¬Òò´ËÄܹ»±Èservlet HttpSessionÊÊÓÃÓÚ¸üΪ¹ã·ºµÄ³¡¾°ÖС£

org.springframework.session.ExpiringSessionÀ©Õ¹ÁËSession½Ó¿Ú£¬ËüÌṩÁËÅжÏsessionÊÇ·ñ¹ýÆÚµÄÊôÐÔ¡£RedisSessionÊÇÕâ¸ö½Ó¿ÚµÄÒ»¸öÑùÀýʵÏÖ¡£

org.springframework.session.SessionRepository¶¨ÒåÁË´´½¨¡¢±£´æ¡¢É¾³ýÒÔ¼°¼ìË÷sessionµÄ·½·¨¡£½«SessionʵÀýÕæÕý±£´æµ½Êý¾Ý´æ´¢µÄÂß¼­ÊÇÔÚÕâ¸ö½Ó¿ÚµÄʵÏÖÖбàÂëÍê³ÉµÄ¡£ÀýÈ磬RedisOperationsSessionRepository¾ÍÊÇÕâ¸ö½Ó¿ÚµÄÒ»¸öʵÏÖ£¬Ëü»áÔÚRedisÖд´½¨¡¢´æ´¢ºÍɾ³ýsession¡£

Spring SessionÈÏΪ½«ÇëÇóÓëÌض¨µÄsessionʵÀý¹ØÁªÆðÀ´µÄÎÊÌâÊÇÓëЭÒéÏà¹ØµÄ£¬ÒòΪÔÚÇëÇó/ÏìÓ¦ÖÜÆÚÖУ¬¿Í»§¶ËºÍ·þÎñÆ÷Ö®¼äÐèҪЭÉÌͬÒâÒ»ÖÖ´«µÝsession idµÄ·½Ê½¡£ÀýÈ磬Èç¹ûÇëÇóÊÇͨ¹ýHTTP´«µÝ½øÀ´µÄ£¬ÄÇôsession¿ÉÒÔͨ¹ýHTTP cookie»òHTTP HeaderÐÅÏ¢ÓëÇëÇó½øÐйØÁª¡£Èç¹ûʹÓÃHTTPSµÄ»°£¬ÄÇô¿ÉÒÔ½èÖúSSL session idʵÏÖÇëÇóÓësessionµÄ¹ØÁª¡£Èç¹ûʹÓÃJMSµÄ»°£¬ÄÇôJMSµÄHeaderÐÅÏ¢Äܹ»ÓÃÀ´´æ´¢ÇëÇóºÍÏìÓ¦Ö®¼äµÄsession id¡£

¶ÔÓÚHTTPЭÒéÀ´Ëµ£¬Spring Session¶¨ÒåÁËHttpSessionStrategy½Ó¿ÚÒÔ¼°Á½¸öĬÈÏʵÏÖ£¬¼´CookieHttpSessionStrategyºÍHeaderHttpSessionStrategy£¬ÆäÖÐÇ°ÕßʹÓÃHTTP cookie½«ÇëÇóÓësession id¹ØÁª£¬¶øºóÕßʹÓÃHTTP header½«ÇëÇóÓësession¹ØÁª¡£

ÈçϵÄÕ½ÚÏêϸ²ûÊöÁËSpring SessionʹÓÃHTTPЭÒéµÄϸ½Ú¡£

ÔÚ׫д±¾ÎĵÄʱºò£¬ÔÚµ±Ç°µÄSpring Session 1.0.2 GA·¢²¼°æ±¾ÖУ¬°üº¬ÁËSpring SessionʹÓÃRedisµÄʵÏÖ£¬ÒÔ¼°»ùÓÚMapµÄʵÏÖ£¬Õâ¸öʵÏÖÖ§³ÖÈÎÒâµÄ·Ö²¼Ê½Map£¬ÈçHazelcast¡£ÈÃSpring SessionÖ§³ÖijÖÖÊý¾Ý´æ´¢ÊÇÏ൱ÈÝÒ׵ģ¬ÏÖÔÚÓÐÖ§³Ö¸÷ÖÖÊý¾Ý´æ´¢µÄÉçÇøʵÏÖ¡£

Spring Session¶ÔHTTPµÄÖ§³Ö

Spring Session¶ÔHTTPµÄÖ§³ÖÊÇͨ¹ý±ê×¼µÄservlet filterÀ´ÊµÏֵģ¬Õâ¸öfilter±ØÐëÒªÅäÖÃΪÀ¹½ØËùÓеÄwebÓ¦ÓÃÇëÇ󣬲¢ÇÒËüÓ¦¸ÃÊÇfilterÁ´ÖеĵÚÒ»¸öfilter¡£Spring Session filter»áÈ·±£Ëæºóµ÷ÓÃjavax.servlet.http.HttpServletRequestµÄgetSession()·½·¨Ê±£¬¶¼»á·µ»ØSpring SessionµÄHttpSessionʵÀý£¬¶ø²»ÊÇÓ¦Ó÷þÎñÆ÷ĬÈϵÄHttpSession¡£

Èç¹ûÒªÀí½âËüµÄ»°£¬×î¼òµ¥µÄ·½Ê½¾ÍÊDz鿴Spring Sessionʵ¼ÊËùʹÓõÄÔ´Âë¡£Ê×ÏÈ£¬ÎÒÃÇÁ˽âһϱê×¼servletÀ©Õ¹µãµÄһЩ±³¾°ÖªÊ¶£¬ÔÚʵÏÖSpring SessionµÄʱºò»áʹÓÃÕâЩ֪ʶ¡£

ÔÚ2001Ä꣬Servlet 2.3¹æ·¶ÒýÈëÁËServletRequestWrapper¡£ËüµÄjavadocÎĵµÕâÑùдµÀ£¬ServletRequestWrapper¡°ÌṩÁËServletRequest½Ó¿ÚµÄ±ãÀûʵÏÖ£¬¿ª·¢ÈËÔ±Èç¹ûÏ£Íû½«ÇëÇóÊÊÅäµ½ServletµÄ»°£¬¿ÉÒÔ±àдËüµÄ×ÓÀà¡£Õâ¸öÀàʵÏÖÁË°ü×°£¨Wrapper£©»òÕß˵ÊÇ×°ÊΣ¨Decorator£©Ä£Ê½¡£¶Ô·½·¨µÄµ÷ÓÃĬÈÏ»áͨ¹ý°ü×°µÄÇëÇó¶ÔÏóÀ´Ö´ÐС±¡£ÈçϵĴúÂëÑùÀý³éÈ¡×ÔTomcat£¬Õ¹ÏÖÁËServletRequestWrapperÊÇÈçºÎʵÏֵġ£

public class ServletRequestWrapper implements ServletRequest {

private ServletRequest request;

/**
* ´´½¨ServletRequestÊÊÅäÆ÷£¬Ëü°ü×°Á˸ø¶¨µÄÇëÇó¶ÔÏó¡£
* @throws java.lang.IllegalArgumentException if the request is null
*/
public ServletRequestWrapper(ServletRequest request) {
if (request == null) {
throw new IllegalArgumentException("Request cannot be null");
}
this.request = request;
}

public ServletRequest getRequest() {
return this.request;
}

public Object getAttribute(String name) {
return this.request.getAttribute(name);
}

// ΪÁ˱£Ö¤¿É¶ÁÐÔ£¬ÆäËûµÄ·½·¨É¾¼õµôÁË
}

Servlet 2.3¹æ·¶»¹¶¨ÒåÁËHttpServletRequestWrapper£¬ËüÊÇServletRequestWrapperµÄ×ÓÀ࣬Äܹ»¿ìËÙÌṩHttpServletRequestµÄ×Ô¶¨ÒåʵÏÖ£¬ÈçϵĴúÂëÊÇ´ÓTomcat³éÈ¡³öÀ´µÄ£¬Õ¹ÏÖÁËHttpServletRequesWrapperÀàÊÇÈçºÎÔËÐеġ£

public class HttpServletRequestWrapper extends ServletRequestWrapper 
implements HttpServletRequest {

public HttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}

private HttpServletRequest _getHttpServletRequest() {
return (HttpServletRequest) super.getRequest();
}

public HttpSession getSession(boolean create) {
return this._getHttpServletRequest().getSession(create);
}

public HttpSession getSession() {
return this._getHttpServletRequest().getSession();
}
// ΪÁ˱£Ö¤¿É¶ÁÐÔ£¬ÆäËûµÄ·½·¨É¾¼õµôÁË
}

ËùÒÔ£¬½èÖúÕâЩ°ü×°Àà¾ÍÄܱàд´úÂëÀ´À©Õ¹HttpServletRequest£¬ÖØÔØ·µ»ØHttpSessionµÄ·½·¨£¬ÈÃËü·µ»ØÓÉÍⲿ´æ´¢ËùÌṩµÄʵÏÖ¡£ÈçϵĴúÂëÊÇ´ÓSpring SessionÏîÄ¿ÖÐÌáÈ¡³öÀ´µÄ£¬µ«ÊÇÎÒ½«Ô­À´µÄ×¢ÊÍÌ滻ΪÎÒ×Ô¼ºµÄ×¢ÊÍ£¬ÓÃÀ´ÔÚ±¾ÎÄÖнâÊÍ´úÂ룬ËùÒÔÔÚÔĶÁÏÂÃæµÄ´úÂëƬ¶Îʱ£¬ÇëÁôÒâ×¢ÊÍ¡£

/*
* ×¢Ò⣬Spring SessionÏîÄ¿¶¨ÒåÁËÀ©Õ¹×Ô
* ±ê×¼HttpServletRequestWrapperµÄÀ࣬ÓÃÀ´ÖØÔØ
* HttpServletRequestÖÐÓësessionÏà¹ØµÄ·½·¨¡£
*/
private final class SessionRepositoryRequestWrapper
extends HttpServletRequestWrapper {

private HttpSessionWrapper currentSession;
private Boolean requestedSessionIdValid;
private boolean requestedSessionInvalidated;
private final HttpServletResponse response;
private final ServletContext servletContext;

/*
* ×¢Ò⣬Õâ¸ö¹¹ÔìÆ÷·Ç³£¼òµ¥£¬Ëü½ÓÊÜÉÔºó»áÓõ½µÄ²ÎÊý£¬
* ²¢ÇÒίÍиøËüËùÀ©Õ¹µÄHttpServletRequestWrapper
*/
private SessionRepositoryRequestWrapper(
HttpServletRequest request,
HttpServletResponse response,
ServletContext servletContext) {
super(request);
this.response = response;
this.servletContext = servletContext;
}

/*
* ÔÚÕâÀSpring SessionÏîÄ¿²»ÔÙ½«µ÷ÓÃίÍиø
* Ó¦Ó÷þÎñÆ÷£¬¶øÊÇʵÏÖ×Ô¼ºµÄÂß¼­£¬
* ·µ»ØÓÉÍⲿÊý¾Ý´æ´¢×÷Ϊ֧³ÅµÄHttpSessionʵÀý¡£
*
* »ù±¾µÄʵÏÖÊÇ£¬Ïȼì²éÊDz»ÊÇÒѾ­ÓÐsessionÁË¡£Èç¹ûÓеĻ°£¬
* ¾Í½«Æä·µ»Ø£¬·ñÔòµÄ»°£¬Ëü»á¼ì²éµ±Ç°µÄÇëÇóÖÐÊÇ·ñÓÐsession id¡£
* Èç¹ûÓеĻ°£¬½«»á¸ù¾ÝÕâ¸ösession id£¬´ÓËüµÄSessionRepositoryÖмÓÔØsession¡£
* Èç¹ûsession repositoryÖÐûÓÐsession£¬»òÕßÔÚµ±Ç°ÇëÇóÖУ¬
* ûÓе±Ç°session idÓëÇëÇó¹ØÁªµÄ»°£¬
* ÄÇôËü»á´´½¨Ò»¸öеÄsession£¬²¢½«Æä³Ö¾Ã»¯µ½session repositoryÖС£
*/
@Override
public HttpSession getSession(boolean create) {
if(currentSession != null) {
return currentSession;
}
String requestedSessionId = getRequestedSessionId();
if(requestedSessionId != null) {
S session = sessionRepository.getSession(requestedSessionId);
if(session != null) {
this.requestedSessionIdValid = true;
currentSession = new HttpSessionWrapper(session, getServletContext());
currentSession.setNew(false);
return currentSession;
}
}
if(!create) {
return null;
}
S session = sessionRepository.createSession();
currentSession = new HttpSessionWrapper(session, getServletContext());
return currentSession;
}

@Override
public HttpSession getSession() {
return getSession(true);
}
}

Spring Session¶¨ÒåÁËSessionRepositoryFilter£¬ËüʵÏÖÁË Servlet Filter½Ó¿Ú¡£ÎÒ³éÈ¡ÁËÕâ¸öfilterµÄ¹Ø¼ü²¿·Ö£¬½«ÆäÁÐÔÚÏÂÃæµÄ´úÂëƬ¶ÎÖУ¬ÎÒ»¹Ìí¼ÓÁËһЩעÊÍ£¬ÓÃÀ´ÔÚ±¾ÎÄÖвûÊöÕâЩ´úÂ룬ËùÒÔ£¬Í¬ÑùµÄ£¬ÇëÔĶÁÏÂÃæ´úÂëµÄ×¢ÊͲ¿·Ö¡£

/*
* SessionRepositoryFilterÖ»ÊÇÒ»¸ö±ê×¼µÄServletFilter£¬
* ËüµÄʵÏÖÀ©Õ¹ÁËÒ»¸öhelper»ùÀà¡£
*/
public class SessionRepositoryFilter < S extends ExpiringSession >
extends OncePerRequestFilter {

/*
* Õâ¸ö·½·¨ÊÇħÁ¦ÕæÕý·¢»Ó×÷Óõĵط½¡£Õâ¸ö·½·¨´´½¨ÁË
* ÎÒÃÇÉÏÎÄËùÊöµÄ·â×°ÇëÇó¶ÔÏóºÍ
* Ò»¸ö·â×°µÄÏìÓ¦¶ÔÏó£¬È»ºóµ÷ÓÃÆäÓàµÄfilterÁ´¡£
* ÕâÀ¹Ø¼üÔÚÓÚµ±Õâ¸öfilterºóÃæµÄÓ¦ÓôúÂëÖ´ÐÐʱ£¬
* Èç¹ûÒª»ñµÃsessionµÄ»°£¬µÃµ½µÄ½«»áÊÇSpring SessionµÄ
* HttpServletSessionʵÀý£¬ËüÊÇÓɺó¶ËµÄÍⲿÊý¾Ý´æ´¢×÷Ϊ֧³ÅµÄ¡£
*/
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {

request.setAttribute(SESSION_REPOSITORY_ATTR, sessionRepository);

SessionRepositoryRequestWrapper wrappedRequest =
new SessionRepositoryRequestWrapper(request,response,servletContext);

SessionRepositoryResponseWrapper wrappedResponse =
new SessionRepositoryResponseWrapper(wrappedRequest, response);

HttpServletRequest strategyRequest =
httpSessionStrategy.wrapRequest(wrappedRequest, wrappedResponse);

HttpServletResponse strategyResponse =
httpSessionStrategy.wrapResponse(wrappedRequest, wrappedResponse);

try {
filterChain.doFilter(strategyRequest, strategyResponse);
} finally {
wrappedRequest.commitSession();
}
}
}

ÎÒÃÇ´ÓÕâÒ»Õ½ڵõ½µÄ¹Ø¼üÐÅÏ¢ÊÇ£¬Spring Session¶ÔHTTPµÄÖ§³ÖËùÒÀ¿¿µÄÊÇÒ»¸ö¼òµ¥ÀÏʽµÄServletFilter£¬½èÖúservlet¹æ·¶Öбê×¼µÄÌØÐÔÀ´ÊµÏÖSpring SessionµÄ¹¦ÄÜ¡£Òò´Ë£¬ÎÒÃÇÄܹ»ÈÃÒÑÓеÄwarÎļþʹÓÃSpring SessionµÄ¹¦ÄÜ£¬¶øÎÞÐèÐÞ¸ÄÒÑÓеĴúÂ룬µ±È»Èç¹ûÄãʹÓÃjavax.servlet.http.HttpSessionListenerµÄ»°£¬¾ÍÁíµ±±ðÂÛÁË¡£Spring Session 1.0²¢²»Ö§³ÖHttpSessionListener £¬µ«ÊÇSpring Session 1.1 M1·¢²¼°æ±¾ÒѾ­Ìí¼ÓÁ˶ÔËüµÄÖ§³Ö£¬Äã¿ÉÒÔͨ¹ý¸ÃµØÖ·Á˽â¸ü¶àϸ½ÚÐÅÏ¢¡£

ÅäÖÃSpring Session

ÔÚWebÏîÄ¿ÖÐÅäÖÃSpring Session·ÖΪËIJ½£º

´î½¨ÓÃÓÚSpring SessionµÄÊý¾Ý´æ´¢

½«Spring SessionµÄjarÎļþÌí¼Óµ½webÓ¦ÓÃÖÐ

½«Spring Session filterÌí¼Óµ½webÓ¦ÓõÄÅäÖÃÖÐ

ÅäÖÃSpring SessionÈçºÎÑ¡ÔñsessionÊý¾Ý´æ´¢µÄÁ¬½Ó

Spring Session×Ô´øÁ˶ÔRedisµÄÖ§³Ö¡£´î½¨ºÍ°²×°redisµÄϸ½Ú¿ÉÒԲο¼¸ÃµØÖ·¡£

ÓÐÁ½ÖÖ³£¼ûµÄ·½Ê½Äܹ»Íê³ÉÉÏÊöµÄSpring SessionÅäÖò½Öè¡£µÚÒ»ÖÖ·½Ê½ÊÇʹÓÃSpring BootÀ´×Ô¶¯ÅäÖÃSpring Session¡£µÚ¶þÖÖÅäÖÃSpring SessionµÄ·½Ê½ÊÇÊÖ¶¯Íê³ÉÉÏÊöµÄÿһ¸öÅäÖò½Öè¡£

½èÖúÏñMaven»òGradleÕâÑùµÄÒÀÀµ¹ÜÀíÆ÷£¬½«Spring SessionÌí¼ÓÓ¦ÓÃÖÐÊǺÜÈÝÒ׵ġ£Èç¹ûÄãʹÓÃMavenºÍSpring BootµÄ»°£¬ÄÇô¿ÉÒÔÔÚpom.xmlÖÐʹÓÃÈçϵÄÒÀÀµ£º

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>

ÆäÖУ¬spring-boot-starter-redisÒÀÀµÄܹ»È·±£Ê¹ÓÃredisËùÐèµÄËùÓÐjar¶¼»á°üº¬ÔÚÓ¦ÓÃÖУ¬ËùÒÔËüÃÇ¿ÉÒÔ½èÖúSpring Boot½øÐÐ×Ô¶¯×°Åä¡£spring-sessionÒÀÀµ½«»áÒýÈë Spring SessionµÄjar¡£

ÖÁÓÚSpring Session Servlet filterµÄÅäÖ㬿ÉÒÔͨ¹ýSpring BootµÄ×Ô¶¯ÅäÖÃÀ´ÊµÏÖ£¬ÕâÖ»ÐèÒªÔÚSpring BootµÄÅäÖÃÀàÉÏʹÓà @EnableRedisHttpSession×¢½â¾Í¿ÉÒÔÁË£¬ÈçÏÂÃæµÄ´úÂëƬ¶ÎËùʾ¡£

@SpringBootApplication
@EnableRedisHttpSession
public class ExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}

ÖÁÓÚSpring Sessionµ½RedisÁ¬½ÓµÄÅäÖ㬿ÉÒÔÌí¼ÓÈçÏÂÅäÖõ½Spring BootµÄapplication.propertiesÎļþÖУº

spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379

Spring BootÌṩÁË´óÁ¿µÄ»ù´¡ÉèÊ©ÓÃÀ´ÅäÖõ½RedisµÄÁ¬½Ó£¬¶¨Òåµ½RedisÊý¾Ý¿âÁ¬½ÓµÄ¸÷ÖÖ·½Ê½¶¼¿ÉÒÔÓÃÔÚÕâÀï¡£Äã¿ÉÒԲο¼¸ÃµØÖ·µÄÖð²½²Ù×÷Ö¸ÄÏ£¬À´Á˽âÈçºÎʹÓÃSpring SessionºÍSpring Boot¡£

ÔÚ´«Í³µÄwebÓ¦ÓÃÖУ¬¿ÉÒԲο¼¸ÃÖ¸ÄÏÀ´Á˽âÈçºÎͨ¹ýweb.xmlÀ´Ê¹ÓÃSpring Session¡£

ÔÚ´«Í³µÄwarÎļþÖУ¬¿ÉÒԲο¼¸ÃÖ¸ÄÏÀ´Á˽âÈçºÎ²»Ê¹ÓÃweb.xml½øÐÐÅäÖá£

ĬÈÏÇé¿öÏ£¬Spring Session»áʹÓÃHTTP cookieÀ´´æ´¢session id£¬µ«ÊÇÎÒÃÇÒ²¿ÉÒÔÅäÖÃSpring SessionʹÓÃ×Ô¶¨ÒåµÄHTTP headerÐÅÏ¢£¬Èçx-auth-token: 0dc1f6e1-c7f1-41ac-8ce2-32b6b3e57aa3£¬µ±¹¹½¨REST APIµÄʱºò£¬ÕâÖÖ·½Ê½ÊǺÜÓÐÓõġ£ÍêÕûµÄÖ¸ÄÏ¿ÉÒԲο¼¸ÃµØÖ·¡£

ʹÓÃSpring Session

Spring SessionÅäÖÃÍê³ÉÖ®ºó£¬ÎÒÃǾͿÉÒÔʹÓñê×¼µÄServlet APIÓëÖ®½»»¥ÁË¡£ÀýÈ磬ÈçϵĴúÂ붨ÒåÁËÒ»¸öservlet£¬ËüʹÓñê×¼µÄServlet session APIÀ´·ÃÎÊsession¡£

@WebServlet("/example")
public class Example extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// ʹÓÃÕý³£µÄservlet API»ñÈ¡session£¬Ôڵײ㣬
// sessionÊÇͨ¹ýSpring SessionµÃµ½µÄ£¬²¢ÇÒ»á´æ´¢µ½Redis»ò
// ÆäËûÄãËùÑ¡ÔñµÄÊý¾ÝÔ´ÖÐ

HttpSession session = request.getSession();
String value = session.getAttribute(a€?someAttributea€?);

}
}

ÿ¸öä¯ÀÀÆ÷¶à¸öSession

Spring Session»áΪÿ¸öÓû§±£Áô¶à¸ösession£¬ÕâÊÇͨ¹ýʹÓÃÃûΪ¡°_s¡±µÄsession±ðÃû²ÎÊýʵÏֵġ£ÀýÈ磬Èç¹ûµ½´ïµÄÇëÇóΪhttp://example.com/doSomething?_s=0 £¬ÄÇôSpring Session½«»á¶ÁÈ¡¡°_s¡±²ÎÊýµÄÖµ£¬²¢Í¨¹ýËüÈ·¶¨Õâ¸öÇëÇóËùʹÓõÄÊÇĬÈÏsession¡£

Èç¹ûµ½´ïµÄÇëÇóÊÇhttp://example.com/doSomething?_s=1µÄ»°£¬ÄÇôSpring Session¾ÍÄÜÖªµÀÕâ¸öÇëÇóËùҪʹÓõÄsession±ðÃûΪ1.Èç¹ûÇëÇóûÓÐÖ¸¶¨¡°_s¡±²ÎÊýµÄ»°£¬ÀýÈçhttp://example.com/doSomething£¬ÄÇôSpring Session½«ÆäÊÓΪʹÓÃĬÈϵÄsession£¬Ò²¾ÍÊÇ˵_s=0¡£

ҪΪij¸öä¯ÀÀÆ÷´´½¨ÐµÄsession£¬Ö»ÐèÒªµ÷ÓÃjavax.servlet.http.HttpServletRequest.getSession()¾Í¿ÉÒÔÁË£¬¾ÍÏñÎÒÃÇͨ³£Ëù×öµÄÄÇÑù£¬Spring Session½«»á·µ»ØÕýÈ·µÄsession»òÕß°´ÕÕ±ê×¼Servlet¹æ·¶µÄÓïÒå´´½¨Ò»¸öеÄsession¡£ÏÂÃæµÄ±í¸ñÃèÊöÁËÕë¶Ôͬһ¸öä¯ÀÀÆ÷´°¿Ú£¬getSession()Ãæ¶Ô²»Í¬urlʱµÄÐÐΪ¡£

ÈçÉÏÃæµÄ±í¸ñËùʾ£¬session±ðÃû²»Ò»¶¨±ØÐëÊÇÕûÐÍ£¬ËüÖ»ÐèÒªÇø±ðÓÚÆäËû·ÖÅä¸øÓû§µÄsession±ðÃû¾Í¿ÉÒÔÁË¡£µ«ÊÇ£¬ÕûÐ͵Äsession±ðÃû¿ÉÄÜÊÇ×îÒ×ÓÚʹÓõģ¬Spring SessionÌṩÁËHttpSessionManager½Ó¿Ú£¬Õâ¸ö½Ó¿Ú°üº¬ÁËһЩʹÓÃsession±ðÃûµÄ¹¤¾ß·½·¨¡£

ÎÒÃÇ¿ÉÒÔÔÚHttpServletRequestÖУ¬Í¨¹ýÃûΪ¡°org.springframework.session.web.http.HttpSessionManager¡±µÄÊôÐÔ»ñÈ¡µ±Ç°µÄHttpSessionManager¡£ÈçϵÄÑùÀý´úÂë²ûÊöÁËÈçºÎµÃµ½HttpSessionManager£¬²¢ÇÒÔÚÑùÀý×¢ÊÍÖÐÃèÊöÁËÆä¹Ø¼ü·½·¨µÄÐÐΪ¡£

@WebServlet("/example")
public class Example extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {

/*
* ÔÚÇëÇóÖУ¬¸ù¾ÝÃûΪorg.springframework.session.web.http.HttpSessionManagerµÄkey
* »ñµÃSpring Session session¹ÜÀíÆ÷µÄÒýÓÃ
*/

HttpSessionManager sessionManager=(HttpSessionManager)request.getAttribute(
"org.springframework.session.web.http.HttpSessionManager");

/*
* ʹÓÃsession¹ÜÀíÆ÷ÕÒ³öËùÇëÇósessionµÄ±ðÃû¡£
* ĬÈÏÇé¿öÏ£¬session±ðÃû»á°üº¬ÔÚurlÖУ¬²¢ÇÒÇëÇó²ÎÊýµÄÃû³ÆΪ¡°_s¡±¡£
* ÀýÈ磬http://localhost:8080/example?_s=1
* ½«»áʹÈçϵĴúÂë´òÓ¡³ö¡°Requested Session Alias is: 1¡±
*/
String requestedSessionAlias=sessionManager.getCurrentSessionAlias(request);
System.out.println("Requested Session Alias is: " + requestedSessionAlias);

/* ·µ»ØÒ»¸öΨһµÄsession±ðÃûid£¬Õâ¸ö±ðÃûĿǰûÓб»ä¯ÀÀÆ÷ÓÃÀ´·¢ËÍÇëÇó¡£
* Õâ¸ö·½·¨²¢²»»á´´½¨ÐµÄsession£¬
* ÎÒÃÇÐèÒªµ÷ÓÃrequest.getSession()À´´´½¨ÐÂsession¡£
*/
String newSessionAlias = sessionManager.getNewSessionAlias(request);

/* ʹÓÃд´½¨µÄsession±ðÃûÀ´½¨Á¢URL£¬Õâ¸öURL½«»á°üº¬
* ¡°_s¡±²ÎÊý¡£ÀýÈ磬Èç¹ûnewSessionAliasµÄֵΪ2µÄ»°£¬
* ÄÇôÈçϵķ½·¨½«»á·µ»Ø¡°/inbox?_s=2¡±
*/

String encodedURL = sessionManager.encodeURL("/inbox", newSessionAlias);
System.out.println(encodedURL);

/* ·µ»Øsession±ðÃûÓësession idËù×é³ÉµÄMap£¬
* ËüÃÇÊÇÓÉä¯ÀÀÆ÷·¢ËÍÇëÇóËùÐγɵġ£
*/
Map < String, String > sessionIds = sessionManager.getSessionIds(request);
}
}

½áÂÛ

Spring SessionΪÆóÒµ¼¶JavaµÄsession¹ÜÀí´øÀ´Á˸ïУ¬Ê¹µÃÈçϵÄÈÎÎñ±äµÃ¸ü¼ÓÈÝÒ×£º

±àд¿ÉˮƽÀ©Õ¹µÄÔ­ÉúÔÆÓ¦Óá£

½«sessionËù±£´æµÄ״̬жÔص½Ìض¨µÄÍⲿsession´æ´¢ÖУ¬ÈçRedis»òApache GeodeÖУ¬ËüÃÇÄܹ»ÒÔ¶ÀÁ¢ÓÚÓ¦Ó÷þÎñÆ÷µÄ·½Ê½Ìṩ¸ßÖÊÁ¿µÄ¼¯Èº¡£

µ±Óû§Ê¹ÓÃWebSocket·¢ËÍÇëÇóµÄʱºò£¬Äܹ»±£³ÖHttpSession´¦ÓÚ»îԾ״̬¡£

ÔÚ·ÇWebÇëÇóµÄ´¦Àí´úÂëÖУ¬Äܹ»·ÃÎÊsessionÊý¾Ý£¬±ÈÈçÔÚJMSÏûÏ¢µÄ´¦Àí´úÂëÖС£

Ö§³Öÿ¸öä¯ÀÀÆ÷ÉÏʹÓöà¸ösession£¬ÕâÑù¾Í¿ÉÒÔºÜÈÝÒ׵ع¹½¨¸ü¼Ó·á¸»µÄÖÕ¶ËÓû§ÌåÑé¡£

¿ØÖÆ¿Í»§¶ËºÍ·þÎñÆ÷¶ËÖ®¼äÈçºÎ½øÐÐsession idµÄ½»»»£¬ÕâÑù¸ü¼ÓÒ×ÓÚ±àдRestful API£¬ÒòΪËü¿ÉÒÔ´ÓHTTP Í·ÐÅÏ¢ÖлñÈ¡session id£¬¶ø²»±ØÔÙÒÀÀµÓÚcookie¡£

Èç¹ûÄãÏëÅ×Æú´«Í³µÄÖØÁ¿¼¶Ó¦Ó÷þÎñÆ÷£¬µ«ÊÜÖÆÓÚÒѾ­Ê¹ÓÃÁËÕâЩӦÓ÷þÎñÆ÷µÄsession¼¯ÈºÌØÐÔ£¬ÄÇôSpring Session½«ÊÇ°ïÖúÄãÂõÏò¸ü¼ÓÇáÁ¿¼¶ÈÝÆ÷µÄÖØÒªÒ»²½£¬ÕâЩÇáÁ¿¼¶µÄÈÝÆ÷°üÀ¨Tomcat¡¢Jetty»òUndertow¡£

   
1474 ´Îä¯ÀÀ       20
Ïà¹ØÎÄÕÂ

Java΢·þÎñÐÂÉú´úÖ®Nacos
ÉîÈëÀí½âJavaÖеÄÈÝÆ÷
JavaÈÝÆ÷Ïê½â
Java´úÂëÖÊÁ¿¼ì²é¹¤¾ß¼°Ê¹Óð¸Àý
Ïà¹ØÎĵµ

JavaÐÔÄÜÓÅ»¯
Spring¿ò¼Ü
SSM¿ò¼Ü¼òµ¥¼òÉÜ
´ÓÁ㿪ʼѧjava±à³Ì¾­µä
Ïà¹Ø¿Î³Ì

¸ßÐÔÄÜJava±à³ÌÓëϵͳÐÔÄÜÓÅ»¯
JavaEE¼Ü¹¹¡¢ Éè¼Æģʽ¼°ÐÔÄܵ÷ÓÅ
Java±à³Ì»ù´¡µ½Ó¦Óÿª·¢
JAVAÐéÄâ»úÔ­ÀíÆÊÎö
×îл¼Æ»®
LLM´óÄ£ÐÍÓ¦ÓÃÓëÏîÄ¿¹¹½¨ 12-26[ÌØ»Ý]
QTÓ¦Óÿª·¢ 11-21[ÏßÉÏ]
C++¸ß¼¶±à³Ì 11-27[±±¾©]
ÒµÎñ½¨Ä£&ÁìÓòÇý¶¯Éè¼Æ 11-15[±±¾©]
Óû§Ñо¿ÓëÓû§½¨Ä£ 11-21[±±¾©]
SysMLºÍEA½øÐÐϵͳÉè¼Æ½¨Ä£ 11-28[±±¾©]

Java ÖеÄÖÐÎıàÂëÎÊÌâ
Java»ù´¡ÖªÊ¶µÄÈýÊ®¸ö¾­µäÎÊ´ð
Íæת Java Web Ó¦Óÿª·¢
ʹÓÃSpring¸üºÃµØ´¦ÀíStruts
ÓÃEclipse¿ª·¢iPhone WebÓ¦ÓÃ
²å¼þϵͳ¿ò¼Ü·ÖÎö

Struts+Spring+Hibernate
»ùÓÚJ2EEµÄWeb 2.0Ó¦Óÿª·¢
J2EEÉè¼ÆģʽºÍÐÔÄܵ÷ÓÅ
Java EE 5ÆóÒµ¼¶¼Ü¹¹Éè¼Æ
Javaµ¥Ôª²âÊÔ·½·¨Óë¼¼Êõ
Java±à³Ì·½·¨Óë¼¼Êõ

Struts+Spring+Hibernate/EJB+ÐÔÄÜÓÅ»¯
»ªÏÄ»ù½ð ActiveMQ Ô­ÀíÓë¹ÜÀí
ijÃñº½¹«Ë¾ Java»ù´¡±à³Ìµ½Ó¦Óÿª·¢
ij·çµç¹«Ë¾ Java Ó¦Óÿª·¢Æ½Ì¨ÓëǨÒÆ
ÈÕÕÕ¸Û J2EEÓ¦Óÿª·¢¼¼Êõ¿ò¼ÜÓëʵ¼ù
ij¿ç¹ú¹«Ë¾ ¹¤×÷Á÷¹ÜÀíJBPM
¶«·½º½¿Õ¹«Ë¾ ¸ß¼¶J2EE¼°ÆäÇ°Ñؼ¼Êõ