diff --git a/bootdo/pom.xml b/bootdo/pom.xml index fd5fe83..fe1422e 100644 --- a/bootdo/pom.xml +++ b/bootdo/pom.xml @@ -208,11 +208,11 @@ 1.9.2 - - org.crazycake - shiro-redis - 2.4.2.1-RELEASE - + + + + + diff --git a/bootdo/src/main/java/com/bootdo/activiti/controller/ProcessController.java b/bootdo/src/main/java/com/bootdo/activiti/controller/ProcessController.java index 4e54689..056808a 100644 --- a/bootdo/src/main/java/com/bootdo/activiti/controller/ProcessController.java +++ b/bootdo/src/main/java/com/bootdo/activiti/controller/ProcessController.java @@ -2,6 +2,8 @@ import com.bootdo.activiti.service.ProcessService; import com.bootdo.activiti.vo.ProcessVO; +import com.bootdo.common.config.Constant; +import com.bootdo.common.controller.BaseController; import com.bootdo.common.utils.PageUtils; import com.bootdo.common.utils.R; import org.activiti.engine.ActivitiException; @@ -27,7 +29,7 @@ @RequestMapping("activiti/process") @RestController -public class ProcessController { +public class ProcessController extends BaseController{ @Autowired private RepositoryService repositoryService; @@ -114,6 +116,9 @@ public R deploy(String exportDir, String category, MultipartFile file) { */ @RequestMapping(value = "/convertToModel/{procDefId}") public R convertToModel(@PathVariable("procDefId") String procDefId, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException, XMLStreamException { + if (Constant.DEMO_ACCOUNT.equals(getUsername())) { + return R.error(1, "演示系统不允许修改,完整体验请部署程序"); + } org.activiti.engine.repository.Model modelData = null; try { modelData = processService.convertToModel(procDefId); @@ -137,6 +142,9 @@ public void resourceRead(@PathVariable("xml") String resType, @PathVariable("id" @PostMapping("/remove") public R remove(String id){ + if (Constant.DEMO_ACCOUNT.equals(getUsername())) { + return R.error(1, "演示系统不允许修改,完整体验请部署程序"); + } repositoryService.deleteDeployment(id,true); return R.ok(); } diff --git a/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCache.java b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCache.java new file mode 100644 index 0000000..c7be6b1 --- /dev/null +++ b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCache.java @@ -0,0 +1,195 @@ +package com.bootdo.common.redis.shiro; + +/** + * @author bootdo 1992lcg@163.com + * @version V1.0 + */ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheException; +import org.apache.shiro.util.CollectionUtils; +import org.crazycake.shiro.RedisManager; +import org.crazycake.shiro.SerializeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RedisCache implements Cache { + + private Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * The wrapped Jedis instance. + */ + private RedisManager cache; + + /** + * The Redis key prefix for the sessions + */ + private String keyPrefix = "shiro_redis_session:"; + + /** + * Returns the Redis session keys + * prefix. + * @return The prefix + */ + public String getKeyPrefix() { + return keyPrefix; + } + + /** + * Sets the Redis sessions key + * prefix. + * @param keyPrefix The prefix + */ + public void setKeyPrefix(String keyPrefix) { + this.keyPrefix = keyPrefix; + } + + /** + * 通过一个JedisManager实例构造RedisCache + */ + public RedisCache(RedisManager cache){ + if (cache == null) { + throw new IllegalArgumentException("Cache argument cannot be null."); + } + this.cache = cache; + } + + /** + * Constructs a cache instance with the specified + * Redis manager and using a custom key prefix. + * @param cache The cache manager instance + * @param prefix The Redis key prefix + */ + public RedisCache(RedisManager cache, + String prefix){ + + this( cache ); + + // set the prefix + this.keyPrefix = prefix; + } + + /** + * 获得byte[]型的key + * @param key + * @return + */ + private byte[] getByteKey(K key){ + if(key instanceof String){ + String preKey = this.keyPrefix + key; + return preKey.getBytes(); + }else{ + return SerializeUtils.serialize(key); + } + } + + @Override + public V get(K key) throws CacheException { + logger.debug("根据key从Redis中获取对象 key [" + key + "]"); + try { + if (key == null) { + return null; + }else{ + byte[] rawValue = cache.get(getByteKey(key)); + @SuppressWarnings("unchecked") + V value = (V)SerializeUtils.deserialize(rawValue); + return value; + } + } catch (Throwable t) { + throw new CacheException(t); + } + + } + + @Override + public V put(K key, V value) throws CacheException { + logger.debug("根据key从存储 key [" + key + "]"); + try { + cache.set(getByteKey(key), SerializeUtils.serialize(value)); + return value; + } catch (Throwable t) { + throw new CacheException(t); + } + } + + @Override + public V remove(K key) throws CacheException { + logger.debug("从redis中删除 key [" + key + "]"); + try { + V previous = get(key); + cache.del(getByteKey(key)); + return previous; + } catch (Throwable t) { + throw new CacheException(t); + } + } + + @Override + public void clear() throws CacheException { + logger.debug("从redis中删除所有元素"); + try { + cache.flushDB(); + } catch (Throwable t) { + throw new CacheException(t); + } + } + + @Override + public int size() { + try { + Long longSize = new Long(cache.dbSize()); + return longSize.intValue(); + } catch (Throwable t) { + throw new CacheException(t); + } + } + + @SuppressWarnings("unchecked") + @Override + public Set keys() { + try { + Set keys = cache.keys(this.keyPrefix + "*"); + if (CollectionUtils.isEmpty(keys)) { + return Collections.emptySet(); + }else{ + Set newKeys = new HashSet(); + for(byte[] key:keys){ + newKeys.add((K)key); + } + return newKeys; + } + } catch (Throwable t) { + throw new CacheException(t); + } + } + + @Override + public Collection values() { + try { + Set keys = cache.keys(this.keyPrefix + "*"); + if (!CollectionUtils.isEmpty(keys)) { + List values = new ArrayList(keys.size()); + for (byte[] key : keys) { + @SuppressWarnings("unchecked") + V value = get((K)key); + if (value != null) { + values.add(value); + } + } + return Collections.unmodifiableList(values); + } else { + return Collections.emptyList(); + } + } catch (Throwable t) { + throw new CacheException(t); + } + } + +} diff --git a/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCacheManager.java b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCacheManager.java new file mode 100644 index 0000000..c7a2d7a --- /dev/null +++ b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCacheManager.java @@ -0,0 +1,78 @@ +package com.bootdo.common.redis.shiro; + +/** + * @author bootdo 1992lcg@163.com + * @version V1.0 + */ +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheException; +import org.apache.shiro.cache.CacheManager; +import org.crazycake.shiro.RedisManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RedisCacheManager implements CacheManager { + + private static final Logger logger = LoggerFactory + .getLogger(RedisCacheManager.class); + + // fast lookup by name map + private final ConcurrentMap caches = new ConcurrentHashMap(); + + private RedisManager redisManager; + + /** + * The Redis key prefix for caches + */ + private String keyPrefix = "shiro_redis_cache:"; + + /** + * Returns the Redis session keys + * prefix. + * @return The prefix + */ + public String getKeyPrefix() { + return keyPrefix; + } + + /** + * Sets the Redis sessions key + * prefix. + * @param keyPrefix The prefix + */ + public void setKeyPrefix(String keyPrefix) { + this.keyPrefix = keyPrefix; + } + + @Override + public Cache getCache(String name) throws CacheException { + logger.debug("获取名称为: " + name + " 的RedisCache实例"); + + Cache c = caches.get(name); + + if (c == null) { + + // initialize the Redis manager instance + redisManager.init(); + + // create a new cache instance + c = new RedisCache(redisManager, keyPrefix); + + // add it to the cache collection + caches.put(name, c); + } + return c; + } + + public RedisManager getRedisManager() { + return redisManager; + } + + public void setRedisManager(RedisManager redisManager) { + this.redisManager = redisManager; + } + +} diff --git a/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisManager.java b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisManager.java new file mode 100644 index 0000000..8f50ea8 --- /dev/null +++ b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisManager.java @@ -0,0 +1,201 @@ +package com.bootdo.common.redis.shiro; + +/** + * @author bootdo 1992lcg@163.com + * @version V1.0 + */ +import java.util.Set; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +public class RedisManager { + + private String host = "127.0.0.1"; + + private int port = 6379; + + // 0 - never expire + private int expire = 0; + + //timeout for jedis try to connect to redis server, not expire time! In milliseconds + private int timeout = 0; + + private String password = ""; + + private static JedisPool jedisPool = null; + + public RedisManager(){ + + } + + /** + * 初始化方法 + */ + public void init(){ + if(jedisPool == null){ + if(password != null && !"".equals(password)){ + jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password); + }else if(timeout != 0){ + jedisPool = new JedisPool(new JedisPoolConfig(), host, port,timeout); + }else{ + jedisPool = new JedisPool(new JedisPoolConfig(), host, port); + } + + } + } + + /** + * get value from redis + * @param key + * @return + */ + public byte[] get(byte[] key){ + byte[] value = null; + Jedis jedis = jedisPool.getResource(); + try{ + value = jedis.get(key); + }finally{ + jedisPool.returnResource(jedis); + } + return value; + } + + /** + * set + * @param key + * @param value + * @return + */ + public byte[] set(byte[] key,byte[] value){ + Jedis jedis = jedisPool.getResource(); + try{ + jedis.set(key,value); + if(this.expire != 0){ + jedis.expire(key, this.expire); + } + }finally{ + jedisPool.returnResource(jedis); + } + return value; + } + + /** + * set + * @param key + * @param value + * @param expire + * @return + */ + public byte[] set(byte[] key,byte[] value,int expire){ + Jedis jedis = jedisPool.getResource(); + try{ + jedis.set(key,value); + if(expire != 0){ + jedis.expire(key, expire); + } + }finally{ + jedisPool.returnResource(jedis); + } + return value; + } + + /** + * del + * @param key + */ + public void del(byte[] key){ + Jedis jedis = jedisPool.getResource(); + try{ + jedis.del(key); + }finally{ + jedisPool.returnResource(jedis); + } + } + + /** + * flush + */ + public void flushDB(){ + Jedis jedis = jedisPool.getResource(); + try{ + jedis.flushDB(); + }finally{ + jedisPool.returnResource(jedis); + } + } + + /** + * size + */ + public Long dbSize(){ + Long dbSize = 0L; + Jedis jedis = jedisPool.getResource(); + try{ + dbSize = jedis.dbSize(); + }finally{ + jedisPool.returnResource(jedis); + } + return dbSize; + } + + /** + * keys + * @param regex + * @return + */ + public Set keys(String pattern){ + Set keys = null; + Jedis jedis = jedisPool.getResource(); + try{ + keys = jedis.keys(pattern.getBytes()); + }finally{ + jedisPool.returnResource(jedis); + } + return keys; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getExpire() { + return expire; + } + + public void setExpire(int expire) { + this.expire = expire; + } + + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + + +} diff --git a/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisSessionDAO.java b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisSessionDAO.java new file mode 100644 index 0000000..39bc964 --- /dev/null +++ b/bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisSessionDAO.java @@ -0,0 +1,140 @@ +package com.bootdo.common.redis.shiro; + +import org.apache.shiro.session.Session; +import org.apache.shiro.session.UnknownSessionException; +import org.apache.shiro.session.mgt.eis.AbstractSessionDAO; +import org.crazycake.shiro.SerializeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * @author bootdo 1992lcg@163.com + * @version V1.0 + */ +public class RedisSessionDAO extends AbstractSessionDAO { + + private static Logger logger = LoggerFactory.getLogger(RedisSessionDAO.class); + /** + * shiro-redis的session对象前缀 + */ + private RedisManager redisManager; + + /** + * The Redis key prefix for the sessions + */ + private String keyPrefix = "shiro_redis_session:"; + + @Override + public void update(Session session) throws UnknownSessionException { + this.saveSession(session); + } + + /** + * save session + * @param session + * @throws UnknownSessionException + */ + private void saveSession(Session session) throws UnknownSessionException{ + if(session == null || session.getId() == null){ + logger.error("session or session id is null"); + return; + } + + byte[] key = getByteKey(session.getId()); + byte[] value = SerializeUtils.serialize(session); + session.setTimeout(redisManager.getExpire()*1000); + this.redisManager.set(key, value, redisManager.getExpire()); + } + + @Override + public void delete(Session session) { + if(session == null || session.getId() == null){ + logger.error("session or session id is null"); + return; + } + redisManager.del(this.getByteKey(session.getId())); + + } + + @Override + public Collection getActiveSessions() { + Set sessions = new HashSet(); + + Set keys = redisManager.keys(this.keyPrefix + "*"); + if(keys != null && keys.size()>0){ + for(byte[] key:keys){ + Session s = (Session)SerializeUtils.deserialize(redisManager.get(key)); + sessions.add(s); + } + } + + return sessions; + } + + @Override + protected Serializable doCreate(Session session) { + Serializable sessionId = this.generateSessionId(session); + this.assignSessionId(session, sessionId); + this.saveSession(session); + return sessionId; + } + + @Override + protected Session doReadSession(Serializable sessionId) { + if(sessionId == null){ + logger.error("session id is null"); + return null; + } + + Session s = (Session)SerializeUtils.deserialize(redisManager.get(this.getByteKey(sessionId))); + return s; + } + + /** + * 获得byte[]型的key + * @param key + * @return + */ + private byte[] getByteKey(Serializable sessionId){ + String preKey = this.keyPrefix + sessionId; + return preKey.getBytes(); + } + + public RedisManager getRedisManager() { + return redisManager; + } + + public void setRedisManager(RedisManager redisManager) { + this.redisManager = redisManager; + + /** + * 初始化redisManager + */ + this.redisManager.init(); + } + + /** + * Returns the Redis session keys + * prefix. + * @return The prefix + */ + public String getKeyPrefix() { + return keyPrefix; + } + + /** + * Sets the Redis sessions key + * prefix. + * @param keyPrefix The prefix + */ + public void setKeyPrefix(String keyPrefix) { + this.keyPrefix = keyPrefix; + } + + +} \ No newline at end of file diff --git a/bootdo/src/main/java/com/bootdo/common/redis/shiro/SerializeUtils.java b/bootdo/src/main/java/com/bootdo/common/redis/shiro/SerializeUtils.java new file mode 100644 index 0000000..68db5a9 --- /dev/null +++ b/bootdo/src/main/java/com/bootdo/common/redis/shiro/SerializeUtils.java @@ -0,0 +1,89 @@ +package com.bootdo.common.redis.shiro; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author bootdo 1992lcg@163.com + * @version V1.0 + */ +public class SerializeUtils { + + private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class); + + /** + * 反序列化 + * @param bytes + * @return + */ + public static Object deserialize(byte[] bytes) { + + Object result = null; + + if (isEmpty(bytes)) { + return null; + } + + try { + ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); + try { + ObjectInputStream objectInputStream = new ObjectInputStream(byteStream); + try { + result = objectInputStream.readObject(); + } + catch (ClassNotFoundException ex) { + throw new Exception("Failed to deserialize object type", ex); + } + } + catch (Throwable ex) { + throw new Exception("Failed to deserialize", ex); + } + } catch (Exception e) { + logger.error("Failed to deserialize",e); + } + return result; + } + + public static boolean isEmpty(byte[] data) { + return (data == null || data.length == 0); + } + + /** + * 序列化 + * @param object + * @return + */ + public static byte[] serialize(Object object) { + + byte[] result = null; + + if (object == null) { + return new byte[0]; + } + try { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128); + try { + if (!(object instanceof Serializable)) { + throw new IllegalArgumentException(SerializeUtils.class.getSimpleName() + " requires a Serializable payload " + + "but received an object of type [" + object.getClass().getName() + "]"); + } + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream); + objectOutputStream.writeObject(object); + objectOutputStream.flush(); + result = byteStream.toByteArray(); + } + catch (Throwable ex) { + throw new Exception("Failed to serialize", ex); + } + } catch (Exception ex) { + logger.error("Failed to serialize",ex); + } + return result; + } +} \ No newline at end of file