i have a probleme with httpSession and spring redis implementation. I have declare a filter who have one job. Catch a request param and set it into session. This attribute is use later for call an external api.
My implementation :
i use spring-boot
i use a JedisConnectionFactory for persist my session into reddis.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
have declare use store-type redis to spring-boot
session:
store-type: redis
timeout: 900s
redis:
host: redis
port: 6379
now my filter
@Component
@Slf4j
@Order(1)
public class MyCustomFilter implements Filter {
@Resource
private SessionHelper sessionHelper;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
// retrieve site from request attribute
final String site = request.getParameter("site");
// persiste site into session with a bean scoped SESSION
sessionHelper.saveSite(site);
filterChain.doFilter(request, response);
}
}
my sessionHelper
@Component
@Slf4j
@Scope(value = SCOPE_SESSION, proxyMode = TARGET_CLASS)
public class SessionHelper implements Serializable {
@Resource
private HttpSession httpSession;
public void saveSite(String site) {
httpSession.setAttribute("site", site);
}
public String getSite(String site) {
return (String) httpSession.getAttribute("site");
}
}
my service who need to retrieve site from session
@Slf4j
@Component
public class MyFacade {
@Resource
private SessionHelper sessionHelper;
public void myMethod() {
log.debug(sessionHelper.getSite());
}
}
result, my facade log always a null. So i don't understand why. My filter set this variable into session.
i have try to log sessionId for the moment when variable is set and when she is get. sessionId is different... and i don't understand why.
if someone has a clue for me...
thx