Quellcode durchsuchen

这个想法真是满意琢磨

tuonina vor 5 Jahren
Ursprung
Commit
4c45bcfd8b

+ 4 - 3
tuon-core/build.gradle

@@ -10,10 +10,11 @@ repositories {
 dependencies {
 dependencies {
     testCompile group: 'junit', name: 'junit', version: '4.12'
     testCompile group: 'junit', name: 'junit', version: '4.12'
     compile 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
     compile 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
-    compile('org.springframework.boot:spring-boot-starter-webflux')
-//    compile 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
+    compileOnly('org.springframework.boot:spring-boot-starter-webflux')
+    compileOnly('org.springframework.boot:spring-boot-starter-web')
+    compileOnly 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
     compile("com.baomidou:mybatis-plus-boot-starter:$mybatisPlusVersion")
     compile("com.baomidou:mybatis-plus-boot-starter:$mybatisPlusVersion")
     compile 'org.springframework.session:spring-session-data-redis'
     compile 'org.springframework.session:spring-session-data-redis'
-    compile ("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.9")
+    compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.9")
     compile('com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9')
     compile('com.fasterxml.jackson.module:jackson-module-kotlin:2.9.9')
 }
 }

+ 17 - 22
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/properties/CustomConfigProperties.java

@@ -11,36 +11,31 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 @ConfigurationProperties(prefix = "tuon.config")
 @ConfigurationProperties(prefix = "tuon.config")
 public class CustomConfigProperties {
 public class CustomConfigProperties {
 
 
-    private EnableConfig enable;
-
-    public void setEnable(EnableConfig enable) {
-        this.enable = enable;
+    /**
+     * 跨域设置的前缀
+     */
+    public static final String CROS_PREFIX="tuon.config.cros";
+    private CrosCfg cros;
+
+    public void setCros(CrosCfg cros) {
+        this.cros = cros;
     }
     }
 
 
-    public EnableConfig getEnable() {
-        return enable;
+    public CrosCfg getCros() {
+        return cros;
     }
     }
 
 
-    public static class EnableConfig{
-        private boolean web;
-
-        private boolean cros;
 
 
-        public void setCros(boolean cros) {
-            this.cros = cros;
-        }
+    public static class CrosCfg{
+        private boolean enable=true;
 
 
-        public boolean isCros() {
-            return cros;
+        public void setEnable(boolean enable) {
+            this.enable = enable;
         }
         }
 
 
-        public void setWeb(boolean web) {
-            this.web = web;
+        public boolean isEnable() {
+            return enable;
         }
         }
-
-        public boolean isWeb() {
-            return web;
-        }
-
     }
     }
+
 }
 }

+ 1 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/redis/RedisSentinelConfiguration.java

@@ -4,6 +4,7 @@ import org.springframework.context.annotation.Configuration;
 
 
 /**
 /**
  * redis配置,默认即可
  * redis配置,默认即可
+ * 所以根本就不需要自己去定义配置Redis,用默认的兼容性还更强大一
  */
  */
 @Configuration
 @Configuration
 public class RedisSentinelConfiguration {
 public class RedisSentinelConfiguration {

+ 37 - 30
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/session/SessionUtils.java

@@ -16,6 +16,8 @@ import reactor.core.publisher.Mono;
  */
  */
 public class SessionUtils {
 public class SessionUtils {
 
 
+    public static final String USER_ID="userId";
+    public static final String USER="user";
 
 
     private static ThreadLocal<IUser> mUserData = new ThreadLocal<>();
     private static ThreadLocal<IUser> mUserData = new ThreadLocal<>();
 
 
@@ -29,18 +31,47 @@ public class SessionUtils {
         mUserData.set(user);
         mUserData.set(user);
     }
     }
 
 
+
+    /**
+     * 清楚信息
+     */
+    public static void remove() {
+        mUserData.remove();
+    }
+
+    /**
+     * 获取用户信息
+     *
+     * @return 用户信息
+     */
+    public static IUser getUser() {
+        return mUserData.get();
+    }
+
+    /**
+     * 是否登录
+     *
+     * @return 由用户信息,则表示一登录
+     */
+    public static boolean isLogin() {
+        return mUserData.get() != null;
+    }
+
+
+
     /**
     /**
      * 异步获取用户信息,如果当前没有用户信息,则抛出异常
      * 异步获取用户信息,如果当前没有用户信息,则抛出异常
+     * 一下三个方法是为WebFlux 服务下实现的
      *
      *
      * @param exchange
      * @param exchange
      * @return
      * @return
      */
      */
-    public static <T extends IUser> Mono<IUser> getUser(ServerWebExchange exchange) {
+    public static <T extends IUser> Mono<T> getUser(ServerWebExchange exchange) {
         return exchange.getSession()
         return exchange.getSession()
                 .flatMap(webSession -> {
                 .flatMap(webSession -> {
-                    IUser info = webSession.getAttribute("user");
+                    IUser info = webSession.getAttribute(USER);
                     if (info != null) {
                     if (info != null) {
-                        return Mono.just(info);
+                        return Mono.just((T) info);
                     }
                     }
                     return Mono.error(new SessionNoUserException());
                     return Mono.error(new SessionNoUserException());
                 });
                 });
@@ -49,7 +80,7 @@ public class SessionUtils {
     public static Mono<String> getUserId(ServerWebExchange exchange) {
     public static Mono<String> getUserId(ServerWebExchange exchange) {
         return exchange.getSession()
         return exchange.getSession()
                 .flatMap(webSession -> {
                 .flatMap(webSession -> {
-                    String userId = webSession.getAttribute("userId");
+                    String userId = webSession.getAttribute(USER_ID);
                     if (StringUtils.isBlank(userId)) {
                     if (StringUtils.isBlank(userId)) {
                         return Mono.error(new SessionNoUserException());
                         return Mono.error(new SessionNoUserException());
                     }
                     }
@@ -61,36 +92,12 @@ public class SessionUtils {
     public static Mono<IUser> setUser(ServerWebExchange exchange, IUser user) {
     public static Mono<IUser> setUser(ServerWebExchange exchange, IUser user) {
         return exchange.getSession()
         return exchange.getSession()
                 .flatMap(webSession -> {
                 .flatMap(webSession -> {
-                    webSession.getAttributes().put("user", user);
-                    webSession.getAttributes().put("userId", user.getUserId());
+                    webSession.getAttributes().put(USER, user);
+                    webSession.getAttributes().put(USER_ID, user.getUserId());
                     return Mono.just(user);
                     return Mono.just(user);
                 });
                 });
     }
     }
 
 
-    /**
-     * 清楚信息
-     */
-    public static void remove() {
-        mUserData.remove();
-    }
-
-    /**
-     * 获取用户信息
-     *
-     * @return 用户信息
-     */
-    public static IUser getUser() {
-        return mUserData.get();
-    }
-
-    /**
-     * 是否登录
-     *
-     * @return 由用户信息,则表示一登录
-     */
-    public static boolean isLogin() {
-        return mUserData.get() != null;
-    }
 
 
 
 
 }
 }

+ 51 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/session/filter/SessionFillFilter.java

@@ -0,0 +1,51 @@
+package cn.tonyandmoney.tuon.core.session.filter;
+
+import cn.tonyandmoney.tuon.core.session.SessionUtils;
+import cn.tonyandmoney.tuon.core.user.IUser;
+import org.springframework.core.Ordered;
+import org.springframework.session.web.http.SessionRepositoryFilter;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
+
+/**
+ * session 注入和移除
+ */
+public class SessionFillFilter extends OncePerRequestFilter implements Ordered {
+
+    /**
+     * The default filter order.
+     * 一定要比 SessionRepositoryFilter 的顺序靠后,不然怎么可能获取到session呢
+     */
+    public static final int DEFAULT_ORDER = Integer.MIN_VALUE + 51;
+
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+            throws ServletException, IOException {
+
+        try {
+            HttpSession session = request.getSession(false);
+            if (session != null) {
+                Object user = session.getAttribute(SessionUtils.USER);
+                if (user != null) {
+                    SessionUtils.setUser((IUser) user);
+                }
+            }
+            filterChain.doFilter(request, response);
+        } finally {
+            SessionUtils.remove();
+        }
+
+    }
+
+    @Override
+    public int getOrder() {
+        return DEFAULT_ORDER;
+    }
+}

+ 44 - 10
tuon-core/src/main/kotlin/cn/tonyandmoney/tuon/core/config/CustomCoreConfiguration.kt

@@ -6,15 +6,17 @@ import cn.tonyandmoney.tuon.core.properties.CustomConfigProperties
 import com.fasterxml.jackson.databind.ObjectMapper
 import com.fasterxml.jackson.databind.ObjectMapper
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
 import com.fasterxml.jackson.module.kotlin.registerKotlinModule
 import com.fasterxml.jackson.module.kotlin.registerKotlinModule
-import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
+import org.springframework.beans.factory.annotation.Qualifier
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
 import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.context.annotation.Bean
 import org.springframework.context.annotation.Bean
 import org.springframework.context.annotation.Configuration
 import org.springframework.context.annotation.Configuration
 import org.springframework.context.annotation.Primary
 import org.springframework.context.annotation.Primary
 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
 import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
+import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
 import org.springframework.web.server.WebFilter
 import org.springframework.web.server.WebFilter
-import reactor.core.publisher.Flux
 import java.text.SimpleDateFormat
 import java.text.SimpleDateFormat
 
 
 
 
@@ -25,25 +27,31 @@ import java.text.SimpleDateFormat
 @EnableConfigurationProperties(value = [CustomConfigProperties::class])
 @EnableConfigurationProperties(value = [CustomConfigProperties::class])
 class CustomCoreConfiguration {
 class CustomCoreConfiguration {
 
 
+    companion object {
+        const val CONVERTER_NAME = "hookMappingJackson2HttpMessageConverter"
+    }
+
 
 
+    /**
+     * 跨域配置,Reactive
+     */
     @Bean
     @Bean
-    @ConditionalOnProperty(prefix = "custom.config.enable", name = ["cros"], havingValue = "true", matchIfMissing = true)
-    @ConditionalOnClass(Flux::class)
+    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
+    @ConditionalOnProperty(prefix = CustomConfigProperties.CROS_PREFIX, name = ["enable"], havingValue = "true", matchIfMissing = true)
     fun fluxCrosFilter(): WebFilter {
     fun fluxCrosFilter(): WebFilter {
         return GlobalCrosFilter()
         return GlobalCrosFilter()
     }
     }
 
 
+    /**
+     * servlet跨域配置
+     */
     @Bean
     @Bean
-    @ConditionalOnProperty(prefix = "custom.config.enable", name = ["cros"], havingValue = "true", matchIfMissing = true)
+    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
+    @ConditionalOnProperty(prefix = CustomConfigProperties.CROS_PREFIX, name = ["enable"], havingValue = "true", matchIfMissing = true)
     fun crosFilter(): GlobalWebCrosFilter {
     fun crosFilter(): GlobalWebCrosFilter {
         return GlobalWebCrosFilter()
         return GlobalWebCrosFilter()
     }
     }
 
 
-    @Bean("xmlObjectMapper")
-    fun xmlObjectMapper(): ObjectMapper {
-        return Jackson2ObjectMapperBuilder.xml().build()
-    }
-
 
 
     @Bean
     @Bean
     @Primary
     @Primary
@@ -68,5 +76,31 @@ class CustomCoreConfiguration {
         return mapper
         return mapper
     }
     }
 
 
+    /**
+     * XML解析和生成
+     */
+    @Bean("xmlObjectMapper")
+    fun xmlObjectMapper(): ObjectMapper {
+        return Jackson2ObjectMapperBuilder.xml().build()
+    }
+
+
+    @Bean(CONVERTER_NAME)
+    fun getConverter(objectMapper: ObjectMapper): MappingJackson2HttpMessageConverter {
+        return MappingJackson2HttpMessageConverter().apply {
+            this.objectMapper = objectMapper
+        }
+    }
+
+    /**
+     * 解析XML转换器,不知道能否成功
+     */
+    @Bean
+    fun xmlConverter(@Qualifier("xmlObjectMapper") objectMapper: ObjectMapper): MappingJackson2XmlHttpMessageConverter {
+        return MappingJackson2XmlHttpMessageConverter().apply {
+            this.objectMapper = objectMapper
+        }
+    }
+
 
 
 }
 }

+ 18 - 29
tuon-core/src/main/kotlin/cn/tonyandmoney/tuon/core/config/CustomWebMvcConfiguration.kt

@@ -1,32 +1,31 @@
 package cn.tonyandmoney.tuon.core.config
 package cn.tonyandmoney.tuon.core.config
 
 
 import cn.tonyandmoney.tuon.core.properties.CustomConfigProperties
 import cn.tonyandmoney.tuon.core.properties.CustomConfigProperties
-import com.fasterxml.jackson.databind.ObjectMapper
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
+import cn.tonyandmoney.tuon.core.session.filter.SessionFillFilter
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
 import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.context.annotation.Bean
 import org.springframework.context.annotation.Bean
 import org.springframework.context.annotation.Configuration
 import org.springframework.context.annotation.Configuration
-import org.springframework.http.codec.ServerCodecConfigurer
-import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
-import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
-import org.springframework.web.reactive.config.ResourceHandlerRegistry
-import org.springframework.web.reactive.config.WebFluxConfigurationSupport
+import org.springframework.core.annotation.Order
+import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
+import javax.servlet.Filter
 
 
 /**
 /**
  * Created by niantuo on 2018/9/20.
  * Created by niantuo on 2018/9/20.
  * mvc 配置
  * mvc 配置
- *
+ * 实际上这个配置无所谓有没有了
  */
  */
 @Configuration
 @Configuration
+@EnableRedisHttpSession
 @EnableConfigurationProperties(CustomConfigProperties::class)
 @EnableConfigurationProperties(CustomConfigProperties::class)
-@ConditionalOnProperty(prefix = "custom.config.enable", name = ["web"], havingValue = "true", matchIfMissing = true)
-class CustomWebMvcConfiguration : WebFluxConfigurationSupport() {
+@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
+class CustomWebMvcConfiguration : WebMvcConfigurationSupport() {
 
 
-    companion object {
-        const val CONVERTER_NAME = "hookMappingJackson2HttpMessageConverter"
-    }
 
 
     override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
     override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
+        super.addResourceHandlers(registry)
         registry.addResourceHandler("swagger-ui.html")
         registry.addResourceHandler("swagger-ui.html")
                 .addResourceLocations("classpath:/META-INF/resources/")
                 .addResourceLocations("classpath:/META-INF/resources/")
 
 
@@ -35,24 +34,14 @@ class CustomWebMvcConfiguration : WebFluxConfigurationSupport() {
                 .addResourceLocations("classpath:/META-INF/resources/webjars/")
                 .addResourceLocations("classpath:/META-INF/resources/webjars/")
     }
     }
 
 
-    override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
-        configurer.registerDefaults(true)
-    }
-
-
-    @Bean(CONVERTER_NAME)
-    fun getConverter(objectMapper: ObjectMapper): MappingJackson2HttpMessageConverter {
-        return MappingJackson2HttpMessageConverter().apply {
-            this.objectMapper = objectMapper
-        }
-    }
 
 
+    /**
+     * session注入和移除
+     */
     @Bean
     @Bean
-    fun xmlConverter(): MappingJackson2HttpMessageConverter {
-        return MappingJackson2HttpMessageConverter().apply {
-            this.objectMapper = Jackson2ObjectMapperBuilder.xml().build()
-        }
+    @Order(Order.)
+    fun sessionFilter(): Filter {
+        return SessionFillFilter()
     }
     }
 
 
-
 }
 }

+ 27 - 0
tuon-core/src/main/kotlin/cn/tonyandmoney/tuon/core/config/reactive/ReactiveCoreConfiguration.kt

@@ -0,0 +1,27 @@
+package cn.tonyandmoney.tuon.core.config.reactive
+
+import cn.tonyandmoney.tuon.core.properties.CustomConfigProperties
+import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
+import org.springframework.boot.context.properties.EnableConfigurationProperties
+import org.springframework.context.annotation.Configuration
+import org.springframework.web.reactive.config.ViewResolverRegistry
+import org.springframework.web.reactive.config.WebFluxConfigurationSupport
+
+/**
+ * 什么配置
+ */
+@Configuration
+@EnableConfigurationProperties(CustomConfigProperties::class)
+@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
+class ReactiveCoreConfiguration : WebFluxConfigurationSupport() {
+
+
+    /**
+     * 配置视图解析
+     */
+    override fun configureViewResolvers(registry: ViewResolverRegistry) {
+        super.configureViewResolvers(registry)
+    }
+
+
+}

+ 4 - 3
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/ISearchService.kt

@@ -5,7 +5,7 @@ import cn.tonyandmoney.tuon.qywx.search.SearchResult
 import reactor.core.publisher.Mono
 import reactor.core.publisher.Mono
 
 
 /**
 /**
- * 指定一个搜索的服务,能提供服务的service都实现该接口
+ * 指定一个搜索的服务,能提供搜索结果的service都实现该接口
  */
  */
 interface ISearchService {
 interface ISearchService {
 
 
@@ -14,9 +14,10 @@ interface ISearchService {
      * 判断是否响应该关键词,
      * 判断是否响应该关键词,
      * 如果能,则返回true
      * 如果能,则返回true
      */
      */
-    fun apply(keywords: String):Boolean
+    fun apply(keywords: String): Boolean
+
     /**
     /**
      * 进行搜索操作,返回数据
      * 进行搜索操作,返回数据
      */
      */
-    fun search(keywords:String): Mono<BaseResp<List<SearchResult>>>
+    fun search(keywords: String): Mono<BaseResp<List<SearchResult>>>
 }
 }

+ 4 - 0
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/impl/DutyServiceImpl.kt

@@ -58,6 +58,10 @@ class DutyServiceImpl : IDutyService {
         }
         }
     }
     }
 
 
+    /**
+     * 根据指定的日期查询当前的值班人员情况
+     * @param date 值班日期
+     */
     override fun queryByDate(date: Date): Mono<List<TDuty>> {
     override fun queryByDate(date: Date): Mono<List<TDuty>> {
         return Mono.create {
         return Mono.create {
             val wrapper = QueryWrapper<TDuty>().apply {
             val wrapper = QueryWrapper<TDuty>().apply {

+ 1 - 0
tuon-web/build.gradle

@@ -20,6 +20,7 @@ version '0.1.0'
 dependencies {
 dependencies {
    compile project(':tuon-core')
    compile project(':tuon-core')
    compile project(':tuon-qywx')
    compile project(':tuon-qywx')
+   implementation('org.springframework.boot:spring-boot-starter-webflux')
 }
 }