java - 我如何获得 Spring Security SessionRegistry?

标签 java session spring-security struts2

我似乎找不到如何在 Struts 操作中获取对 Spring Security (V3) SessionRegistry 的引用。

我已经在我的 web.xml 文件中配置了监听器:

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

而且我尝试使用@Autowired 注释将它带入一个 Action 中:

@Autowired
private SessionRegistry sessionRegistry;

@Override
public String execute() throws Exception {
    numberOfUsersLoggedin= sessionRegistry.getAllPrincipals().size();
    return SUCCESS;       
}

public SessionRegistry getSessionRegistry() {
    return sessionRegistry;
}

public void setSessionRegistry(SessionRegistry sessionRegistry) {
    this.sessionRegistry = sessionRegistry;
}

http 配置如下所示:

    <session-management invalid-session-url="/public/login.do?login_error=expired"
        session-authentication-error-url="/public/login.do" 
        session-fixation-protection="newSession">
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
    </session-management>    

一般来说,我更愿意自己连接 Spring bean,但不确定如何使用命名空间公开它。每次执行操作时, session 注册表为空。

任何人都可以指出我在这里做错了什么,或者告诉我如何找到一个例子吗?

提前感谢任何/所有回复!

最佳答案

如果您通过命名空间配置 Spring Security,concurrency-control 标记的以下属性可用于访问 SystemRegistry:

  1. session 注册表别名。
  2. session-registry-ref.

官方文档对各个属性的描述:

session-registry-alias. It can also be useful to have a reference to the internal session registry for use in your own beans or an admin interface. You can expose the internal bean using the session-registry-alias attribute, giving it a name that you can use elsewhere in your configuration.

session-registry-ref. The user can supply their own SessionRegistry implementation using the session-registry-ref attribute. The other concurrent session control beans will be wired up to use it.

关于java - 我如何获得 Spring Security SessionRegistry?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886256/

相关文章:

php - 使用 Zend_Auth 设置和延长 session 生命周期

php - 多语言网页 : php + mysql + cookies

spring-security - 如何使用 spring security 重定向到拒绝访问页面

asp.net-mvc - ASP.NET MVC - 使用 session 的替代方法

java - 身份验证期间抛出并处理自定义异常 Spring Security + WebFlux

java - 如何使用JWT身份验证在springboot中使用html模板?

java - 无法使用 Maven 编译简单的 Java 10/Java 11 项目

java - 将读取文件中的整数字符存储到二维数组中

java - 线程 "main"java.net.MalformedURLException : no protocol error while finding broken links in a page using Selenium and Java 中出现异常

java - 如何使某些东西与数组和集合兼容?