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

2024-08-31 15:18

本文主要是介绍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等等,这只是其中的一种,希望能够帮到大家,谢谢!

 

这篇关于java结合wabacus实现session共享(第一版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124268

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1