spring-projects / spring-projects/spring-session

Documentation: OpenSessionInView problem

Open
#542 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
1.9k
Forks
1.2k
Avg merge
4h 27m
Merged PRs (30d)
55

Description

When you follow the guide to integrated Spring session with Spring Security, you will place the sessionRepositoryFilter at the top of the processing chain.

This places it before the OpenSessionInViewFilter which will normally be configured in the AppInitializer or web.xml. And it will create LazyLoadingExceptions if anything is saved during the authentication process, such as updating the last login date.

To solve this, a user will need to remove the OpenSessionInViewFilter from the AppInitializer/web.xml and add it to AbstractHttpSessionApplicationInitializer like so:

public class SessionWebApplicationInitializer extends AbstractHttpSessionApplicationInitializer {

public SessionWebApplicationInitializer() {
}

public SessionWebApplicationInitializer(Class<?>... configurationClasses) {
    super(configurationClasses);
}

protected void beforeSessionRepositoryFilter(ServletContext servletContext) {
    Dynamic registration = servletContext.addFilter("openSessionInViewFilter", new OpenSessionInViewFilter());
    if (registration == null) {
        throw new IllegalStateException(
                "Duplicate Filter registration for openSessionInViewFilter. Check to ensure the Filter is only configured once.");
    }
    registration.setAsyncSupported(false);
    EnumSet<DispatcherType> dispatcherTypes = getSessionDispatcherTypes();
    registration.addMappingForUrlPatterns(dispatcherTypes, false,"/*");
}

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the integrated Spring Session and Spring Security guide and locate its filter-ordering instructions. Update the guide to explain the OpenSessionInViewFilter interaction and the AppInitializer/web.xml configuration, using AbstractHttpSessionApplicationInitializer as shown. Done means the documented setup avoids LazyLoadingExceptions during authentication.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.