3

I have a running jetty-server with basic authentification. Now i want to see in the application which users are logged in and have an active session. I want to save changes in a jpa-database by user, so i need to get the informationen in my JpaService-class to save the change by the user. My running server is

public class TestServer {

public static void main(String[] args) {
    startServer();
}

public static void startServer() {
    Server server = new Server(8899);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setSecurityHandler(basicAuth());                
    server.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", JpaService.class.getCanonicalName());

    try {
        server.start();
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        server.destroy();
    }
}

private static final SecurityHandler basicAuth() {
    HashLoginService l = new HashLoginService();

    l.putUser("user1", Credential.getCredential("pass1"), new String[] {"user"});
    l.putUser("user2", Credential.getCredential("pass2"), new String[] { "admin"});
    l.setName("Private Server");

    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(new String[] { "admin", "user" });
    constraint.setAuthenticate(true);

    ConstraintMapping cm = new ConstraintMapping();
    cm.setConstraint(constraint);
    cm.setPathSpec("/*");

    ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
    csh.setAuthenticator(new BasicAuthenticator());
    csh.setRealmName("NameOfRealm");
    csh.addConstraintMapping(cm);
    csh.setLoginService(l);

    return csh;
}

}

So how can i "read" the relevant informations? I use jetty 9.2.3 and Get jetty realm credentials in application seems outdated.

I tried to add in a few different ways to add the following, but nothing happens.

public class HttpSessionCollector implements HttpSessionListener {
private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();

@Override
public void sessionCreated(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    sessions.put(session.getId(), session);
    System.out.println("Created Session: "+event.getSession().getId());
    System.out.println("Last Accessed: "+new Date(event.getSession().getLastAccessedTime()));
    System.out.println("Attributes: "+ event.getSession().getAttributeNames());
}

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    sessions.remove(event.getSession().getId());
    System.out.println("Destroyed Session: "+event.getSession().getId());
}

public static HttpSession find(String sessionId) {
    return sessions.get(sessionId);
}

public static Map<String, HttpSession> getSessions() {
    return sessions;
}
}

And when i add context.getSessionHandler().getState() it says "STOPPED". When i try .start() i get a NullPointerException. Maybe i did all wrong, but how is it done, so i get logged in users in my web-application?

Community
  • 1
  • 1
Matthias Wegner
  • 303
  • 4
  • 18

0 Answers0