Shiro implements session sharing and shirosession sharing

Source: Internet
Author: User

Shiro implements session sharing and shirosession sharing

Session sharing: in multiple application systems, if Server Load balancer is used, users' requests are distributed to different applications, session data in application A cannot be obtained in application B, which leads to the sharing problem.

Assume that the first time you access server A connected to the user, the user logs on to the system. When the user attempts again, the request is forwarded to server B, and the user does not log on to server B, at this time, the user again came to the login page, which is difficult to understand and accept, which leads to session sharing.

How can the shiro framework share sessions? Shiro can be shared in two aspects: session sharing and cache sharing. Next we will combine redis to implement these two aspects.

 

1. Session sharing

Shiro provides its own session manager sessionManager, which has the attribute sessionDao to process all session information.

For sessionDao, shiro also provides its own implementation, which is commonly used by ehcache. Ehcache is jvm-level. Multiple applications generate multiple cache examples, and information cannot be shared across processes. To achieve sharing, we need to rewrite sessionDao and implement our own Dao to ensure the uniqueness of the same session information.

Take a look at the following class diagram:

1. inherit the abstract sessionDao provided by shiro, and rewrite the methods such as create, read, and delete.

2. Considering the scalability of the system, we abstract a Data Warehousing interface and provide a redis implementation method. If we need to replace the underlying database to store session data in the future, we only need to expand the implementation class of a database and inject it.

 

Ii. Cache sharing

 

Similarly, shiro provides its own cache manager: cacheManager, Which is rewritten to meet cache sharing.

See the following class diagram:

  1. We only need to implement the shiro CacheManager and Cache interfaces. The former exposes the method for obtaining the Cache instance, and the latter provides the addition, deletion, modification, and query operations for the Cache instance.

  2. The abstraction at the intermediate layer is designed to be good and provide scalability. In the future, you only need to add implementation classes of ShiroCacheManager.

  3. Cache instances in JedisShiroCacheManager Cache through ConCurrentHashMap to prevent repeated object creation.

 

Iii. Configuration

 

3.1 Cache Configuration

<! -- RedisManager -->

<Bean id = "redisManager" class = "com. yingxinhuitong. shiro. util. RedisManager">
<Property name = "host" value = "127.0.0.1"/>
<Property name = "port" value = "6379"/>
<Property name = "expire" value = "1800"/>
<! -- <Property name = "password" value = ""/> -->
<! -- <Property name = "timeout" value = "0"/> -->
</Bean>
<! -- Shiro cache -->
<Bean id = "shrioCacheManager" class = "com. yingxinhuitong. shiro. cache. JedisShiroCacheManager">

<Property name = "redisManager" ref = "redisManager"/>
</Bean>
<Bean id = "shiroJedisManager" class = "com. yingxinhuitong. shiro. cache. CustomShiroCacheManager">

<Property name = "shrioCacheManager" ref = "shrioCacheManager"/>
</Bean>

 

3.2 sessionDao Configuration

<! -- Session DAO -->

<Bean id = "shiroSessionRepository"

Class = "com. yingxinhuitong. shiro. session. JedisShiroSessionRepository">

<Property name = "redisManager" ref = "redisManager"/> </bean>

<Bean id = "sessionDAO" class = "com. yingxinhuitong. shiro. session. CustomShiroSessionDao"> <property name = "sessionIdGenerator" ref = "sessionIdGenerator"/>

<Property name = "shiroSessionRepository" ref = "shiroSessionRepository"/>

</Bean>

 

4. Code

See https://github.com/zljk0306/shiro-redis-share

 

Follow ginger to talk about technology, no.: helojava, or scan the following QR code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.