使用 SessionRegistry 获取登录用户

2023-11-29

我有一个完全由 Weblogic 容器保护的 Web 应用程序。现在我必须列出当前登录的用户。我必须使用 Spring Security 2.0.4

在 web.xml 中我定义了必要的监听器和过滤器:

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
    </listener>

    <filter>
        <filter-name>Spring Security Filter Chain Proxy</filter-name>
        <filter-class>org.springframework.security.util.FilterToBeanProxy</filter-class>
        <init-param>
            <param-name>targetClass</param-name>
            <param-value>org.springframework.security.util.FilterChainProxy</param-value>
        </init-param> 
    </filter>


    <filter-mapping>
        <filter-name>Spring Security Filter Chain Proxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

之后,我按照我的理解定义了 bean:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<bean id="filterChainProxy"
        class="org.springframework.security.util.FilterChainProxy">
        <property name="filterInvocationDefinitionSource">
            <value>
                CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
                PATTERN_TYPE_APACHE_ANT
                /**=httpSessionIntegrationFilter,logoutFilter,exceptionTranslationFilter,concurrencyFilter
            </value>
        </property>
    </bean>

    <bean id="httpSessionIntegrationFilter"
        class="org.springframework.security.context.HttpSessionContextIntegrationFilter" />

    <bean id="logoutFilter"
        class="org.springframework.security.ui.logout.LogoutFilter">
        <constructor-arg value="/logout.html" />
        <!-- URL redirected to after logout -->
        <constructor-arg>
            <list>
                <bean
                    class="org.springframework.security.ui.logout.SecurityContextLogoutHandler" />
            </list>
        </constructor-arg>
        <property name="filterProcessesUrl" value="/j_acegi_logout" />
    </bean>

    <bean name="concurrencyFilter" class="org.springframework.security.concurrent.ConcurrentSessionFilter">
      <property name="sessionRegistry" ref="sessionRegistryBean"/>
      <property name="expiredUrl" value="/session-expired.htm"/>
    </bean>

    <bean id="authenticationEntryPoint"
        class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
        <property name="loginFormUrl">
            <value>/</value>
        </property>
    </bean>

    <bean id="exceptionTranslationFilter"
        class="org.springframework.security.ui.ExceptionTranslationFilter">
        <property name="authenticationEntryPoint"
            ref="authenticationEntryPoint" />
    </bean>

    <bean id="sessionRegistryBean" class="org.springframework.security.concurrent.SessionRegistryImpl">
    </bean>

</beans>

最后我编写了一个简单的 JSP 页面来列出用户:

  <body>
    <% 

    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
    ActiveSessions as = new ActiveSessions(appContext);
    for(String u : as.getUsers()) {
      %>
        <ul><li><% out.println(u); %></li></ul>
        <% 
    }
    %>
  </body>

以下是我的帮助程序类尝试获取用户列表的方式:

public List<String> getUsers() {
    SessionRegistry sr = (SessionRegistry) a.getBean("sessionRegistryBean");
    Object[] principals = sr.getAllPrincipals();

    List<String> result = new ArrayList<String>();

    for(int i = 0; i < principals.length; i++) {
      SessionInformation[] sis = sr.getAllSessions(principals[i], false);
      result.add(principals[i].toString());
      logger.info("Adding entry: " + principals[i].toString() + ", sessions: " + sis.length);
    }

    return result;
}

不幸的是,这一切都不起作用,我真的不知道如何调试它。我所做的是开始使用应用程序(在容器管理的 BASIC 身份验证之后)并调用 jsp 页面。该列表始​​终为空。


上述配置的问题是没有一个bean 负责将数据放入SessionRegistry。会话事件在应用程序中发布,但需要更多 bean 以便将身份验证放入注册表中,例如AuthenticationManager应该配置。如果没有这个SessionRegistry总是空着。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SessionRegistry 获取登录用户 的相关文章

随机推荐

  • 如何添加动态文本作为任务栏图标叠加?

    我正在尝试将带有文本的任务栏图标覆盖添加到 windows7 应用程序图标 我确实设法添加了小覆盖 但无法添加文本 有谁知道如何添加动态文本作为任务栏图标叠加层 使用 WPF 和 C 您只能添加一个Image所以你必须创建 Rectangl
  • 使用 LAST_INSERT_ID() 和另一个表中的行在 PHP/MySQL 中进行两次插入

    我正在使用 PHP 和 MySQL V5 1 制作一个活动日历 其中管理员将添加一个活动 然后也会创建该活动的出席列表 我有三个表 活动 出席和会员 到目前为止 我可以使用通过 PHP 表单输入的信息创建事件 我正在尝试通过插入刚刚创建的活
  • python 获取表单数据 这是怎么回事?

    这是我的ajax表单提交的js函数 sendButton on click function post send form contactForm serialize 这是我的表格
  • SSIS - 单击“编辑脚本”按钮时没有任何反应

    我有 Visual Studio 2017 SSIS 和 SSRS 直到昨天都工作正常 但现在 当我单击 SSIS 编辑脚本 按钮时 没有任何反应 我已经卸载SSDT并重新安装 但问题仍然存在 Microsoft SqlServer Scr
  • 类似于 let in Ruby

    我曾经编写类似 let 的表达式 具有词法范围 所以我自己写了 悲伤 但它会因多线程而失败 Useful thing for replacing a value of variable only for one block of code
  • 在delphi中为分支声明块级变量

    在Delphi prism中我们可以声明只有在特殊场合才需要的变量 eg In prism If acondition then begin var a Integer end a 3 this line will produce erro
  • 这个语法在 Python 中意味着什么?

    下面声明中的逗号是什么意思 它是否同时定义两个变量 resp content client request request token url GET 它创建了一个tuple 在这种情况下 元组有两个变量 它们被分配结果request re
  • 使用 Swift 格式化日期

    我正在尝试使用 Date Formatter 将字符串转换为日期 var dateFormatter NSDateFormatter dateFormatter dateFormat yyyy MM dd T HH mm ss Z var
  • CSS 对齐文本,用点填充空间[重复]

    这个问题在这里已经有答案了 可能的重复 用点或连字符填充标签之间的可用空间 有没有办法用简单的 CSS 来格式化这样的文本 我有一个包含不同产品及其药物和剂量的数据库 希望统一显示它们 但没有等宽字体 Drug 1 10ml Another
  • 如何重现 sendRedirect 问题 - HTTPS 更改为 HTTP

    一旦用户成功通过身份验证 我将使用 response sendRedirect 将用户重定向到主页 这是为了避免 登录重定向漏洞 然而 由于上述更改 我的一位客户面临着他的 HTTPS 请求转换为 HTTP URL 中包含 IP 地址 的问
  • 301 将 Blogger 重定向到另一个主机并保持 Google 排名

    首先 我对任何拼写错误表示歉意 我是法国人 我希望您能提供一些可能有简单修复的帮助 但我还没有找到任何针对我的情况的信息 解释 我在 Blogger 平台上有一个网站 并且在 Godaddy 注册了大约两年的域名 但现在 我在那里并不满意
  • 如何保持后台 GPS 服务活跃

    我正在尝试将后台 GPS 位置侦听器作为一项服务 供我的应用程序中的所有活动使用 它还应该扫描位置 直到我 杀死 它 然而我意识到几个小时后 GPS 服务就被终止了 我无法获得更多的位置 如何保持此服务处于活动状态 至少是 location
  • 如何使用 jQuery 按名称选择元素?

    我有一个表格列 我正在尝试扩展和隐藏 jQuery 似乎隐藏了 td 当我选择它时的元素class但不是由元素的name 例如 bold hide Selecting by class works tcol1 hide Selecting
  • 移动网站的用户代理、域更改

    我有一个移动网站 我想将用户重定向到domain mobi 或mobile subdomain 我们都有设置 如何确定移动浏览器 将移动网站放在不同的域或子域中是一种不好的做法吗 如何确定移动浏览器 下面是一些可以完成这项工作的 javas
  • 打开“DEBUG”宏值

    在我的代码中 我想有条件地执行一些操作 ifdef DEBUG NSLog I m in debug mode endif 我已配置 项目 gt 编辑项目设置 gt 构建 选项卡 以便 DEBUG 列为用户定义设置 其值为 1 在 配置 下
  • Tkinter 侧边栏

    我想创建一个像上面那样可折叠的侧栏 在折叠形式中 侧栏仅包含选项的图标 当您将鼠标悬停在侧栏上时 侧栏将展开 显示图标的描述 名称 如果您单击该图标 它将带您进入该图标指定的功能 例如设置页面 右侧是带有很多按钮的主页 我不知道你会如何做到
  • 使用 Sails.js 进行个性化聊天

    sails js 中是否有一种方法或教程可以帮助我使用套接字 例如 pubnub 构建点对点聊天 我正在尝试使用 sails js 实现个性化聊天功能 这意味着我的应用程序中有多个用户 并且一个用户可以与另一个用户聊天 因此 如果用户 A
  • 如何通过Matlab正确读取串口数据

    我正在使用 Matlab 从微处理器的串行端口读取数据 数据为十六进制格式 每个样本为 4 个字节 32 位 当我读取这些数据时 我得到一些与传输的数据不匹配的随机数字序列 下面是我的代码 s serial COM16 assigns th
  • 在 JavaScript 中更改所选文本的字体样式

    我正在使用 javascript 没有任何库 现在我只想更改文本区域所选文本的字体样式 我使用以下函数提取了选择的文本 有人可以帮忙吗 function ShowSelectionInsideTextarea editor var text
  • 使用 SessionRegistry 获取登录用户

    我有一个完全由 Weblogic 容器保护的 Web 应用程序 现在我必须列出当前登录的用户 我必须使用 Spring Security 2 0 4 在 web xml 中我定义了必要的监听器和过滤器