浏览代码

优化框架,添加websocket,修复权限url匹配错误的bug,文件工具类添加一些用法,移除MQ配置文件并添加msg配置文件,添加自定义的数据权限拦截器,添加websocket测试controller等

NorthLan 6 年之前
父节点
当前提交
e9fdffa218
共有 32 个文件被更改,包括 148 次插入95 次删除
  1. 3 1
      build.gradle
  2. 0 2
      settings.gradle
  3. 0 1
      zen-api/build.gradle
  4. 43 0
      zen-api/src/main/kotlin/com/gxzc/zen/msg/ws/config/WebSocketConfig.kt
  5. 12 0
      zen-api/src/main/kotlin/com/gxzc/zen/msg/ws/config/WebSocketProperties.kt
  6. 4 4
      zen-api/src/main/kotlin/com/gxzc/zen/orm/Generator.kt
  7. 7 6
      zen-api/src/main/kotlin/com/gxzc/zen/orm/config/CustomMetaObjectHandler.kt
  8. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/config/MybatisPlusConfig.kt
  9. 1 1
      zen-api/src/main/kotlin/com/gxzc/zen/orm/config/ZenSqlInjector.kt
  10. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DAHelper.kt
  11. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthority.kt
  12. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthorityBuilder.kt
  13. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/interceptor/ZenDataAuthorityInterceptor.kt
  14. 0 0
      zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/visitor/DataAuthoritySelectVisitor.kt
  15. 1 0
      zen-api/src/main/kotlin/com/gxzc/zen/umps/config/ShiroConfig.kt
  16. 2 1
      zen-api/src/main/kotlin/com/gxzc/zen/umps/filter/UrlPermissionsFilter.kt
  17. 4 4
      zen-api/src/main/kotlin/com/gxzc/zen/umps/util/SSOUtil.kt
  18. 0 0
      zen-api/src/main/resources/templates/entity.java.vm
  19. 0 0
      zen-api/src/main/resources/templates/entity.kt.vm
  20. 0 0
      zen-api/src/main/resources/templates/mapper.java.vm
  21. 0 0
      zen-api/src/main/resources/templates/service.java.vm
  22. 0 0
      zen-api/src/main/resources/templates/serviceImpl.java.vm
  23. 3 1
      zen-core/src/main/kotlin/com/gxzc/zen/common/config/response/ZenJacksonJsonFilter.kt
  24. 25 1
      zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt
  25. 0 17
      zen-mq/build.gradle
  26. 0 18
      zen-orm/build.gradle
  27. 0 27
      zen-orm/src/test/kotlin/com/gxzc/zen/generator/TestPathFinder.kt
  28. 0 2
      zen-web/build.gradle
  29. 30 0
      zen-web/src/main/kotlin/com/gxzc/zen/ws/sys/controller/WebSocketTestController.kt
  30. 5 1
      zen-web/src/main/resources/application-msg.yml
  31. 2 2
      zen-web/src/main/resources/application-orm.yml
  32. 6 6
      zen-web/src/main/resources/application.yml

+ 3 - 1
build.gradle

@@ -1,6 +1,6 @@
 buildscript {
     ext {
-        kotlin_version = '1.2.41'
+        kotlin_version = '1.2.51'
         spring_boot_version = '1.5.10.RELEASE'
         junit_version = '4.12'
         commons_io_version = '2.5'
@@ -79,6 +79,8 @@ subprojects {
         compile('org.springframework.boot:spring-boot-starter')
         compile('org.springframework.boot:spring-boot-starter-web')
         compile('org.springframework.boot:spring-boot-starter-aop')
+        compile('org.springframework.boot:spring-boot-starter-actuator')
+        compile('org.springframework.boot:spring-boot-starter-websocket')
 //        compile('org.springframework.boot:spring-boot-starter-amqp')
 //        compile('org.springframework.boot:spring-boot-starter-jta-atomikos')
         compile('org.springframework.boot:spring-boot-starter-cache')

+ 0 - 2
settings.gradle

@@ -1,8 +1,6 @@
 rootProject.name = 'zen'
 include 'zen-core'
-include 'zen-orm'
 include 'zen-api'
 include 'zen-web'
-include 'zen-mq'
 include 'zen-job'
 

+ 0 - 1
zen-api/build.gradle

@@ -11,7 +11,6 @@ buildscript {
 
 
 dependencies {
-    compile project(":zen-orm")
     compile project(":zen-core")
 }
 

+ 43 - 0
zen-api/src/main/kotlin/com/gxzc/zen/msg/ws/config/WebSocketConfig.kt

@@ -0,0 +1,43 @@
+package com.gxzc.zen.msg.ws.config
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
+import org.springframework.context.annotation.Configuration
+import org.springframework.messaging.simp.config.MessageBrokerRegistry
+import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry
+
+/**
+ * WebSocket 配置
+ * @author NorthLan
+ * @date 2018/7/30
+ * @url https://noahlan.com
+ */
+@Configuration
+@EnableWebSocketMessageBroker
+@ConditionalOnProperty(prefix = "ws", name = ["enabled"], havingValue = "true", matchIfMissing = true)
+//@EnableConfigurationProperties(WebSocketProperties::class)
+class WebSocketConfig : AbstractWebSocketMessageBrokerConfigurer() {
+
+    //    @Autowired
+    //    private lateinit var webSocketProperties: WebSocketProperties
+
+    override fun configureMessageBroker(registry: MessageBrokerRegistry) {
+        /**
+         * 客户端需订阅如下地址的消息
+         * 服务端通过 @SendTo 或者使用SimpMessageTemplate来发送消息
+         */
+        registry.enableSimpleBroker("/topic", "/user")
+        /**
+         * 客户端需用如下地址作为前缀发送消息,经由Broker转发
+         */
+        registry.setApplicationDestinationPrefixes("/app", "/web")
+    }
+
+    override fun registerStompEndpoints(registry: StompEndpointRegistry) {
+        /**
+         * 以 /ws 作为 Stomp 端点,对外暴露
+         */
+        registry.addEndpoint("/ws").setAllowedOrigins("*")
+    }
+}

+ 12 - 0
zen-api/src/main/kotlin/com/gxzc/zen/msg/ws/config/WebSocketProperties.kt

@@ -0,0 +1,12 @@
+package com.gxzc.zen.msg.ws.config
+
+/**
+ * WebSocket 配置类 暂无用
+ * @author NorthLan
+ * @date 2018/7/31
+ * @url https://noahlan.com
+ */
+//@ConfigurationProperties(prefix = "ws")
+open class WebSocketProperties {
+
+}

+ 4 - 4
zen-orm/src/main/kotlin/com/gxzc/zen/Generator.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/Generator.kt

@@ -1,4 +1,4 @@
-package com.gxzc.zen
+package com.gxzc.zen.orm
 
 import com.baomidou.mybatisplus.generator.AutoGenerator
 import com.baomidou.mybatisplus.generator.InjectionConfig
@@ -28,9 +28,9 @@ fun main(args: Array<String>) {
         it.username = "archives"
         it.password = "archives"
         it.url = "jdbc:mysql://192.168.1.10:3306/archives_sys?characterEncoding=utf8"
-//        it.url = "jdbc:mysql://127.0.0.1:3306/archives_mgr?characterEncoding=utf8"
-//        it.username = "root"
-//        it.password = "root"
+        //        it.url = "jdbc:mysql://127.0.0.1:3306/archives_mgr?characterEncoding=utf8"
+        //        it.username = "root"
+        //        it.password = "root"
     })
 }
 

+ 7 - 6
zen-web/src/main/kotlin/com/gxzc/zen/orm/CustomMetaObjectHandler.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/config/CustomMetaObjectHandler.kt

@@ -1,4 +1,4 @@
-package com.gxzc.zen.orm
+package com.gxzc.zen.orm.config
 
 import com.baomidou.mybatisplus.mapper.MetaObjectHandler
 import com.gxzc.zen.umps.util.SSOUtil
@@ -9,6 +9,7 @@ import java.util.*
 
 /**
  * 公共字段自动填充
+ * 多线程 + WebSocket 下不适用
  * @author NorthLan
  * @date 2018/1/31
  * @url https://noahlan.com
@@ -21,9 +22,8 @@ class CustomMetaObjectHandler : MetaObjectHandler() {
 
     override fun insertFill(metaObject: MetaObject?) {
         try {
-            val curUser = SSOUtil.getCurAccount()
-            logger.trace("公共字段自动填充: insertFill $curUser")
             if (getFieldValByName("createBy", metaObject) == null) {
+                val curUser = SSOUtil.getCurAccount()
                 setFieldValByName("createBy", curUser, metaObject)
             }
             if (getFieldValByName("createTime", metaObject) == null) {
@@ -36,9 +36,10 @@ class CustomMetaObjectHandler : MetaObjectHandler() {
 
     override fun updateFill(metaObject: MetaObject?) {
         try {
-            val curUser = SSOUtil.getCurAccount()
-            logger.trace("公共字段自动填充: updateFill $curUser")
-            setFieldValByName("updateBy", curUser, metaObject)
+            if (getFieldValByName("updateBy", metaObject) == null) {
+                val curUser = SSOUtil.getCurAccount()
+                setFieldValByName("updateBy", curUser, metaObject)
+            }
             setFieldValByName("updateTime", Date(), metaObject)
         } catch (e: Throwable) {
             logger.warn("updateFill error, cause: ", e)

+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/config/MybatisPlusConfig.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/config/MybatisPlusConfig.kt


+ 1 - 1
zen-orm/src/main/kotlin/com/gxzc/zen/orm/sql/ZenSqlInjector.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/config/ZenSqlInjector.kt

@@ -1,4 +1,4 @@
-package com.gxzc.zen.orm.sql
+package com.gxzc.zen.orm.config
 
 import com.baomidou.mybatisplus.entity.TableInfo
 import com.baomidou.mybatisplus.enums.FieldFill

+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/data/authority/DAHelper.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DAHelper.kt


+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthority.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthority.kt


+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthorityBuilder.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/DataAuthorityBuilder.kt


+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/data/authority/interceptor/ZenDataAuthorityInterceptor.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/interceptor/ZenDataAuthorityInterceptor.kt


+ 0 - 0
zen-web/src/main/kotlin/com/gxzc/zen/orm/data/authority/visitor/DataAuthoritySelectVisitor.kt → zen-api/src/main/kotlin/com/gxzc/zen/orm/data/authority/visitor/DataAuthoritySelectVisitor.kt


+ 1 - 0
zen-api/src/main/kotlin/com/gxzc/zen/umps/config/ShiroConfig.kt

@@ -89,6 +89,7 @@ class ShiroConfig {
                     "/test/**" to "canon", // 测试 免登录
                     "/upload/**" to "canon", // 上传免登录
                     "/api/**" to "canon", // api 免登陆
+                    "/ws/**" to "canon", // websocket 免登陆
                     ////////////////////// 静态资源 /////////////////////
                     "/v2/api-docs" to "canon",
                     "/swagger-resources/**" to "anon",

+ 2 - 1
zen-api/src/main/kotlin/com/gxzc/zen/umps/filter/UrlPermissionsFilter.kt

@@ -68,7 +68,8 @@ class UrlPermissionsFilter : PathMatchingFilter() {
         return perms.filter {
             if (it.uri != null) {
                 val regEx = "^${it.uri!!.replace("\\{\\*}".toRegex(), "[a-zA-Z\\\\d]+")}\$"
-                (Pattern.compile(regEx).matcher(requestUri).find() || requestUri.startsWith(it.uri + "/")) && method.equals(it.method, false)
+                //                (Pattern.compile(regEx).matcher(requestUri).find() || requestUri.startsWith(it.uri + "/")) && method.equals(it.method, false)
+                Pattern.compile(regEx).matcher(requestUri).find() && method.equals(it.method, false)
             } else {
                 false
             }

+ 4 - 4
zen-api/src/main/kotlin/com/gxzc/zen/umps/util/SSOUtil.kt

@@ -18,7 +18,7 @@ import org.apache.shiro.session.Session
  */
 @Suppress("UNCHECKED_CAST")
 object SSOUtil {
-    private var permissionSerivce: ISysPermissionService? = SpringContextHolder.getBean(ISysPermissionService::class.java)
+    private var permissionService: ISysPermissionService? = SpringContextHolder.getBean(ISysPermissionService::class.java)
 
     fun getSession(): Session? {
         return SecurityUtils.getSubject().getSession(false)
@@ -64,10 +64,10 @@ object SSOUtil {
      * 获取当前用户 权限列表
      */
     fun getCurUserPerms(): MutableList<ZenPermission>? {
-        if (permissionSerivce == null) {
-            permissionSerivce = SpringContextHolder.getBean(ISysPermissionService::class.java)
+        if (permissionService == null) {
+            permissionService = SpringContextHolder.getBean(ISysPermissionService::class.java)
         }
-        return permissionSerivce!!.getSubjectPermission()
+        return permissionService!!.getSubjectPermission()
     }
 
     /**

+ 0 - 0
zen-orm/src/main/resources/templates/entity.java.vm → zen-api/src/main/resources/templates/entity.java.vm


+ 0 - 0
zen-orm/src/main/resources/templates/entity.kt.vm → zen-api/src/main/resources/templates/entity.kt.vm


+ 0 - 0
zen-orm/src/main/resources/templates/mapper.java.vm → zen-api/src/main/resources/templates/mapper.java.vm


+ 0 - 0
zen-orm/src/main/resources/templates/service.java.vm → zen-api/src/main/resources/templates/service.java.vm


+ 0 - 0
zen-orm/src/main/resources/templates/serviceImpl.java.vm → zen-api/src/main/resources/templates/serviceImpl.java.vm


+ 3 - 1
zen-core/src/main/kotlin/com/gxzc/zen/common/config/response/ZenJacksonJsonFilter.kt

@@ -1,3 +1,5 @@
+@file:Suppress("DEPRECATION")
+
 package com.gxzc.zen.common.config.response
 
 import com.fasterxml.jackson.core.JsonGenerator
@@ -24,7 +26,7 @@ class ZenJacksonJsonFilter : FilterProvider() {
     private val includeMap = hashMapOf<KClass<out Any>, Set<String>>()
     private val filterMap = hashMapOf<KClass<out Any>, Set<String>>()
 
-    @Suppress("deprecation")
+    //    @Suppress("deprecation")
     override fun findFilter(filterId: Any?): BeanPropertyFilter {
         throw UnsupportedOperationException("Access to deprecated filters not supported")
     }

+ 25 - 1
zen-core/src/main/kotlin/com/gxzc/zen/common/util/FileUtil.kt

@@ -1,5 +1,6 @@
 package com.gxzc.zen.common.util
 
+import com.gxzc.zen.common.properties.UploadProperties
 import org.apache.commons.io.FilenameUtils
 import java.io.File
 import java.io.FileInputStream
@@ -13,11 +14,34 @@ import java.nio.ByteBuffer
  */
 object FileUtil {
 
+    private var uploadProperties = SpringContextHolder.getBean(UploadProperties::class.java)
+        get() {
+            if (field == null) {
+                field = SpringContextHolder.getBean(UploadProperties::class.java)
+            }
+            return field
+        }
+
     /**
      * 获取 文件首尾chunk+lastModifiedTime 拼接起来的md5
      */
-    fun md5HeadTail(path: String, chunkSize: Int): String {
+    fun md5HeadTail(path: String, chunkSize: Int?): String {
         val file = File(path)
+        return transientMd5HeadTail(file, chunkSize)
+    }
+
+    /**
+     * 获取 文件首尾chunk+lastModifiedTime 拼接起来的md5
+     */
+    fun md5HeadTail(file: File, chunkSize: Int?): String {
+        return transientMd5HeadTail(file, chunkSize)
+    }
+
+    private fun transientMd5HeadTail(file: File, size: Int?): String {
+        var chunkSize = size
+        if (chunkSize == null) {
+            chunkSize = uploadProperties!!.chunkSize!!.toInt()
+        }
         val fc = FileInputStream(file).channel
         val buffer = ByteBuffer.allocate(chunkSize)
         val fileSize = fc.size()

+ 0 - 17
zen-mq/build.gradle

@@ -1,17 +0,0 @@
-buildscript {
-    repositories {
-        mavenCentral()
-    }
-    dependencies {
-        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
-        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
-    }
-}
-
-dependencies {
-}
-
-bootRepackage {
-    enabled = false
-}

+ 0 - 18
zen-orm/build.gradle

@@ -1,18 +0,0 @@
-buildscript {
-    repositories {
-        mavenCentral()
-    }
-    dependencies {
-        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
-        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
-        classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
-    }
-}
-
-dependencies {
-    compile project(":zen-core")
-}
-
-bootRepackage {
-    enabled = false
-}

+ 0 - 27
zen-orm/src/test/kotlin/com/gxzc/zen/generator/TestPathFinder.kt

@@ -1,27 +0,0 @@
-package com.gxzc.zen.generator
-
-import org.junit.Test
-import java.io.File
-
-/**
- *
- * @author NorthLan
- * @date 2018/1/26
- * @url https://noahlan.com
- */
-class TestPathFinder {
-    @Test
-    fun pathFinder() {
-        val moduleName = "zen-orm"
-        val file = File(moduleName)
-        val path = file.absolutePath
-        System.out.println(path)
-    }
-
-    @Test
-    fun enumTest() {
-//        DSKey.values()
-//                .filter { it.pkgName in "com.gxzc.zen.api.bus.mapper" }
-//                .forEach { println(it) }
-    }
-}

+ 0 - 2
zen-web/build.gradle

@@ -14,9 +14,7 @@ buildscript {
 
 dependencies {
     compile project(":zen-core")
-    compile project(":zen-orm")
     compile project(":zen-api")
-    compile project(":zen-mq")
     compile project(":zen-job")
 }
 

+ 30 - 0
zen-web/src/main/kotlin/com/gxzc/zen/ws/sys/controller/WebSocketTestController.kt

@@ -0,0 +1,30 @@
+package com.gxzc.zen.ws.sys.controller
+
+import com.gxzc.zen.api.sys.model.SysUser
+import org.springframework.messaging.handler.annotation.DestinationVariable
+import org.springframework.messaging.handler.annotation.MessageMapping
+import org.springframework.messaging.handler.annotation.SendTo
+import org.springframework.stereotype.Controller
+import org.springframework.web.bind.annotation.ResponseBody
+
+/**
+ * 测试用 Controller
+ * @author NorthLan
+ * @date 2018/7/31
+ * @url https://noahlan.com
+ */
+@Controller
+class WebSocketTestController {
+    //    @Autowired
+    //    private lateinit var template: SimpMessagingTemplate
+
+    @MessageMapping("/test1/{id}")
+    @SendTo("/topic/test")
+    @ResponseBody
+    fun test(@DestinationVariable id: Int): SysUser {
+        return SysUser().apply {
+            this.id = id.toLong()
+            this.username = "2333"
+        }
+    }
+}

+ 5 - 1
zen-web/src/main/resources/application-mq.yml → zen-web/src/main/resources/application-msg.yml

@@ -15,6 +15,8 @@ spring:
         concurrency: 2 # 最小消息监听线程数
         max-concurrency: 2 # 最大消息监听线程数
     virtual-host: /
+ws:
+  enabled: true
 
 ---
 spring:
@@ -33,4 +35,6 @@ spring:
       simple:
         concurrency: 2 # 最小消息监听线程数
         max-concurrency: 2 # 最大消息监听线程数
-    virtual-host: /
+    virtual-host: /
+ws:
+  enabled: false

+ 2 - 2
zen-web/src/main/resources/application-orm.yml

@@ -8,8 +8,8 @@ mybatis-plus:
     refresh-mapper: true
     logic-delete-value: 0
     logic-not-delete-value: 1
-    sql-injector: com.gxzc.zen.orm.sql.ZenSqlInjector # 自定义injector
-    meta-object-handler: com.gxzc.zen.orm.CustomMetaObjectHandler
+    sql-injector: com.gxzc.zen.orm.config.ZenSqlInjector # 自定义injector
+    meta-object-handler: com.gxzc.zen.orm.config.CustomMetaObjectHandler
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: true #配置的缓存的全局开关

+ 6 - 6
zen-web/src/main/resources/application.yml

@@ -1,7 +1,7 @@
 spring:
   profiles:
     active: dev
-    include: orm,rpc,mq,cache,umps,platform,job,upload
+    include: orm,rpc,msg,cache,umps,platform,job,upload
 ---
 spring:
   profiles: dev
@@ -15,10 +15,10 @@ spring:
     deserialization:
       fail-on-unknown-properties: false # 多余字段反序列化过滤
   application:
-    name: Zen
+    name: Zen-Framework
 
 server:
-  port: 10001
+  port: 10000
 
 zen:
   swagger-open: true # swagger-ui是否开启
@@ -31,7 +31,7 @@ logging:
       zen.umps.config: warn
     com.xxl.job.core: warn
     com.atomikos: warn
-  path: logs/
+  path: ./logs/
   file: zen.log
 
 ---
@@ -47,7 +47,7 @@ spring:
     deserialization:
       fail-on-unknown-properties: false # 多余字段反序列化过滤
   application:
-    name: Zen
+    name: Zen-Framework
 
 server:
   port: 10000 # 生产环境端口号
@@ -63,5 +63,5 @@ logging:
       zen.umps.config: warn
     com.xxl.job.core: warn
     com.atomikos: warn
-  path: logs/
+  path: ./logs/
   file: zen.log