java结合wabacus实现session共享(第一版)

一、通过页面实现session共享(注:不适用于后台代码)

app1、app2同时部署在D:\apache-tomcat-8080\webapps下,类似于同时运行两个工程,而index1.jsp、index2.jsp分别放在app1、app2文件夹下面,期望:将app1工程的session共享给app2。内容如下:

app1/index1.jsp

<body>
<!-- 设置session值 -->
<%
	session.setAttribute("name", "lenian");
	String names = (String) session.getAttribute("name");
	application.setAttribute("globelSession",session);
%>
	您的用户名是app1:<%=names%>
</body>

app2/index2.jsp

<body>
<!-- 获取session值 -->
您的用户名是application:<%=((HttpSession)application.getContext("/app1").getAttribute("globelSession")).getAttribute("name") %>
</body>

二、spring boot与wabacus的结合(注:即使后台框架变了,原理也不会变)

/**
 * 登录认证
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	UsernamePasswordToken upToken = (UsernamePasswordToken) token;
	String username = upToken.getUsername();
	String password = "";
	if (upToken.getPassword() != null) {
		password = new String(upToken.getPassword());
	}

	User user = null;
	try {
		user = loginService.login(username, password);
		System.out.println("==============================启用SESSION共享(跨域)===============================");
		HttpSession session = request().getSession();
		session.setAttribute("userPA01", user.getPA01());
		session.setAttribute("userName", user.getUserName());
		session.setAttribute("phoneNumber", user.getPhonenumber());
		session.setAttribute("loginName", user.getLoginName());
		session.setAttribute("email", user.getEmail());
		request().getServletContext().setAttribute("globelSession", session);
		HttpSession session2 = (HttpSession)request().getServletContext().getAttribute("globelSession");
		Object id = session2.getAttribute("userPA01");
		System.out.println("======================================="+id+"=========================================");
	} catch (CaptchaException e) {
		throw new AuthenticationException(e.getMessage(), e);
	} catch (Exception e) {
		log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
		throw new AuthenticationException(e.getMessage(), e);
	}
	SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
	return info;
}

必备的方法:

/**
 * new一个request
 * 
 * @return
 */
public static HttpServletRequest request() {
	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
			.getRequest();
	return request;
}

/**
 * 获取当前请求session
 * 
 * @return
 */
public static HttpSession getHttpSession() {
	return request().getSession();
}

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

protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	ServletContext context = request.getServletContext().getContext("/app2");
	HttpSession session = (HttpSession) context.getAttribute("globelSession");
	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> -->

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

三、在wabacus中的用法,必须遵循wabacus的语法。如下:

新建一个拦截器DataSessionIntercetor.java,目的是用wabacus本身的方法set、get。

package com.perfect.interceptor;

import javax.servlet.ServletContext;
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");
		if (null != session) {
			rrequest.getRequest().getSession().setAttribute("userid", session.getAttribute("userPA01"));
			rrequest.getRequest().getSession().setAttribute("username", session.getAttribute("userName"));
			rrequest.getRequest().getSession().setAttribute("phonenumber", session.getAttribute("phoneNumber"));
			rrequest.getRequest().getSession().setAttribute("email", session.getAttribute("email"));
			rrequest.getRequest().getSession().setAttribute("login_name", session.getAttribute("loginName"));
			System.out.println("当前用户ID(PA01):" + session.getAttribute("userPA01"));
			rrequest.setAttribute("userid", rrequest.getRequest().getSession().getAttribute("userid"));
			rrequest.setAttribute("username", rrequest.getRequest().getSession().getAttribute("username"));
			rrequest.setAttribute("phonenumber", rrequest.getRequest().getSession().getAttribute("phonenumber"));
			rrequest.setAttribute("email", rrequest.getRequest().getSession().getAttribute("email"));
			rrequest.setAttribute("login_name", rrequest.getRequest().getSession().getAttribute("login_name"));
		}
		System.out.println("--------------------------------------------------------");
	}
}

在xml页面上的使用方式:

<page id="interview" js="/webresources/script/validate.js,/zhaopin/js/sendData.js" interceptor="com.perfect.interceptor.DataSessionIntercetor">
	<tabpanel id="tp1" margin="10px" titlestyle="2">
		<report id="report1" title="面试待邀约信息" parenttitle="待邀约request{c}" rowselect="checkbox" type="editablelist2">
			<interceptor>
				<imports>
					<import>com.wabacus.util.*</import>
					<import>com.java.util.*</import>
				</imports>
				<preaction>
					<![CDATA[
						rrequest.setAttribute("txtgonghao",rrequest.getRequest().getSession().getAttribute("userid"));
					]]>
				</preaction>
			</interceptor>
			<display></display>
			<sql>
				<value>
					<![CDATA[select (SELECT COUNT(*) c FROM A05 a WHERE c10='待邀约') c,c01,c02,c03,c04,c05,c06,c07,c37,c41 from A05 where {#condition#} and c10='待邀约']]>
				</value>
				<condition name="txtgonghao" label="操作人" hidden="true" source="session{userid}">
					<value>
						<![CDATA[c08 = '#data#']]>
					</value>
				</condition>
			</sql>
			<format></format>
		</report>
	</tabpanel>	
</page>

实现session共享的方法有很多,例如:redis、webservie等等,这只是其中的一种,希望能够帮到大家,谢谢!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度没有我的爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值