zoukankan      html  css  js  c++  java
  • jfinal shiro共享

    和上一篇tomcat sexxion共享一样,用的也是redis

    代码:

    package com.test.shiro;
    
    import com.jfinal.log.Log;
    import com.jfinal.plugin.redis.Redis;
    import org.apache.shiro.session.Session;
    import org.apache.shiro.session.UnknownSessionException;
    import org.apache.shiro.session.mgt.SimpleSession;
    import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
    
    import java.io.*;
    
    public class OnlineSessionDao extends EnterpriseCacheSessionDAO {
    
        private static final Log log = Log.getLog(OnlineSessionDao.class);
    
        //定义sessionDao缓存的前缀,可以通过 Redis.use().getJedis().keys(OnlineSessionDao.cacheNamePrefix + "*") 获取到sessionDao缓存的所有session
        public static final String cacheNamePrefix = "shiro_sessionDao_cache:";
    
        private void set(String key, Object value){
            Redis.use().set(cacheNamePrefix + key, value);
        }
    
        private Object get(String key){
            return Redis.use().get(cacheNamePrefix + key);
        }
    
        private void remove(String key){
            Redis.use().del(cacheNamePrefix + key);
        }
    
        /**
         * 创建session
         */
        @Override
        public Serializable doCreate(Session session) {
            Serializable sessionId = super.doCreate(session);
            log.info("创建  Session:"+session.getHost() + ";" + session.getId());
            set(session.getId().toString(), sessionToByte(session));
            return sessionId;
        }
    
    
        /**
         * 删除session
         */
        @Override
        public void doDelete(Session session) {
            log.info("删除 Session:"+session.getHost() + ";" + session.getId());
            remove(session.getId().toString());
            super.doDelete(session);
        }
    
        /**
         * 更新session的最后一次访问时间
         */
        @Override
        public void doUpdate(Session session) throws UnknownSessionException {
            log.info("更新 Session:"+session.getHost() + ";" + session.getId());
            set(session.getId().toString(), sessionToByte(session));
            super.doUpdate(session);
    
        }
    
        /**
         * 获取session
         */
        @Override
        protected Session doReadSession(Serializable sessionId) {
            Session session = super.doReadSession(sessionId);
            if(session == null){
                byte[] bytes = (byte[]) get(sessionId.toString());
                if(bytes != null && bytes.length > 0){
                    session = byteToSession(bytes);
                }
            }
            return session;
        }
    
    
        // 把session对象转化为byte保存到缓存中
        public byte[] sessionToByte(Session session){
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            byte[] bytes = null;
            try {
                ObjectOutputStream oo = new ObjectOutputStream(bo);
                oo.writeObject(session);
                bytes = bo.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bytes;
        }
    
        // 把byte还原为session
        public Session byteToSession(byte[] bytes){
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream in;
            SimpleSession session = null;
            try {
                in = new ObjectInputStream(bi);
                session = (SimpleSession) in.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return session;
        }
    }

      

    package com.test.shiro;
    
    import com.jfinal.log.Log;
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    import redis.clients.jedis.Jedis;
    
    import java.util.*;
    
    public class RedisCache <K, V> implements Cache<K, V> {
    
        Log log = Log.getLog(RedisCache.class);
    
        private RedisManage redisManage;
    
        public RedisCache(RedisManage redisManage){
            this.redisManage = redisManage;
        }
    
        private com.jfinal.plugin.redis.Cache getCache(){
            log.info("user cache :" + redisManage.getPrefix());
            return redisManage.getCache();
        }
    
    
        public void clear() throws CacheException {
            // TODO Auto-generated method stub
            getCache().getJedis().flushDB();
        }
    
        public V get(K key) throws CacheException {
            // TODO Auto-generated method stub
            return getCache().get(redisManage.getPrefix() + key);
        }
    
        @SuppressWarnings("unchecked")
        public Set<K> keys() {
            // TODO Auto-generated method stub
            Jedis jedis = getCache().getJedis();
            Set<String> keys = jedis.keys(redisManage.getPrefix() + "*");
            Set<K> ks = new HashSet<K>();
            for (String key : keys) {
                ks.add((K)key);
            }
            return ks;
        }
    
        public V put(K key, V value) throws CacheException {
            // TODO Auto-generated method stub
            getCache().set(redisManage.getPrefix() + key, value);
            return value;
        }
    
        public V remove(K key) throws CacheException {
            // TODO Auto-generated method stub
            V value = getCache().get(redisManage.getPrefix() + key);
            getCache().del(redisManage.getPrefix() + key);
            return value;
        }
    
        public int size() {
            // TODO Auto-generated method stub
            return keys().size();
        }
    
        public Collection<V> values() {
            // TODO Auto-generated method stub
            Set<K> ks = keys();
            List<V> vs = new ArrayList<V>();
            for (K k : ks) {
                vs.add(get(k));
            }
            return vs;
        }
    
    }
    package com.test.shiro;
    
    import com.jfinal.log.Log;
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.cache.CacheException;
    import org.apache.shiro.cache.CacheManager;
    
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    public class RedisCacheManage implements CacheManager {
        private static final Log log = Log.getLog(RedisCacheManage.class);
    
    
        private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
    
        public <K, V> Cache<K, V> getCache(String name) throws CacheException {
            // TODO Auto-generated method stub
            log.info(String.format("获取redis %s 实例", name));
            if(caches.containsKey(name)){
                return caches.get(name);
            }
            RedisCache<K, V> redisCache = new RedisCache<K, V>(new RedisManage(name));
            caches.put(name, redisCache);
            return redisCache;
        }
    }
    package com.test.shiro;
    
    import com.jfinal.plugin.redis.Redis;
    
    public class RedisManage {
        private com.jfinal.plugin.redis.Cache cache;
    
        //用于区分shiro不同的cache name
        private String prefix;
    
        public RedisManage(String cachename) {
            // TODO Auto-generated constructor stub
            this.prefix = cachename + ":";
        }
    
        public com.jfinal.plugin.redis.Cache getCache() {
            if(cache == null){
                //在jfinalConfig中添加redis插件  me.add(new RedisPlugin(Constant.REDIS_SHIROMANAGE_CACHE, "127.0.0.1", 6379));
                //cache = Redis.use(Constant.REDIS_SHIROMANAGE_CACHE);
                cache = Redis.use("jfinalShiro");
            }
            return cache;
        }
    
        public String getPrefix(){
            return this.prefix;
        }
    }
    package com.test.shiro;
    
    
    import com.test.common.model.AuthUser;
    import org.apache.shiro.authc.*;
    import org.apache.shiro.authz.AuthorizationInfo;
    import org.apache.shiro.authz.SimpleAuthorizationInfo;
    import org.apache.shiro.cache.Cache;
    import org.apache.shiro.realm.AuthorizingRealm;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.subject.SimplePrincipalCollection;
    
    public class ShiroDbRealm extends AuthorizingRealm {
        /**
         * 认证回调函数,登录时调用.
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
            UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
            AuthUser user = AuthUser.dao.findByName(token.getUsername());
            if (user != null) {
                return new SimpleAuthenticationInfo(new ShiroUser(user), user.getPassword(), getName());
            } else {
                return null;
            }
        }
    
        /**
         * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            ShiroUser user = (ShiroUser) principals.getPrimaryPrincipal();
    
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    
            return info;
        }
    
        /**
         * 更新用户授权信息缓存.
         */
        public void clearCachedAuthorizationInfo(String principal) {
            SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
            clearCachedAuthorizationInfo(principals);
        }
    
        /**
         * 清除所有用户授权信息缓存.
         */
        public void clearAllCachedAuthorizationInfo() {
            Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
            if (cache != null) {
                for (Object key : cache.keys()) {
                    cache.remove(key);
                }
            }
        }
    }
    public void configPlugin(Plugins plugins) {
    
            plugins.add(new RedisPlugin("jfinalShiro", "ip", 6379,"123456"));
    
        }
  • 相关阅读:
    实验5
    实验4
    实验3
    《整数计算器》
    《写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示》
    《把一个英语句子中的单词次序颠倒后输出》
    个人简介
    学习《构建之法》的感想
    把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
    写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
  • 原文地址:https://www.cnblogs.com/yzssoft/p/10918105.html
Copyright © 2011-2022 走看看