Session Management

Inrupt’s Java Client Libraries provide a Sessionarrow-up-right interface to handle session objects. In multi-user contexts, multiple sessions in a single application must be managed to ensure that one user’s session is not used by another user.

The following content provides some recommendations for managing sessions in multi-user applications.

Session Scope

Avoid Application-Scoped Sessions

For multi-user applications, the general guidance is to avoid making a session scoped to the entire application. That is, when using dependency injection framework such as Springarrow-up-right or JakartaEEarrow-up-right:

  • Do NOT use @ApplicationScope, @ApplicationScoped, or equivalent scope.

  • Do NOT use @Singleton or equivalent scope.

Use Request-Scoped Sessions

For applications where a session is used by different components, instantiating an independent session inside each component introduces unnecessary overhead. Instead, in cases where these applications also use dependency injection framework such as Springarrow-up-right or JakartaEEarrow-up-right, consider using request scopes:

  • @RequestScope in Spring,

  • @RequestScoped in JakartaEE, or

  • the equivalent annotation in your framework.

triangle-exclamation
import com.inrupt.client.auth.Session;
import com.inrupt.client.openid.OpenIdSession;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
// ...

@RequestScoped
public class SessionManager {
    private Session session;

    @Inject
    JsonWebToken jwt

    Session getSession() {
        if (session == null) {
            session = OpenIdSession.ofIdToken(jwt.getRawToken());
        }
        return session;
    }
}

With request scoped session, the Java runtime automatically removes references to that session at the end of a request.

Application Scoped Clients

circle-exclamation

For the client object (i.e. SolidClientarrow-up-right, SolidSyncClientarrow-up-right),

When possible, use application/singleton scope for clients.

Then, a Web component (e.g., DataEndpoint in the following code block) of this application can use the application-scoped client and the request-scoped session in the following manner:

Last updated