我想获取当前登录我的应用程序的所有用户的列表。我知道,我应该注射SessionRegistry
在我的代码中调用getAllPrincipals()
方法。不幸的是,我总是得到空列表。看起来SessionRegistry
没有填充,我不知道如何制作它。我知道,StackOverflow 上也有类似的问题,但我仍然无法解决我的问题。首先,我将以下代码添加到我的 web.xml 中:
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
据我所知,它允许获取有关会话生命周期(创建、销毁)的信息。就这样。我在前进方面遇到了很大的问题。我的安全 bean 文件如下所示:
<beans:bean id="successAuth" class="pl.fp.microblog.auth.SuccessAuth"/>
<beans:bean id="failureAuth" class="pl.fp.microblog.auth.FailureAuth"/>
<http auto-config="true">
<intercept-url pattern="/" access="ROLE_USER" />
<intercept-url pattern="/profile/*" access="ROLE_USER" />
<form-login
login-page="/login"
authentication-success-handler-ref="successAuth"
authentication-failure-handler-ref="failureAuth"
username-parameter="username"
password-parameter="password"
login-processing-url="/login_processing_url"
/>
<security:logout logout-url="/logout_processing_url"/>
<session-management>
<concurrency-control max-sessions="1" session-registry-alias="sessionRegistry"/>
</session-management>
</http>
<beans:bean id="saltProvider" class="org.springframework.security.authentication.dao.ReflectionSaltSource">
<beans:property name="userPropertyToUse" value="username"></beans:property>
</beans:bean>
<beans:bean id="userService" class="pl.fp.microblog.auth.UserService">
<beans:property name="userDAO" ref="userDAO"/>
</beans:bean>
<authentication-manager>
<security:authentication-provider user-service-ref="userService">
<password-encoder hash="sha-256">
<security:salt-source ref="saltProvider"/>
</password-encoder>
</security:authentication-provider>
</authentication-manager>
这里我调用 getAllPrinciples() 方法:
@Transactional
@Controller
public class SiteController {
@Autowired
private UserDAO userDAO;
@Autowired
private SessionRegistry sessionRegistry;
@RequestMapping(value = "profile/{login}")
public String profilePage(@PathVariable String login, HttpServletRequest req) throws SQLException {
...
sessionRegistry.getAllPrincipals().size()
...
return "profile";
}
}
我尝试添加session-managemenent
代码到我的http
, ConcurrentSessionFilter
和类似的事情,但实际上我不明白。而且文档对我来说太复杂了。有谁可以帮助我并逐步告诉我下一步该怎么做?我应该添加哪些豆子?
我想你已经快到了。您可能错过的唯一一件事是使用session-registry-alias
。通过在concurrency-control
元素公开会话注册表,以便可以将其注入到您自己的 bean 中。请参阅参考文档 http://static.springsource.org/spring-security/site/docs/3.1.x/reference/appendix-namespace.html#nsa-concurrency-control.
所以你需要的是:
<http auto-config="true">
...
<session-management>
<concurrency-control max-sessions="1" session-registry-alias="sessionRegistry"/>
</session-management>
</http>
现在您已经有了对会话注册表的引用,该注册表将由ConcurrentSessionControlStrategy
这是由上述配置隐式设置的。要使用它,你只需像平常一样将它注入到你的 bean 中:
<bean class="YourOwnSessionRegistryAwareBean">
<property sessionRegistry="sessionRegistry"/>
</bean>
请注意,上述配置还将限制用户可能拥有的并发会话数。如果你不想要这个限制,你将不得不放弃命名空间配置的便利,因为命名空间模式不允许你设置max-sessions
属性为-1。如果您需要有关如何手动连接必要的 bean 的帮助,参考文档给出了详细说明 http://static.springsource.org/spring-security/site/docs/3.1.x/reference/session-mgmt.html#concurrent-sessions.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)