Issues While using Scope Based Services in Grails

94 Views Asked by At

I am working on grails application. For Users Management I am using Spring Security Core PlugIn of Grails. I want to store some information which will be specific to just currently loggedIn user that too until user remains loggedin and I want this information to be available across all browsers. I have tried to access this using scope based services. So I created a service:

class LoggedInUserSessionDataService {

    String searchParams
    String searchQuery

    //This service will get expire when loggedin user's session will expire
    static scope = 'session'
}

And Proxy for it in resources.groovy file:

loggedInUserSessionDataServiceProxy(org.springframework.aop.scope.ScopedProxyFactoryBean) {
    targetBeanName = 'loggedInUserSessionDataService'
    proxyTargetClass = true
}

Then in my controller, I injected this proxy:

def loggedInUserSessionDataServiceProxy

So, after all this, the stored information just available to specific browser, And when I try to access this information in other browser It is not found, while it should be accessible in that browser too.

Please guide me how I can achieve this ? If It's not possile through scope based services, then what could be other options ?

P.S. In the same browser It works fine that stored information remains available in the same browser while user remains loggedIn.

Thanks for your time :)

1

There are 1 best solutions below

0
On

When the user logs in from another browser, they are assigned a different session, even though they are signed in as the same user. You need to somehow link the two sessions together.

One possible solution would be to create an ordinary singleton service FooService containing a data structure like Map<User, SessionData> where SessionData is your abstracted session holding data. It would also contain a counter; when a user logs in, your LoggedInUserSessionDataService would call fooService.connectSession(user) which would return the existing SessionData instance or create a new one and increment its counter by 1. When a session ends, the service would call fooService.disconnectSession(user) which would decrement the counter. When the counter gets to 0, you remove the user key from the map to reset that 'session'.