spring security 三 扩展 HttpSessionEventPublisher 统计在线用户数
spring security 3 扩展 HttpSessionEventPublisher 统计在线用户数
确实是这样的,不知道有啥办法解决~
1. package com.hp.ts.bca.security.online; 2. 3. import javax.servlet.http.HttpSessionEvent; 4. 5. import org.springframework.security.Authentication; 6. import org.springframework.security.context.SecurityContextHolder; 7. import org.springframework.security.ui.session.HttpSessionEventPublisher; 8. import org.springframework.web.context.WebApplicationContext; 9. import org.springframework.web.context.support.WebApplicationContextUtils; 10. 11. import com.yourcompany.domain.entity.security.User; 12. import com.yourcompany.service.mgmt.OnlineUserService; 13. 14. /** 15. * 扩展的HttpSessionEventPublisher 16. * 支持在线人数统计 17. * 18. */ 19. public class EnhancedHttpSessionEventPublisher extends HttpSessionEventPublisher { 20. 21. @Override 22. public void sessionCreated(HttpSessionEvent event) { 23. // 将用户加入到在线用户列表中 24. saveOrDeleteOnlineUser(event, Type.SAVE); 25. super.sessionCreated(event); 26. } 27. 28. @Override 29. public void sessionDestroyed(HttpSessionEvent event) { 30. // 将用户从在线用户列表中移除 31. saveOrDeleteOnlineUser(event, Type.DELETE); 32. super.sessionDestroyed(event); 33. } 34. 35. public void saveOrDeleteOnlineUser(HttpSessionEvent event, Type type) { 36. Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 37. if (auth != null) { 38. Object principal = auth.getPrincipal(); 39. if (principal instanceof User) { 40. User user = (User) principal; 41. 44. switch (type) { 45. case SAVE: 46. OnlineUserList.add(user.getId);//List<String> 47. break; 48. case DELETE: 49. OnlineUserList.remove(user.getId); 50. break; 51. } 52. } 53. } 54. } 55. 56. /** 57. * 定义一个简单的内部枚举 58. */ 59. private static enum Type { 60. SAVE, DELETE; 61. } 62. 63. }
web.xml 里的配置
<listener> <listener-class> com.hp.ts.bca.security.online.EnhancedHttpSessionEventPublisher </listener-class> </listener>
1 楼
caoyangx
2010-11-01
不知道你试过没有,saveOrDeleteOnlineUser这个方法,Authentication auth = SecurityContextHolder.getContext().getAuthentication();
登录后是空的,因为他获取的的是在同一请求线程中的数据,所以每次都是null的。
登录后是空的,因为他获取的的是在同一请求线程中的数据,所以每次都是null的。
2 楼
JetMah
2010-11-13
@Autowired
SessionRegistry sessionRegistry;
@ModelAttribute("numUsers")
public int getNumberOfUsers() {
return sessionRegistry.getAllPrincipals().size();
}
SessionRegistry sessionRegistry;
@ModelAttribute("numUsers")
public int getNumberOfUsers() {
return sessionRegistry.getAllPrincipals().size();
}
3 楼
lmzmem
2011-04-06
使用SessionRegistry,可以取到所有登陆的用户(个数当然可以),根据sessionID取用户,配置如下:
1.string配置
<beans:bean id="sessionRegistry"
class="org.springframework.security.concurrent.SessionRegistryImpl" />
<beans:bean id="defaultConcurrentSessionController"
class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.providers.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
<beans:property name="sessionController"
ref="defaultConcurrentSessionController" />
</beans:bean>
2.java中自动注入
@Autowired
SessionRegistry sessionRegistry;
@ModelAttribute("numUsers")
public int getNumberOfUsers() {
return sessionRegistry.getAllPrincipals().size();
}
1.string配置
<beans:bean id="sessionRegistry"
class="org.springframework.security.concurrent.SessionRegistryImpl" />
<beans:bean id="defaultConcurrentSessionController"
class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.providers.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
<beans:property name="sessionController"
ref="defaultConcurrentSessionController" />
</beans:bean>
2.java中自动注入
@Autowired
SessionRegistry sessionRegistry;
@ModelAttribute("numUsers")
public int getNumberOfUsers() {
return sessionRegistry.getAllPrincipals().size();
}
4 楼
cn_hack
2012-09-12
caoyangx 写道
不知道你试过没有,saveOrDeleteOnlineUser这个方法,Authentication auth = SecurityContextHolder.getContext().getAuthentication();
登录后是空的,因为他获取的的是在同一请求线程中的数据,所以每次都是null的。
登录后是空的,因为他获取的的是在同一请求线程中的数据,所以每次都是null的。
确实是这样的,不知道有啥办法解决~