From fb5f63a9de885e23a58f3175dba0d880b146d2ac Mon Sep 17 00:00:00 2001 From: qi_liang Date: Mon, 10 Jun 2019 10:48:30 +0800 Subject: [PATCH] =?UTF-8?q?shiro=20cacheManager=20=E9=87=87=E7=94=A8redis?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 23 +++++- .../lq/cms/service/SysResourceService.java | 6 +- .../cms/web/wechat/WechatRuleController.java | 4 +- .../com/lq/code/cache/RedisShiroCache.java | 70 +++++++++++++++++ .../com/lq/code/cache/RedisShiroManager.java | 23 ++++++ src/main/java/com/lq/code/dto/QueueDto.java | 55 ++++++++++++++ ...InstantiationTracingBeanPostProcessor.java | 7 +- src/main/java/com/lq/code/util/BeanUtil.java | 25 +++++- .../lq/code/util/sql/AbstractDbBuiler.java | 4 + .../com/lq/code/util/sql/MysqlBuilder.java | 76 ++++++++++++++++--- .../com/lq/code/util/sql/OracleBuiler.java | 7 ++ src/main/resources/dev/redis.properties | 4 +- src/main/resources/spring/spring-redis.xml | 4 + src/main/resources/spring/spring-shiro.xml | 28 ++++--- src/main/webapp/WEB-INF/views/jsp/index.jsp | 20 ++--- src/main/webapp/WEB-INF/web.xml | 16 ++++ 16 files changed, 326 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/lq/code/cache/RedisShiroCache.java create mode 100644 src/main/java/com/lq/code/cache/RedisShiroManager.java create mode 100644 src/main/java/com/lq/code/dto/QueueDto.java diff --git a/pom.xml b/pom.xml index c77d21b..bfb8986 100644 --- a/pom.xml +++ b/pom.xml @@ -173,6 +173,20 @@ ${spring.version} + + + org.springframework.session + spring-session-data-redis + 2.1.4.RELEASE + + + + org.springframework.data + spring-data-commons + 2.1.4.RELEASE + + + org.aspectj aspectjrt @@ -309,7 +323,7 @@ org.springframework.data spring-data-redis - 1.6.2.RELEASE + 2.1.6.RELEASE @@ -499,6 +513,13 @@ 3.0.1 + + + org.apache.commons + commons-pool2 + 2.6.2 + + diff --git a/src/main/java/com/lq/cms/service/SysResourceService.java b/src/main/java/com/lq/cms/service/SysResourceService.java index 8ed2af0..efbe19a 100755 --- a/src/main/java/com/lq/cms/service/SysResourceService.java +++ b/src/main/java/com/lq/cms/service/SysResourceService.java @@ -4,8 +4,6 @@ import com.lq.cms.mode.MenusComposite; import com.lq.cms.vo.SysResourceVo; import com.lq.code.service.BaseService; import com.lq.entity.SysResource; - -import javax.servlet.http.HttpServletRequest; import java.util.List; /** @@ -18,9 +16,9 @@ public interface SysResourceService extends BaseService { List findByParentId(long parenId); //通过用户查找相关的菜单 - List findMenusListBySysUserId(Long sysUserId, HttpServletRequest request); + List findMenusListBySysUserId(Long sysUserId,String contextPath); //用户拥有admin权限查询全部菜单 - List findAllMenusList(HttpServletRequest request); + List findAllMenusList(String contextPath); } diff --git a/src/main/java/com/lq/cms/web/wechat/WechatRuleController.java b/src/main/java/com/lq/cms/web/wechat/WechatRuleController.java index 236f8f9..4d54c02 100755 --- a/src/main/java/com/lq/cms/web/wechat/WechatRuleController.java +++ b/src/main/java/com/lq/cms/web/wechat/WechatRuleController.java @@ -54,18 +54,18 @@ public class WechatRuleController { * @param vo * @param keywordListStr 关键字集合(json字符串) * @param messageListStr 消息集合(json字符串) - * @param wechatInfoId 微信公众号详情ID * @return */ @ResponseBody @RequestMapping("/save") - public Object save(WechatRuleVo vo,String keywordListStr,String messageListStr,Long wechatInfoId){ + public Object save(WechatRuleVo vo,String keywordListStr,String messageListStr){ AjaxResult ajaxResult = new AjaxResult(); if (StringUtil.isNotNull(keywordListStr)) { List wechatKeywordList = JSON.parseArray(keywordListStr, WechatKeyword.class); vo.setWechatKeywordList(wechatKeywordList); } wechatRuleService.saveRule(vo); + return ajaxResult; } diff --git a/src/main/java/com/lq/code/cache/RedisShiroCache.java b/src/main/java/com/lq/code/cache/RedisShiroCache.java new file mode 100644 index 0000000..95155db --- /dev/null +++ b/src/main/java/com/lq/code/cache/RedisShiroCache.java @@ -0,0 +1,70 @@ +package com.lq.code.cache; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; + +import java.util.Collection; +import java.util.Set; + +/** + * @Author: qi + * @Description: + * @Date: Create in 10:07 AM 2019/6/10 + */ +public class RedisShiroCache implements Cache{ + + @Autowired + private RedisTemplate redisTemplate; + + public RedisTemplate getRedisTemplate() { + return redisTemplate; + } + + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + @Override + public V get(K k) throws CacheException { + + return redisTemplate.opsForValue().get(k); + } + + @Override + public V put(K k, V v) throws CacheException { + redisTemplate.opsForValue().set(k,v); + return v; + } + + @Override + public V remove(K k) throws CacheException { + V v = redisTemplate.opsForValue().get(k); + if (redisTemplate.delete(k)){ + return v; + } + + return v; + } + + @Override + public void clear() throws CacheException { + + } + + @Override + public int size() { + return 0; + } + + @Override + public Set keys() { + return null; + } + + @Override + public Collection values() { + return null; + } +} diff --git a/src/main/java/com/lq/code/cache/RedisShiroManager.java b/src/main/java/com/lq/code/cache/RedisShiroManager.java new file mode 100644 index 0000000..0f84b1a --- /dev/null +++ b/src/main/java/com/lq/code/cache/RedisShiroManager.java @@ -0,0 +1,23 @@ +package com.lq.code.cache; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheException; +import org.apache.shiro.cache.CacheManager; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @Author: qi + * @Description: + * @Date: Create in 10:15 PM 2019/6/8 + */ +public class RedisShiroManager implements CacheManager { + + @Autowired + private RedisShiroCache redisShiroCache; + + @Override + public Cache getCache(String s) throws CacheException { + + return redisShiroCache; + } +} diff --git a/src/main/java/com/lq/code/dto/QueueDto.java b/src/main/java/com/lq/code/dto/QueueDto.java new file mode 100644 index 0000000..262fcb5 --- /dev/null +++ b/src/main/java/com/lq/code/dto/QueueDto.java @@ -0,0 +1,55 @@ +package com.lq.code.dto; + +import com.github.jsonzou.jmockdata.JMockData; +import com.lq.entity.SysUser; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadPoolExecutor; + +/** + * @Author: qi + * @Description:自定义队列 + * @Date: Create in 9:23 AM 2019/6/3 + */ +public class QueueDto { + + private List list; + + private int index; + + public QueueDto() { + this.list = new ArrayList<>(); + } + + public QueueDto(int size) { + this.list = new ArrayList<>(size); + } + + public void add(T t){ + list.add(t); + index++; + } + + public synchronized T pop(){ + T t = null; + index--; + if (hasNext()) { + t = list.get(index); + } + return t; + } + + /** + * 判断是否还有元素存在 + * @return + */ + public boolean hasNext(){ + boolean result = true; + if (index==0){ + result = false; + } + return result; + } + +} diff --git a/src/main/java/com/lq/code/executor/processor/InstantiationTracingBeanPostProcessor.java b/src/main/java/com/lq/code/executor/processor/InstantiationTracingBeanPostProcessor.java index c9efd63..1995832 100755 --- a/src/main/java/com/lq/code/executor/processor/InstantiationTracingBeanPostProcessor.java +++ b/src/main/java/com/lq/code/executor/processor/InstantiationTracingBeanPostProcessor.java @@ -41,10 +41,13 @@ public class InstantiationTracingBeanPostProcessor implements ApplicationListene //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。 //扫描实体类 if (isUpdateDB) { - Set set = BeanUtil.getClassList(packagePath); + long startTime = System.currentTimeMillis(); + Set set = BeanUtil.getClassSet(packagePath); AbstractDbBuiler dbBuiler = new MysqlBuilder(); String sql = dbBuiler.automaticUpdateDb(set); - JdbcUtils.createTable(sql); + // JdbcUtils.createTable(sql); + System.out.println("=========实体同步数据结构==========="); + System.out.println(System.currentTimeMillis()-startTime); } } } diff --git a/src/main/java/com/lq/code/util/BeanUtil.java b/src/main/java/com/lq/code/util/BeanUtil.java index 21c7fa9..c38fdeb 100755 --- a/src/main/java/com/lq/code/util/BeanUtil.java +++ b/src/main/java/com/lq/code/util/BeanUtil.java @@ -1,5 +1,6 @@ package com.lq.code.util; +import com.lq.code.dto.QueueDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -161,7 +162,7 @@ public class BeanUtil { * @param packagePath * @return */ - public static Set getClassList(String packagePath){ + public static Set getClassSet(String packagePath){ Set set = new HashSet<>(); String separator = File.separator; @@ -185,6 +186,28 @@ public class BeanUtil { return set; } + public static QueueDto getQueueDto(String packagePath){ + QueueDto classQueueDto = new QueueDto<>(); + String separator = File.separator; + String filePath = packagePath.replace(".",separator); + String classPath = BeanUtil.class.getResource(FILE_SYMBOL).getPath()+filePath; + File file = new File(classPath); + if (file.exists()){ + File[] files = file.listFiles(); + try { + for (File classFile:files){ + String className = classFile.getName().split("\\.")[0]; + Class clazz =Class.forName(packagePath+"."+className); + classQueueDto.add(clazz); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + + } + } + return classQueueDto; + } + public static String getEntityName(Class entityClazz){ String entityPath=entityClazz.getName(); String [] entityNameArray=entityPath.split("\\."); diff --git a/src/main/java/com/lq/code/util/sql/AbstractDbBuiler.java b/src/main/java/com/lq/code/util/sql/AbstractDbBuiler.java index 2aa5f87..f88a883 100755 --- a/src/main/java/com/lq/code/util/sql/AbstractDbBuiler.java +++ b/src/main/java/com/lq/code/util/sql/AbstractDbBuiler.java @@ -1,5 +1,7 @@ package com.lq.code.util.sql; +import com.lq.code.dto.QueueDto; + import java.util.Set; /** @@ -10,6 +12,8 @@ public abstract class AbstractDbBuiler { abstract public String automaticUpdateDb(Set classSet); + abstract public String automaticUpdateDbNew(QueueDto classQueueDto); + /** *  构建分页sql语句 * @param sql diff --git a/src/main/java/com/lq/code/util/sql/MysqlBuilder.java b/src/main/java/com/lq/code/util/sql/MysqlBuilder.java index ff2547e..bf4e9cf 100755 --- a/src/main/java/com/lq/code/util/sql/MysqlBuilder.java +++ b/src/main/java/com/lq/code/util/sql/MysqlBuilder.java @@ -1,6 +1,7 @@ package com.lq.code.util.sql; import com.lq.code.annotation.Length; +import com.lq.code.dto.QueueDto; import com.lq.code.util.BeanUtil; import com.lq.code.util.jdbc.JdbcUtils; import com.lq.code.util.jdbc.mode.Column; @@ -8,6 +9,7 @@ import com.lq.code.util.jdbc.mode.Table; import java.lang.reflect.Field; import java.util.*; +import java.util.concurrent.*; /** * Created by qi_liang on 2018/6/2. @@ -31,17 +33,13 @@ public class MysqlBuilder extends AbstractDbBuiler { public String automaticUpdateDb(Set classSet) { StringBuffer sql = new StringBuffer(); //扫描实体类包的实体class - Set set = classSet; - Map map = new HashMap<>(); - Iterator setIterator = set.iterator(); - while (setIterator.hasNext()){ - Class clazz = setIterator.next(); + Map map = new HashMap<>(classSet.size()); + classSet.forEach(clazz->{ map.put(SqlUtil.beanNameToTableName(clazz), clazz); - } - + }); List tableList = JdbcUtils.getAllTable(); //初始化的时候,tableList可能为null - if(tableList!=null) { + if(tableList!=null&&tableList.size()>0) { Iterator
tableIterator = tableList.iterator(); while (tableIterator.hasNext()) { Table table = (Table) tableIterator.next(); @@ -90,10 +88,68 @@ public class MysqlBuilder extends AbstractDbBuiler { } } } - for (String key : map.keySet()) { - sql.append(this.createTableStr(map.get(key))); + + for (Map.Entry entry:map.entrySet()){ + sql.append(this.createTableStr(entry.getValue())); } + return sql.toString(); + } + @Override + public String automaticUpdateDbNew(QueueDto classQueueDto) { + StringBuffer sql = new StringBuffer(); + List
tableList = JdbcUtils.getAllTable(); + Map tableMap = new HashMap<>(tableList.size()); + tableList.forEach((table)->{ + tableMap.put(table.getTableName(),table); + }); + + ExecutorService executorService = Executors.newFixedThreadPool(6); + while (classQueueDto.hasNext()){ + executorService.execute(()->{ + Class clazz = classQueueDto.pop(); + String tableName = SqlUtil.beanNameToTableName(clazz); + //判断表是否存在 + if (tableMap.containsKey(tableName)){ + List fieldsList = BeanUtil.getAllField(clazz); + Map clazzMap = new HashMap<>(fieldsList.size()); + fieldsList.forEach((field)->{ + clazzMap.put(SqlUtil.caseToHump(field.getName()),field); + }); + List columnList = tableMap.get(tableName).getColumns(); + Iterator columnIterator = columnList.iterator(); + while (columnIterator.hasNext()) { + Column column = columnIterator.next(); + if (clazzMap.containsKey(column.getColumnName())) { + clazzMap.remove(column.getColumnName()); + columnIterator.remove(); + } + } + columnIterator = columnList.iterator(); + while (columnIterator.hasNext()) { + Column column = columnIterator.next(); + sql.append("ALTER TABLE " + tableName + " DROP COLUMN " + column.getColumnName() + ";"); + } + for (Map.Entry entry:clazzMap.entrySet()){ + Field f = entry.getValue(); + String column = SqlUtil.caseToHump(f.getName()); + String columnType = dataTypeMap.get(f.getGenericType().toString()); + String lengthStr = ""; + if ("varchar".equals(columnType) || "int".equals(columnType)) { + Length length = f.getAnnotation(Length.class); + if (length != null) { + lengthStr = "(" + length.value() + ")"; + } else { + lengthStr = "(50)"; + } + } + sql.append("ALTER TABLE " + tableName + " ADD COLUMN " + column + " " + columnType + lengthStr + ";"); + } + }else { + sql.append(this.createTableStr(clazz)); + } + }); + } return sql.toString(); } diff --git a/src/main/java/com/lq/code/util/sql/OracleBuiler.java b/src/main/java/com/lq/code/util/sql/OracleBuiler.java index 53e95e8..22a958a 100755 --- a/src/main/java/com/lq/code/util/sql/OracleBuiler.java +++ b/src/main/java/com/lq/code/util/sql/OracleBuiler.java @@ -1,5 +1,7 @@ package com.lq.code.util.sql; +import com.lq.code.dto.QueueDto; + import java.util.Set; /** @@ -13,6 +15,11 @@ public class OracleBuiler extends AbstractDbBuiler { return null; } + @Override + public String automaticUpdateDbNew(QueueDto classQueueDto) { + return null; + } + @Override public String concatPageSql(String sql, PageInterface pageInterface) { StringBuffer sb=new StringBuffer("select * from (select t1.*,ROWNUM rm from("); diff --git a/src/main/resources/dev/redis.properties b/src/main/resources/dev/redis.properties index 3cd757e..6e9da90 100644 --- a/src/main/resources/dev/redis.properties +++ b/src/main/resources/dev/redis.properties @@ -3,8 +3,8 @@ redis.port=6379 redis.pass= redis.timeout=-1 -redis.maxTotal=30 -redis.maxIdle=10 +redis.maxTotal=10 +redis.maxIdle=5 redis.numTestsPerEvictionRun=1024 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=1800000 diff --git a/src/main/resources/spring/spring-redis.xml b/src/main/resources/spring/spring-redis.xml index ce34580..020c3ff 100644 --- a/src/main/resources/spring/spring-redis.xml +++ b/src/main/resources/spring/spring-redis.xml @@ -64,6 +64,10 @@ p:poolConfig-ref="jedisConfig" /> + + + + diff --git a/src/main/resources/spring/spring-shiro.xml b/src/main/resources/spring/spring-shiro.xml index bb622dc..4885acc 100644 --- a/src/main/resources/spring/spring-shiro.xml +++ b/src/main/resources/spring/spring-shiro.xml @@ -18,20 +18,11 @@ - - - - - - - - - - + @@ -40,9 +31,9 @@ + - @@ -138,11 +129,18 @@ --> - - - + + + + + + + + diff --git a/src/main/webapp/WEB-INF/views/jsp/index.jsp b/src/main/webapp/WEB-INF/views/jsp/index.jsp index ee1af91..30ed231 100755 --- a/src/main/webapp/WEB-INF/views/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/views/jsp/index.jsp @@ -32,16 +32,18 @@ // doucument.cookie = 'bb=bbbbbb'; $("#btn").click(function () { alert("测试!"); - wx.miniProgram.getEnv(function (res) { - if (res.miniprogram) { - //如果当前是小程序环境 - wx.miniProgram.postMessage({ - data: { - name: 'name', - age: 12 - } - }) + wx.miniProgram.navigateTo({ + url:'/pages/test/test', + success: function(){ + console.log('success') + }, + fail: function(){ + console.log('fail'); + }, + complete:function(){ + console.log('complete'); } + }); }) diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 84cfb21..9920663 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -32,6 +32,7 @@ org.springframework.web.context.ContextLoaderListener + CharacterEncodingFilter @@ -67,6 +68,21 @@ + + + springSessionRepositoryFilter + org.springframework.web.filter.DelegatingFilterProxy + + exclusions + /public/*,*.js,*.css,*.jsp,*.swf + + + + springSessionRepositoryFilter + /* + + + MVC-dispatcher