我想获取当前登录我的应用程序的所有用户的列表。我知道,我应该注入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
和类似的东西中,但实际上我不明白。而且文档对我来说太复杂了。有谁可以帮助我并逐步告诉我下一步该做什么?我应该添加哪些豆子?