Amit Yatagiri
Amit Yatagiri

Reputation: 425

differentiate between mobile app and desktop browser request for session management

Context:(Java) Web application uses apache shiro for session management,authentication,authorization. Now for mobile users alone, the session attributes needs to be changed during creation(ex: increasing the session time out interval etc).

How to differentiate between mobile client and desktop browser client? browser user agent might give the value, but is it dependable?

What are the other things need to be taken care(in terms of session handling at back end) when mobile devices consume web application?

Upvotes: 2

Views: 1397

Answers (1)

Wouter
Wouter

Reputation: 4016

We found it easiest to do with the spring mobile device module: http://docs.spring.io/spring-mobile/docs/current/reference/html/device.html

You can just add a servlet filter to your web.xml and it will keep the current device information in the request:

<filter>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>deviceResolverRequestFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And then you can get the info with a util

    Device currentDevice = DeviceUtils.getCurrentDevice((HttpServletRequest) request);
    if (currentDevice != null && (currentDevice.isMobile() || currentDevice.isTablet())){
        //do mobile stuff
    } else {
        //do desktop stuff
    }

All the user-agent stuff to detect a mobile device is handled by that library.

As for your second question, there isn't really a difference on the service side of things, if the webpages are loaded from a mobile device or a desktop. They both have HttpSessions, cookies etc.

Upvotes: 1

Related Questions