JAVA结合Wabacus实现Session共享(第二版)

嘉乐集团总公司 9月前 ⋅ 359 阅读

声明:该版相对于第一版有所改进。原因:第一版存在session覆盖问题,分析如下:

request().getServletContext().setAttribute("globelSession", session);

我们可以把globelSession想象成是索引,或某一搜索条件。每一次请求都搜索名为globelSession的session,而globelSession是指定的,每一次set的session都名为globelSession,所以session将会被更新,即:覆盖。

解决办法:浏览器每一次请求所产生的sessionid都不重复,因此,我们可以将这个sessionid想象成主键ID或索引,ID不重复,查询出来的数据也不会是同一条,因此能够有效地避免session覆盖。代码如下:

HttpSession session = request().getSession();
request().getServletContext().setAttribute(session.getId(), session);

一、完整代码

System.out.println("==============================启用SESSION共享===============================");
HttpSession session = request().getSession();
session.setAttribute("userPA01", user.getPA01());
session.setAttribute("userName", user.getUserName());
// 注意这里,要传递sessionID过去,目的:避免session覆盖
request().getServletContext().setAttribute(session.getId(), session);
//request().getServletContext().setAttribute("globelSession",  session);
/*Cookie[] cookies = request().getCookies();
for (Cookie cookie : cookies) {
System.out.println("登录时cookies:"+cookie.getValue());
}*/
System.out.println("================================"+session.getId()+"==============================");

在另一个工程下新建servlet测试类Global.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	ServletContext context = request.getServletContext().getContext("/app2");
	HttpSession session = (HttpSession) context.getAttribute(session.getId());
	System.out.println("跨域获取到的" + session.getAttribute("userPA01"));
}

此时,需要修改web.xml文件,即:请求的入口

<!-- session跨域、共享(测试用) -->
<!-- <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>Global</servlet-name>
  <servlet-class>com.perfect.util.Global</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Global</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping> -->

server.xml配置:

<Context docBase="D:\WorkSpace\WabacusBlank\WebRoot" path="/OA" reloadable="true" crossContext="true" sessionCookiePath="/"/>
<Context docBase="D:\WorkSpace\PerfectOA\target\oa" path="/" reloadable="true" crossContext="true" sessionCookiePath="/"/>

当你启动app1工程并登录后,运行app2即可获取app1共享的session。

二、在wabacus中的用法,必须遵循wabacus的语法

首先,在wabacus.cfg.xml中配置全局拦截器,代码如下:

<!-- 配置全局拦截器 -->
<global-interceptors>
	<interceptor class="com.perfect.interceptor.DataSessionIntercetor"/>
</global-interceptors>

创建拦截器DataSessionIntercetor.java

package com.perfect.interceptor;
 
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import com.wabacus.system.ReportRequest;
import com.wabacus.system.intercept.AbsPageInterceptor;
 
public class DataSessionIntercetor extends AbsPageInterceptor {
	public void doStart(ReportRequest rrequest) {
		System.out.println("您进入拦截器!");
		System.out.println("++++++++++++++++++获取共享session信息如下:++++++++++++++++++");
		ServletContext context = rrequest.getRequest().getServletContext().getContext("/");
//		HttpSession session = (HttpSession) context.getAttribute("globelSession");
		String wabSessionID = rrequest.getRequest().getRequestedSessionId();
		System.out.println("wabacus操作动作产生的sessionid:" + wabSessionID);
		if (wabSessionID.indexOf("-") != -1) {
			rrequest.getRequest().getSession().setAttribute("sessionid", wabSessionID);
			String sessionid = (String)rrequest.getRequest().getSession().getAttribute("sessionid");
			System.out.println("用户登录时产生的sessionid:" + sessionid);
			if (context != null) {
				HttpSession session = (HttpSession) context.getAttribute(sessionid);
				rrequest.getRequest().getSession().setAttribute("userid", session.getAttribute("userPA01"));
				rrequest.getRequest().getSession().setAttribute("username", session.getAttribute("userName"));
				System.out.println("当前用户ID(PA01):" + session.getAttribute("userPA01"));
				System.out.println("当前用户姓名:" + session.getAttribute("userName"));
				rrequest.setAttribute("userid", rrequest.getRequest().getSession().getAttribute("userid"));
				rrequest.setAttribute("username", rrequest.getRequest().getSession().getAttribute("username"));
			}
		}
		/*Cookie[] cookies = rrequest.getRequest().getCookies();
		for (Cookie cookie : cookies) {
			System.out.println("cookies:"+cookie.getValue());
		}*/
	}
 
}

为什么会有wabSessionID.indexOf("-") != -1这样的判断? 因为浏览器访问不能服务时生成的sessionid会有所不同,如下图,每二个id才是登录时产生的sessionid,而第一个则是访问wabacus服务时产生的sessionid 20181127181501355.png 1、在xml页面上的使用

<sql>
	<value>
		<![CDATA[SELECT (SELECT COUNT(*) c FROM A25 WHERE {#condition#}) c,c01,c02 FROM A25 WHERE {#condition#} order by c07 desc]]>
	</value>
	<condition name="userid" label="操作人" hidden="true" source="session{userid}">
		<value>
			<![CDATA[c05 = '#data#']]>
		</value>
	</condition>
	
</sql>

2、在其拦截器中使用

// ***************************当前用户操作**************************** //
String userPA01 = (String) rrequest.getRequest().getSession().getAttribute("userid");
String userName = (String) rrequest.getRequest().getSession().getAttribute("username");
// ****************************************************************** //

全部评论: 0

    我有话说:

    十堰嘉乐软件基地

    微信 扫一扫

    客户服务热线

    0719-400-10010

    在线客服
    支付宝打赏 微信打赏