How to integrate shiro with redis for session manager using spring boot?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to integrate shiro with redis for session manager using spring boot?

yuwei

Hi,

I already have web app running with shiro and spring boot successfully.

Now I want to use redis as session cache.

Is it possible to integrate shiro with redis for session management?

How could I do this?


Thanks,

Jared, (韦煜)
Software developer
Interested in open source software, big data, Linux

Reply | Threaded
Open this post in threaded view
|

Re: How to integrate shiro with redis for session manager using spring boot?

scSynergy
You have to implement your own CacheManager and Cache and configure Shiro to
use them. These are my classes for Infinispan; you can use them as examples
for your Redis implementation:

shiro.ini
[main]
...
securityManager.cacheManager = $cacheManagerInfinispan
...

-------------------------------------------------------------------------
package de.scsynergy.elementary.qi.shiro;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.apache.shiro.util.Destroyable;
import org.infinispan.manager.EmbeddedCacheManager;
import org.ops4j.pax.shiro.cdi.ShiroIni;

@ShiroIni
public class CacheManagerInfinispan implements CacheManager, Destroyable {

    @Resource(lookup = "java:jboss/infinispan/container/web")
    private EmbeddedCacheManager cacheContainer;
    private static final Pattern PATTERN =
Pattern.compile("([^.]+)\\.authorizationCache\\.?\\d*");
    private final Map<String, Cache> mapCache = new HashMap();

    protected CacheManagerInfinispan() {
    }

    @PostConstruct
    public void init() {
    }

    @PreDestroy
    @Override
    public void destroy() {
        for (String name : cacheContainer.getCacheNames()) {
            if (!CachingSessionDAO.ACTIVE_SESSION_CACHE_NAME.equals(name)) {
                mapCache.get(name).clear();
            }
        }
        mapCache.clear();
    }

    @Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        Cache cache = mapCache.get(name);
        if (cache == null) {
            Matcher matcher = PATTERN.matcher(name);
            if (matcher.find()) {
                name = matcher.group(1);
            }
            cache = new CacheInfinispan(cacheContainer.getCache(name,
"persistent"));
            mapCache.put(name, cache);
        }
        return cache;
    }
}

-------------------------------------------------------------------------
package de.scsynergy.elementary.qi.shiro;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.shiro.cache.CacheException;
import org.infinispan.Cache;

public class CacheInfinispan<K, V> implements
org.apache.shiro.cache.Cache<K, V> {

    private Cache<K, V> cache;

    protected CacheInfinispan() {
    }

    public CacheInfinispan(Cache cache) {
        this.cache = cache;
    }

    @Override
    public V get(K key) throws CacheException {
        return cache.get(key);
    }

    @Override
    public V put(K key, V value) throws CacheException {
        return cache.put(key, value, 1, TimeUnit.DAYS);
    }

    @Override
    public V remove(K key) throws CacheException {
        return cache.remove(key);
    }

    @Override
    public void clear() throws CacheException {
        cache.clear();
    }

    @Override
    public int size() {
        return cache.size();
    }

    @Override
    public Set<K> keys() {
        return cache.keySet();
    }

    @Override
    public Collection<V> values() {
        return cache.values();
    }
}




--
Sent from: http://shiro-user.582556.n2.nabble.com/
Reply | Threaded
Open this post in threaded view
|

RE: How to integrate shiro with redis for session manager using spring boot?

srodrigues
In reply to this post by yuwei

Here is a Git hub package that has shiro with redis implementation for session management.

https://github.com/alexxiyang/shiro-redis

 

HTH,

 

-Sunil

 

 

 

From: Yu Wei [mailto:[hidden email]]
Sent: Sunday, September 17, 2017 5:33 AM
To: [hidden email]
Subject: How to integrate shiro with redis for session manager using spring boot?

 

Hi,

I already have web app running with shiro and spring boot successfully.

Now I want to use redis as session cache.

Is it possible to integrate shiro with redis for session management?

How could I do this?

 

Thanks,

Jared, (韦煜)
Software developer
Interested in open source software, big data, Linux




CONFIDENTIALITY NOTICE: This electronic message contains information which may be legally confidential and/or privileged. The information is intended solely for the individual or entity named above and access by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender that you have received the message in error, and delete it. Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: How to integrate shiro with redis for session manager using spring boot?

yuwei

Thank you, Sunil.

I used spring-session in spring-boot with redis. It seemed that it worked well.

But I didn't find official document about this.

I will try and see whether there is potential issues or not.


Thanks,

Jared, (韦煜)
Software developer
Interested in open source software, big data, Linux


From: Sunil Rodrigues <[hidden email]>
Sent: Monday, September 18, 2017 11:17:24 PM
To: [hidden email]
Subject: RE: How to integrate shiro with redis for session manager using spring boot?
 

Here is a Git hub package that has shiro with redis implementation for session management.

https://github.com/alexxiyang/shiro-redis

 

HTH,

 

-Sunil

 

 

 

From: Yu Wei [mailto:[hidden email]]
Sent: Sunday, September 17, 2017 5:33 AM
To: [hidden email]
Subject: How to integrate shiro with redis for session manager using spring boot?

 

Hi,

I already have web app running with shiro and spring boot successfully.

Now I want to use redis as session cache.

Is it possible to integrate shiro with redis for session management?

How could I do this?

 

Thanks,

Jared, (韦煜)
Software developer
Interested in open source software, big data, Linux




CONFIDENTIALITY NOTICE: This electronic message contains information which may be legally confidential and/or privileged. The information is intended solely for the individual or entity named above and access by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender that you have received the message in error, and delete it. Thank you.