-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibility to apply the SessionRepositoryFilter
conditionally.
#2942
Comments
The current solution is to define our own SessionRepositoryFilter, which extends the default In the Spring configuration class, implement the
public class ConditionalSessionRepositoryFilter<S extends Session> extends SessionRepositoryFilter<S> {
public ConditionalSessionRepositoryFilter(SessionRepository<S> sessionRepository) {
super(sessionRepository);
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (shouldNotFilter(request)) {
filterChain.doFilter(request, response);
return;
}
super.doFilterInternal(request, response, filterChain);
}
private boolean shouldNotFilter(HttpServletRequest request) {
// Return true or false based on your needs.
}
}
@Configuration(proxyBeanMethods = false)
@EnableRedisIndexedHttpSession
public class SpringSessionConfig implements BeanDefinitionRegistryPostProcessor {
@Bean
public LettuceConnectionFactory connectionFactory() {
return new LettuceConnectionFactory();
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registry.removeBeanDefinition("springSessionRepositoryFilter");
BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(ConditionalSessionRepositoryFilter.class)
.addConstructorArgReference("sessionRepository")
.getBeanDefinition();
registry.registerBeanDefinition("springSessionRepositoryFilter", definition);
}
} Not sure whether it is possible for Spring Session to define an optional condition bean in |
Thanks for the report @yuezk. I agree that there should be an easier way to define my own |
Expected Behavior
The
SessionRepositoryFilter
can be applied conditionally.Current Behavior
The
SessionRepositoryFilter
cannot be applied conditionally.Context
We are working on a legacy giant project and we want to migrate to Spring Session progressively. Currently, it seems like the
SessionRepositoryFilter
is applied to all requests once set up. However, we only want to apply the filter to some requests, for example, requests that have a specific header or requests that have a specific path.I looked into the source code of
SessionRepositoryFilter
and it seems like there is no way to achieve this. I am wondering if there is a way to achieve this with the current Spring Session implementation.If not possible, shall we consider introducing such a mechanism to the Spring Session? For example:
SessionRepositoryFilter
bean, instead of creating a new one in SpringHttpSessionConfiguration.javaSessionRepositoryFilter
bean, so that the filter can be applied conditionally.Thanks in advance!
The text was updated successfully, but these errors were encountered: