我想让在线用户列表
这是我的密码
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>spring-security.xml
<security:http auto-config="true">
<security:session-management>
<security:concurrency-control max-sessions="1" expired-url="/login?expire" />
</security:session-management>
</security:http>和服务
@Service
public class UserSessionRegistry {
@Autowired
private SessionRegistry sessionRegistry;
public List<User> getOnlineUsers() {
List<User> retValue = new ArrayList<User>();
List<Object> onlineUsers = sessionRegistry.getAllPrincipals();
for (Object usr : onlineUsers) {
retValue.add((User) usr);
}
return retValue;
}
}但始终返回null作为结果。
发布于 2014-09-30 19:33:18
春季安全措施已更改如下,结果是正确的。
<bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
<security:http auto-config="true">
<security:session-management>
<security:concurrency-control
session-registry-ref="sessionRegistry" max-sessions="1" expired-url="/login?expire" />
</security:session-management>
</security:http>发布于 2014-10-01 05:39:46
通过使用这段代码,您可以得到所有的在线用户,除了试图获取的用户。
并首先在您的春季安全配置中实现SessionRegistry bean。
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String userName = auth.getName();
List<Object> principals = sessionRegistry.getAllPrincipals();
List<UserInfo> usersInfoList = new ArrayList<UserInfo>();
for (Object principal: principals) {
if (principal instanceof UserInfo) {
if(!((UserInfo) principal).getUsername().equals(userName)){
for(SessionInformation sess :sessionRegistry.getAllSessions(principal, false)){
if(!sess.isExpired()){
usersInfoList.add((UserInfo) sess.getPrincipal());
}
}
}
}
发布于 2014-09-30 19:11:09
根据spring 文档,您需要设置并发控制并使用maximumSession设置为-1 (如果不需要控制并发会话的数量)。这样,您将注册SessionRegistry并访问主体信息。
https://stackoverflow.com/questions/26128032
复制相似问题