How can I do relational database-based HTTP Session Persistence in Spring 4? -
i need able store http session in relational database in order stateless load balancing of front-end users across multiple front-end servers. how can achieve in spring 4?
i see how 1 can redis, there not appear documentation on how relational database e.g. postgres.
with spring session (it transparently override httpsessions java ee) can take sessionrepository
interface , implement custom ex. jdbcsessionrepository
. kind of easy do. when have implementation, add manually (you don't need @enableredishttpsession
annotation) created filter filter chain, bellow:
@configuration @enablewebmvcsecurity public class securityconfiguration extends websecurityconfigureradapter { //other stuff... @autowired private sessionrepository<expiringsession> sessionrepository; private httpsessionstrategy httpsessionstrategy = new cookiehttpsessionstrategy(); // or headerhttpsessionstrategy @bean public sessionrepository<expiringsession> sessionrepository() { return new jdbcsessionrepository(); } @override protected void configure(httpsecurity http) throws exception { super.configure(http); sessionrepositoryfilter<expiringsession> sessionrepositoryfilter = new sessionrepositoryfilter<>(sessionrepository); sessionrepositoryfilter.sethttpsessionstrategy(httpsessionstrategy); http .addfilterbefore(sessionrepositoryfilter, channelprocessingfilter.class); } }
here have how sessionrepository
interface looks like. has 4 methods implement. how create session object, can in mapsessionrepository
, mapsession
implementation (or redisoperationssessionrepository
, redissession
).
public interface sessionrepository<s extends session> { s createsession(); void save(s session); s getsession(string id); void delete(string id); }
example solution https://github.com/mati20041/spring-session-jpa-repository
Comments
Post a Comment