`
bluedusk
  • 浏览: 273408 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Session Management

阅读更多

 

Session Management

 

 

What is a Session?

A session, or more accurately, an HttpSession, is probably the most important object in the entire Servlet API. Its importance cannot be understated.

First of all, an HttpSession object has nothing to do with a Session EJB, so let’s get that association out of your mind right away. It is a completely different monster. An HttpSession is a server-managed component completely independent of the EJB spec.

The Session Issue

Here’s the issue: the http protocol, the mechanism we use to surf the Internet and browse websites, is stateless.  Once a Web server is done handling a client request, the web server completely forgets about what it just did, kinda like that fish in Finding Nemo.

The http protocol treats every request as a brand spanking new, unassociated request, regardless of how many client-server interactions may have happened with a particular client in the past.

A web server, using the http protocol, doesn’t maintain any meaningful state with a client.

What is the impact of a Stateless protocol on our Applications?

A stateless protocol, such as http, causes all sorts of problems for our web-based applications.  For example, if someone surfs to our site and sees a product they want to buy, we want to keep track of that information until the user decides to check out.

Or perhaps a user is taking an online exam: we would want to keep track of every answer the user has provided.  The http protocol provides no mechanism for keeping track of a user’s actions from one invocation to the other.  That’s where the Servlet API, and more specifically, the HttpSession, comes in.

Canada and the http protocol have one thing in common: they are both stateless. Canada has ten provinces and three relatively empty territories, but no states. Of course, they have been eyeing Michigan for a while.

What is the purpose of the HttpSession?

The HttpSession adds state to a stateless, web based, interaction with a server. When a client makes a call to an application server, a Servlet developer can programmatically create and associate an HttpSession with that client. The session can then be used to keep track of all sorts of information about the user.

If the user tells us their favorite color, we can store that information in their session.  If the user gives us their address and phone number, we can store that in their session.  If they’re taking an online exam, we can put the answer to every question they’ve been given into the session as well.  We can then go back into that session object, at any point in time, and pull that information out.

So when a user is done taking an online exam, we can go into their session and find out which questions they got correct, and which questions they got wrong.  If the client is picking out books, or other products they want to purchase, when they click ‘check out,’ we can go into their session, process their order, and tell them how much their purchase will be.

HttpSessions add state to a stateless protocol, and they are pivotal in making online applications work.

How does WebSphere map a session to a client?

So, we’ve got this miraculous component that creates a stateful experience out of a stateless protocol.  The question is, how does WebSphere do it?

Well, when an application server creates a session for a user, a crazy, unique number is generated that immediately gets associated with that user.  Then, any time a developer puts information into a users session, the information is automatically associated with the client’s unique id.  This all happens securely on the server.

In order to tie a client’s browser to their session, WebSphere plants a transient cookie on the client’s. This transient cookie contains that crazy unique number that was generated when the users session was created.

The cookie we place on the client machine contains the client’s unique session id. On every subsequent request made by the client to the server, the cookie containing the session id is sent back across the network, and Voila!, the server ties the user to their session.

What is a transient cookie?

WebSphere plants a transient cookie on the client machine through their web browser. 

A transient cookie is maintained in the memory of the browser, and will never actually be written to a textfile. All other types of cookies actually get stored persistently on the users hard drive.

Because the cookie is resident in memory, when a client closes their browser, the cookie, and subsequently the client’s session, is lost.

Some clients see cookies as a potential security loophole, although no evidence to support this claim has ever come to light.  To appease the security conscious, users can configure their web browsers to accept transient cookies, but not cookies that actually get written to the hard drive.  As a result, a transient cookie will allow sessions to be associated with more clients than using cookies that persist to a users hard drive.

If cookies are turned off, will my applications stop working?

Yes, if cookies are turned off, your WebSphere applications will stop working - with the default WebSphere configuration at least. Cookies are the preferred mechanism for storing the session id on a client machine.

If your clients turn cookies off, your applications won’t work. This may seem detrimental to you architecture, but let me ask you a question: what are you losing if you can’t service clients that are afraid of cookies?

The fact is, if a customer is afraid that a simple little textfile called a cookie is going to wreak havoc on their computer, then what are the odds that you are going to get that paranoid user’s email address, home address, or credit card number?  I’d say the odds are somewhere between zero and negative none.

A fear of cookies is irrational.  You really shouldn’t worry about not being able to service these irrational clients.

 

 

 

 


Are there other options for managing session ids?

WebSphere provides three mechanisms for tying a user to their session:

F  Cookies
F  URLRewriting
F  SSL ID tracking

What is URL rewriting?

URL rewriting is a great session id mapping option because, unlike cookies, it can’t be turned off.  This may sound appealing at first, but it is by no means a panacea for session id tracking.

When you use URL rewriting, the unique session id of a client gets added to the end of every link on a web page.  So instead of a user going to:

F  www.ibm.com/index.html
 they go to:
F  www.ibm.com/index.html?sessionid=123xyz

As you can see, the user’s session id gets attached to the URL this is used when making a subsequent request back to the server.

The good news is that by adding the session id to every link, URL rewriting always works and it can’t be turned off.  The bad news?  Well, the fact that the session id must be added to every link on every web page on every part of your website.

How do you implement URL rewriting?

How do you add that special id to the end of every URL or link a user might click on?  Well, you have to encode the URL, which means writing Java code wherever you see a hyperlink, and turning every single page in your website into a JSP.  After all, Java code won’t work if it’s in a regular HTML page.

So, implementing URL rewriting means extra work for the web development team, and it’s going to place a much greater load on your application server.

Think about it. When you use URL rewriting, you can not allow your users to navigate to a single HTML page within your website.

HTML pages are not dynamic, and if your user navigates to a static HTML page, their session id will be lost. If you have a significant amount of static html content, your application server is going to have to assume the job of generating all of that static content and delivering it to the client with the session id embedded at the end of every internal link that might appear on the web page.  That’s a lot of extra work for the server.

 

How does URL rewriting effect the client?

URL rewriting makes client side navigation much less pleasant.

Imagine a user is about to buy a big ticket item off your website, but just before they do, they go to yahoo.com to check their stock portfolio.  After seeing how well their IBM stock is doing, they then type in your web site address to make their purchase, only to find out that all of the information they had provided about their big order has been destroyed.  That has happened because when they typed your address into the browser, there was no reference to their session id.  If you were using cookies, your client wouldn’t run into this problem.

What is Secure Socket Layer (SSL) encoding?

Because SSL creates a unique id to encrypt all interactions between the client and the server, the application server can piggyback off this SSL id to keep track of the client’s session id.

The two drawbacks to using this is the fact that SSL must be enabled, and the fact that SSL encoding is only supported by the IBM Http Server (IHS) and the SunOne web server. 

If you have a highly secure website, this might be a very feasible option for maintaining session ids. Furthermore, we’ll probably see more web servers supported in the future.

What is Session affinity?

The J2EE spec requires an application server to implement session affinity, also known as ‘sticky sessions.’ 

Implementing workload management means setting up multiple Java Virtual Machines that can handle client requests. In a workload-managed environment, if a client requests a JSP or a Servlet, there may be several JVMs capable of handling the request.

Session affinity states that when a server/JVM creates a session for a user, every subsequent request from that user must be directed back to that server or JVM that initially handled it. A user is tied to the server that created their session, thus the term ‘sticky sessions.’

Managing session data is quite a big job for the application server.  Ensuring that a user is always sent back to the JVM that created the user’s session makes sure that session data can be pulled out of memory quickly and efficiently.

What are the implications of session affinity on workload management and failover?

According to session affinity, if a JVM holding a given user’s session information goes down, all of that user’s session information is lost. It’s kinda weird that they’d have something in the J2EE spec that seems to work against the whole idea of workload management and failover, but believe it or not, it’s in there.

How can we provide session failover with WebSphere?

Fortunately, our beloved WebSphere goes over and above the spec by providing several options for persisting a session beyond the JVM in which it was created. The two supported mechanisms for providing session failover include:

F  Memory to memory session replication
F  Database persistence

Until WebSphere 5, using a centralized database to store session information was the only option available for persisting sessions. By and large, persisting sessions works pretty well.

 

How do persistent sessions work?

The basic idea behind persistent sessions is that whenever a session is created, the application server writes the session id to a special ‘session database.’ Any information subsequently stuffed into a users session gets added to the persistent session database. 

If the server that initially created the users session fails, any other server in the cluster can pick up from where the failed server left off.  If one JVM goes down, any alternate server can look up the client’s information in the session database using the session id that gets passed back to the server through the client’s cookie.  A redundant server can then pull any required session information out of the central persistent database. Any problems on the server are completely transparent, and if something does go wrong, the client never even knows there was a problem.

Of course, this scenario isn’t without its drawbacks.  First of all, persisting sessions requires quite a few database writes, which is resource intensive. Persisting session information to a database is going to slow your applications down a tad. 

Furthermore, the database becomes a central point of failure itself.  If maintaining a robust application server is important to you, you better either use db2, which we all know never fails, or else cluster your database servers.

What is ‘memory to memory’ session replication?”

If persisting sessions to a centralized database isn’t up your alley, WebSphere 5 presents a new option for session failover: memory-to-memory session replication.

The idea behind memory replication is that if servers are clustered to support an application, you can configure the application servers in the cluster to pass session information back and forth from one JVM to the other. Each application server keeps a copy of the session data being used by the other servers in the cluster.

Memory to memory session replication is said to be more efficient than writing to a centralized database, although it does present a few potential bottlenecks itself.

What are the drawbacks of memory to memory session replication?

Memory to memory replication does indeed have its drawbacks. First of all, network traffic may become large if session information is being replicated across a large number of application servers.

Secondly, a significant amount of memory is going to be consumed in order to implement session failover. 

For instance, imagine you have five servers, and each server maintains a gig of session data. That gig must be replicated on each of the other four servers, so each server would need five gigs of memory – one gig for current session data, and four gigs of failover data for each of the four servers in the cluster. That’s a lot of memory!

Of course, those clever sausages at WebSphere do provide a mechanism to address the memory and network traffic dilemma presented by memory-to-memory session replication.

Figure 13-4

Tuning Sessions

A variety of options exists for tuning session replication between cluster members.

How can the use of in memory session replication be optimized for performance?

First of all, WebSphere gives you the ability to configure how often replication occurs. That can minimize the amount of network traffic created, although it does reduce the ability of your cluster to recover from a failover as well.

Secondly, WebSphere allows you to set up a master and slave relationship between application servers. If you had five servers, you could add two servers to be dedicated session masters.  Session data would be replicated to these two masters, and these two masters only. This would significantly reduce network traffic and the memory required to support in memory replication.

What is the default mechanism for managing session failover when you set up a cluster?

By default, a cluster provides no session failover.  Session failover must be configured explicitly through the WebSphere Administrative Console.

Setting up database persistence is a bit of work, but it’s not too painful as long as your application servers can all connect to a common JDBC database.

A big benefit to in-memory session replication, on the other hand, is the fact that is can be easily set up and configured. On new installations, in-memory session replication seems to be the failover mechanism of choice.

It must be noted that session management doesn’t just happen by default.  It must be configured explicitly, and it must be tested as well. Don’t wait until your application server fails to find out if your session replication is working properly.

分享到:
评论
1 楼 niuqiang2008 2011-01-12  
晕 ... 

相关推荐

    5G核心网NEF服务标准规范中英文翻译(29508-g00 Session Management Event Exposure Service.docx)

    #### 2.2 Session Management Event Exposure Service 会话管理事件曝光服务是指NEF提供的服务之一,它使能其他网络功能或外部应用通过API接口获取与会话管理相关的事件信息。这些事件可能包括但不限于会话建立、...

    spring security 3.x session-management 会话管理失效

    - `<session-management>`还可以配置`<session-fixation-protection>`来开启会话固定防护。 通过以上知识点,我们可以构建一个完整的Spring Security项目,实现会话控制、权限验证和免登录功能。参考提供的博文...

    webgoat——Session Management Flaws会话管理缺陷

    在WebGoat的"Session Management Flaws"练习中,展示了如何通过分析和篡改WEAKID参数(一个代表Session ID的变量)来尝试模拟会话劫持。攻击者发现WEAKID的生成规律,尝试通过自动化工具(如Sequencer)模拟这个过程...

    Angular-SessionManagement

    Angular 会话管理安装###Bower 安装 SessionManagement 模块的最简单方法是将 bower 包包含到您的解决方案中bower install symanto-angular-sessionmanagement --save 之后,您只需要将模块和所需的模块添加到您的 ...

    DFC_Session_Management_Whitepape

    ### DFC Session Management详解 #### 一、概述 在深入探讨DFC Session Management之前,我们首先需要理解几个关键概念:**DFC (Documentum Foundation Client)** 和 **Session Management**。 **DFC** 是EMC ...

    Session Management for Emacs-开源

    "Session Management for Emacs"是一个开源项目,旨在帮助用户轻松地管理和恢复Emacs的工作会话。这个插件的核心功能是记录并恢复编辑器的多个方面,包括打开的文件、缓冲区、窗口布局,甚至用户的输入历史记录。...

    3GPP TS 23.501 V16.1.0 5.6 Session Management -中英文对照版V1.pdf

    在UE发起PDU会话建立请求时,如果在NAS(Non-Access Stratum)消息中没有提供DNN,AMF(Access and Mobility Management Function)将根据UE的签约信息来确定PDU会话的DNN。如果签约信息中存在默认DNN,AMF会选择这...

    OWASP Security Shepherd-session management challenge1~4会话管理挑战1~4

    OWASP Security Shepherd是一个旨在教育开发人员和安全测试人员关于Web应用安全的在线平台。该项目通过一系列的挑战来提高用户对应用程序安全缺陷的理解和实践能力。在OWASP Security Shepherd中,会话管理挑战是...

    session失效

    在Web应用开发中,会话管理(Session Management)是一项基本且重要的功能。它用于跟踪用户的会话状态,确保用户在与服务器交互过程中保持登录等状态信息的一致性。Session作为会话管理的一种实现方式,在现代Web...

    WebGoat教程解析

    WebGoat里面关于会话劫持(Hijack a Session)这个课程的标准答案里面除了使用WebScarab以外还使用了其他的工具来找出合法的SessionID以完成这个课程,实际上这个课程完全可以只使用WebScarab来完成。

    springmvc session

    4. **Session Management**: Spring MVC 提供了配置选项来管理 Session,例如设置 Session 的超时时间、是否启用分布式 Session 等。这可以通过 Spring Security 或者 Spring Session 这样的扩展库来实现。 5. **...

    Swift 实现的简单的 Session 管理库.zip

    3. **会话管理(Session Management)**: 会话管理是指在应用程序中跟踪用户状态和行为的过程,通常包括登录状态、用户偏好和数据缓存等。简单Session管理库的目标就是简化这一过程,提供一个封装好的接口来创建、...

    spring4 session共享+redis

    在现代Web应用开发中,会话管理(Session Management)是一个至关重要的部分,它涉及到用户登录状态的维护。Spring框架提供了一种优雅的方式来处理会话共享,特别是在分布式系统中。本教程将详细介绍如何在Spring 4...

    jsp中Action使用session方法实例分析.docx

    在Web开发中,特别是采用Java Server Pages (JSP) 和 Struts 框架进行开发时,会话管理(Session Management)是一项非常重要的功能。Session 使得服务器能够记住用户在不同页面之间的活动,这对于维护用户状态至关...

    shiro-redis-session-master.zip

    Shiro框架的核心组件包括Subject(主体)、Realms(领域)、Cryptography(加密)和Session Management(会话管理)。Subject代表当前操作的用户,而Realms是Shiro与应用程序安全数据交互的桥梁,负责从数据源获取...

    29502-g00_SMF功能接口介绍.docx

    《3GPP TS 29.502 V16.0.0 (2019-06) 技术规范》是3GPP(第三代合作伙伴计划)为5G核心网(5G System)中Session Management Services的第三阶段协议和数据模型制定的标准文档。该文档详细介绍了SMF(Session ...

    php利用mysql保存session的实现思路及示例代码.docx

    在Web开发中,会话管理(Session Management)是一项重要的技术,它可以帮助开发者跟踪用户的活动,保持用户的登录状态,存储用户的偏好设置等信息。传统的会话管理方式是将用户的会话数据存储在服务器端的文件系统...

    ;session.save_path = CDOCUME~1ADMINI~1LOCALS~1Temp

    ### PHP Session Management #### Session Basics PHP中的会话管理是一种用来维护用户会话状态的技术。当用户访问Web应用时,服务器可以通过会话来识别不同的用户并保持其状态信息。会话数据通常包括用户的登录...

    搞懂分布式技术11:分布式session解决方案与一致性hash.docx

    在分布式环境中,**会话管理(Session Management)**是一个重要的议题,尤其是对于需要维护用户状态的应用来说。 ##### 1. 什么是Session? - **定义**:Session是指用户与服务器之间的一次会话过程。在HTTP协议中...

    jwts-not-safe-e-book.pdf

    Unlike traditional session management techniques, JWTs embed all necessary information about the session directly in the token. This approach has several implications: - **Token Expiration:** - ...

Global site tag (gtag.js) - Google Analytics