整理Flex获得session的方法

整理Flex获得session的方法

1. 使用Blazeds与JAVA后台通信的

可以通过以下方案获取Session中的信息
FlexContext.getFlexSession().getAttribute(att);


2. Flex Remote Object中直接使用HttpSession的方法
 
问题的背景
Flex Remote Object可以是POJO,JavaBean或是EJB。在面向服务的架构中(Service
Oriented Architecture),我们可以用Remote Object来作为Service Facade,利用应用服务
器提供的persistent service来储存状态信息。
 
Flex既可以提供stateful或stateless的remote object, 另外还有session servlet让mxml
获取/和储存session中的内容。这一切听上去都很完美,但是有一个问题,Flex Remote
Object本身是无法获得任何有关Running Context的信息,也就是说,你无法从你的 Remote
Object 中获得
HttpSession, HttpRequest 和 ServletContext。 所谓的 Flex Session servlet只是让
MXML获得session的内容,而不是直接让Remote Object获得session。
 
Remote Object为什么需要获得HttpRequest, HttpSession?
既然Flex提供了stateful的remote object为什么还要让remote object获得Running
Context呢?问题在于Flex中的stateful是基于应用服务器的http session,而且你无法控
制AMFGateway建立remote
object的过程。打个简单的比方,我们知道一般的应用服务器中,session的时限只有20分
钟,而在很多系统的登陆过程中却有选择保持登陆几个月的选项。
 
其具体实现上就是利用cookie来储存id和password hash,通过控制cookie的存活时间来实
现的。而在服务器端,一旦session过期了,则可以从cookie中获得id和password hash重新
登陆一遍,从而达到自动认证用户的目的。
 
如果你的Remote Object无法获得 HttpServletRequest, HttpSession,你就无法实现上述
的情况。另外,对于小型的应用来说,直接在Remote object中获得servlet context并利用
它来储存/获得共享的资源,可以大大降低开发的复杂程度。
 
解决方案
要让Remote Object获得HttpSession,HttpRequest和ServletContext并不是一件容易的事情
。这里提供了我的一种方法,供大家参考。希望能抛砖引玉,让大家提出更好,更有效的方
案。
 
这个方法的基本思路是利用JAVA提供的 ThreadLocal Object。当服务器接收到一个HTTP请
求后,这个请求的整个处理过程是运行在同一个线程中的。
 
每个HTTP请求的处理会都运行在各自独立的线程中。而在Flex中,所有AMF Remote Object
的请求都需要通过 AMF Gateway Servlet,而Remote Object 的建立和调用恰恰就是运行在
这个HTTP请求的线程中。
 
有了这个原则,我们就可以建立一个Context Object,每当请求建立的时候,就可以把这个
请求放入 Context 的 ThreadLocal 中,而当 Remote Object 被AMF Gateway Servlet调用
的时候,就可以通过访问 Context 的ThreadLoca l来获得其所对应的那个请求。
 
而截获发送到AMF Gateway的请求则可以通过Servlet Filter来实现。废话不说了,看代码
吧!
 
 
 
1. 添加以下内容到WEB-INF/web.xml中
 
<filter>
<filter-name>AMFSessionFilter </filter-name>
<filter-class>com.netop.forum.servlets.AMFSessionFilter </filter-class>
<filter>
 
<filter-mapping>
<filter-name>AMFSessionFilter </filter-name>
<servlet-name>AMFGatewayServlet </servlet-name>
<filter-mapping>
 
 
2. AMFSessionFilter的代码
 
 
package com.netop.forum.servlets;
 
 
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
 
 
public class AMFSessionFilter implements Filter
{
private static Log log LogFactory.getLog(AMFSessionFilter.class);
 
public void init(FilterConfig config)
{
log.info("Init AMFSessionFilter filter");
}
 
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException,IOException
{
AMFContext.setCurrentContext((HttpServletRequest)request,
(HttpServletResponse)response);
chain.doFilter(request,response);
}
 
public void destroy()
{
log.info("Destory AMFSessionFilter filter");
}
}
 
 
 
3. AMFContext的代码
 
 
package com.netop.forum.servlets;
 
 
import javax.servlet.*;
import javax.servlet.http.*;
import com.netop.forum.business.*;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
 
public class AMFContext
{
 
 
public final static String CONNECTION_FACTORY_KEY "sqlMapFactory";
 
 
private static Log log LogFactory.getLog(AMFContext.class);
 
 
private static ThreadLocal tl new ThreadLocal();
 
 
static public void setCurrentContext(HttpServletRequest request,
HttpServletResponse response)
{
AMFContext getCurrentContext();
if (c == null)
{
c new AMFContext(request,response);
tl.set(c);
}
else
{
c.setRequest(request);
c.setResponse(response);
}
}
 
 
static public AMFContext getCurrentContext()
{
return (AMFContext)tl.get();
}
 
 
 
 
 
//----------------------------------------------------------
//
// Class members
//
//----------------------------------------------------------
 
 
private HttpServletRequest request;
 
 
private HttpServletResponse response;
 
 
 
private AMFContext (HttpServletRequest request, HttpServletResponse response)
{
this.request request;
this.response response;
}
 
 
 
public HttpServletRequest getRequest()
{
return request;
}
 
 
public void setRequest(HttpServletRequest request)
{
this.request request;
}
 
 
public HttpServletResponse getResponse()
{
return response;
}
 
 
public void setResponse(HttpServletResponse response)
{
this.response response;
}
 
 
public ServletContext getServletContext()
{
HttpSession session this.getSession();
return session.getServletContext();
}
 
 
public HttpSession getSession()
{
return request.getSession();
}
 
 
 
public Object getSessionAttribute(String attr)
{
HttpSession session this.getSession();
return session.getAttribute(attr);
}
 
 
public void setSessionAttribute(String attr, Object value)
{
HttpSession session this.getSession();
session.setAttribute(attr, value);
}
 
 
public Object getContextAttribute(String attr)
{
ServletContext sc this.getServletContext();
return sc.getAttribute(attr);
}
 
 
public void setContextAttribute(String attr, Object value)
{
ServletContext sc this.getServletContext();
sc.setAttribute(attr,value);
}
 
 
public Object getRequestAttribute(String attr)
{
return request.getAttribute(attr);
}
 
 
public void setRequestAttribute(String attr, Object value)
{
request.setAttribute(attr,value);
}
 
 
 
public ConnectionFactory getConnectionFactory()
{
ConnectionFactory factory
=(ConnectionFactory)this.getContextAttribute(CONNECTION_FACTORY_KEY);
if (factory == null)
{
try
{
factory new ConnectionFactory();
// factory is lazy instantiated, so we need to invoke it once to make sure it is
ok.
factory.getSqlMap();
this.setContextAttribute(CONNECTION_FACTORY_KEY, factory);
}
catch (Throwable e)
{
log.fatal("Can not create connection factory: "+e.getMessage(), e);
}
}
return factory;
}
 
}
 
 
4. 如何在remote object中使用AMFContext
 
 
class YouRemoteService
{
public void serviceMethod()
{
AMFContext context AMFContext.getCurrentContext();
HttpSession context.getSession();
ServletContext context.getServletContext();
 
HttpServletRequest request context.getRequest();
HttpServletResponse response context.getResponse();
 
context.setSessionAttribute("attr","value");
context.setContextAttribute("attr","value");
 
}
}

 

再修改一下,使其使用数据库thigr_teach表thuring内注册的账户 <?php session_start(); // 处理登录请求 if ($_SERVER["REQUEST_METHOD"] == "POST") { $servername = "localhost"; $dbname = "thigr_teach"; $dbusername = "your_db_username"; $dbpassword = "your_db_password"; $loginResult = ""; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $username = htmlspecialchars($_POST['username']); // XSS防护 $password = $_POST['password']; $captchaInput = $_POST['captchaInput']; // 验证验证码 if (!isset($_SESSION['captcha']) || $captchaInput != $_SESSION['captcha']) { $loginResult = "<div class='error'>验证码错误,请重试。</div>"; } else { $stmt = $conn->prepare("SELECT password FROM thuring WHERE username = :username"); $stmt->bindParam(':username', $username); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result) { if (password_verify($password, $result['password'])) { $loginResult = "<div class='success'>登录成功!</div>"; } else { $loginResult = "<div class='error'>用户名或密码不正确</div>"; } } else { $loginResult = "<div class='error'>用户名或密码不正确</div>"; } } } catch(PDOException $e) { $loginResult = "<div class='error'>数据库错误: ". htmlspecialchars($e->getMessage())."</div>"; } $conn = null; unset($_SESSION['captcha']); } // 生成新验证码 $captcha = rand(100000, 999999); $_SESSION['captcha'] = $captcha; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> <style> .login-container { max-width: 400px; margin: 50px auto; padding: 20px; border: 1px solid #ddd; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } .form-group { margin-bottom: 15px; } label { display: inline-block; width: 80px; } input[type="text"], input[type="password"] { width: 200px; padding: 8px; border: 1px solid #ddd; border-radius: 4px; } .success { color: green; background: #eaffea; padding: 10px; border-radius: 4px; margin-bottom: 15px; text-align: center; } .error { color: #d8000c; background: #ffd2d2; padding: 10px; border-radius: 4px; margin-bottom: 15px; text-align: center; } .captcha-container { display: flex; align-items: center; gap: 10px; } .captcha-display { font-weight: bold; font-size: 18px; padding: 5px 10px; background: #f5f5f5; border-radius: 4px; } input[type="submit"] { padding: 10px 20px; background: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; } input[type="submit"]:hover { background: #45a049; } </style> </head> <body> <div class="login-container"> <h2 style="text-align:center;">用户登录</h2> <?php // 显示登录结果 if (!empty($loginResult)) { echo $loginResult; } ?> <form action="" method="post"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" id="username" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" required> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" id="password" name="password" required> </div> <div class="form-group"> <label for="captcha">验证码:</label> <div class="captcha-container"> <span class="captcha-display"><?php echo $captcha; ?></span> <input type="text" id="captchaInput" name="captchaInput" placeholder="输入上方验证码" required> </div> </div> <div class="form-group" style="text-align:center;"> <input type="submit" value="登录"> </div> </form> </div> </body> </html>
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值