Guest User

Untitled

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package usersessions
  2.  
  3. import javax.servlet.http.HttpSessionEvent
  4. import javax.servlet.http.HttpSessionListener
  5.  
  6. //import org.apache.shiro.subject.PrincipalCollection
  7. //import org.apache.shiro.subject.SimplePrincipalCollection;
  8. //import org.apache.shiro.subject.support.DefaultSubjectContext
  9.  
  10. class MyHttpSessionListener implements HttpSessionListener {
  11. private static List activeUsers = Collections.synchronizedList(new ArrayList());
  12. @Override
  13. public void sessionCreated(HttpSessionEvent event) { }
  14.  
  15. @Override
  16. public void sessionDestroyed(HttpSessionEvent event) {
  17. String userName = getCurrentUserName(event)
  18. if (userName == null) {
  19. return
  20. }
  21. MyHttpSessionListener.userLoggedOut(userName)
  22. }
  23.  
  24. public static void userLoggedIn(String userName) {
  25. if (!activeUsers.contains(userName)) {
  26. activeUsers.add(userName)
  27. }
  28. }
  29.  
  30. public static void userLoggedOut(String userName) {
  31. activeUsers.remove(userName)
  32. }
  33.  
  34. public static List getCurrentUserNames() {
  35. return Collections.unmodifiableList(activeUsers);
  36. }
  37. /*
  38. private String getCurrentUserName(HttpSessionEvent event) {
  39. PrincipalCollection currentUser = (PrincipalCollection) event.getSession().getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
  40. if (currentUser == null) {
  41. return null
  42. }
  43. return (String) currentUser.getPrimaryPrincipal();
  44. }
  45. */
  46.  
  47. }
  48.  
  49. myHttpSessionListener(MyHttpSessionListener)
  50. /* shiroAuthenticator(ModularRealmAuthenticator) {
  51. authenticationStrategy = ref("shiroAuthenticationStrategy")
  52. authenticationListeners = [ ref("authListener") ]
  53. }
  54. */
  55.  
  56. package usersessions
  57.  
  58. class NewController extends MyHttpSessionListener {
  59. def index() {
  60. render "Users "+ getCurrentUserNames()
  61. }
  62. }
Add Comment
Please, Sign In to add comment