zhanghaijun лет назад: 2
Родитель
Сommit
f46bc8c94b

+ 4 - 0
pom.xml

@@ -154,6 +154,10 @@
                                 <groupId>top.haijunit</groupId>
                                 <artifactId>train-common-mybatis</artifactId>
                             </dependency>
+                            <dependency>
+                                <groupId>top.haijunit</groupId>
+                                <artifactId>train-common-web</artifactId>
+                            </dependency>
                             <dependency>
                                 <groupId>top.haijunit</groupId>
                                 <artifactId>train-common-websocket</artifactId>

+ 1 - 0
train-common/pom.xml

@@ -14,6 +14,7 @@
         <module>train-common-bom</module>
         <module>train-common-core</module>
         <module>train-common-mybatis</module>
+        <module>train-common-web</module>
         <module>train-common-websocket</module>
     </modules>
 </project>

+ 35 - 0
train-common/train-common-web/pom.xml

@@ -0,0 +1,35 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>top.haijunit</groupId>
+        <artifactId>train-common</artifactId>
+        <version>${revision}</version>
+    </parent>
+
+    <artifactId>train-common-web</artifactId>
+    <packaging>jar</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>top.haijunit</groupId>
+            <artifactId>train-common-core</artifactId>
+        </dependency>
+        <!-- SpringBoot Web容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                    <groupId>org.springframework.boot</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <!-- web 容器使用 undertow 性能更强 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-undertow</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 22 - 0
train-common/train-common-web/src/main/java/top/haijunit/web/config/I18nConfig.java

@@ -0,0 +1,22 @@
+package top.haijunit.web.config;
+
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.servlet.LocaleResolver;
+import top.haijunit.web.core.I18nLocaleResolver;
+
+/**
+ * @author zhanghaijun
+ * @date 2023/11/21 17:35
+ * @description 国际化配置
+ */
+@AutoConfiguration(before = WebMvcAutoConfiguration.class)
+public class I18nConfig {
+
+    @Bean
+    public LocaleResolver localeResolver() {
+        return new I18nLocaleResolver();
+    }
+
+}

+ 37 - 0
train-common/train-common-web/src/main/java/top/haijunit/web/config/ResourcesConfig.java

@@ -0,0 +1,37 @@
+package top.haijunit.web.config;
+
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * @author zhanghaijun
+ * @date 2023/11/21 17:38
+ * @description 通用配置
+ */
+@AutoConfiguration
+public class ResourcesConfig implements WebMvcConfigurer {
+
+    /** 跨域配置 */
+    @Bean
+    public CorsFilter corsFilter() {
+        CorsConfiguration config = new CorsConfiguration();
+        config.setAllowCredentials(true);
+        // 设置访问源地址
+        config.addAllowedOriginPattern("*");
+        // 设置访问源请求头
+        config.addAllowedHeader("*");
+        // 设置访问源请求方法
+        config.addAllowedMethod("*");
+        // 有效期 1800秒
+        config.setMaxAge(1800L);
+        // 添加映射路径,拦截一切请求
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", config);
+        // 返回新的CorsFilter
+        return new CorsFilter(source);
+    }
+}

+ 28 - 0
train-common/train-common-web/src/main/java/top/haijunit/web/config/UndertowConfig.java

@@ -0,0 +1,28 @@
+package top.haijunit.web.config;
+
+import io.undertow.server.DefaultByteBufferPool;
+import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
+import org.springframework.boot.web.server.WebServerFactoryCustomizer;
+
+/**
+ * @author zhanghaijun
+ * @date 2023/11/21 17:31
+ * @description Undertow 自定义配置
+ */
+@AutoConfiguration
+public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
+
+    /** 设置 Undertow 的 websocket 缓冲池 */
+    @Override
+    public void customize(UndertowServletWebServerFactory factory) {
+        // 默认不直接分配内存 如果项目中使用了 websocket 建议直接分配
+        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
+            WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
+            webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 512));
+            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
+        });
+    }
+
+}

+ 30 - 0
train-common/train-common-web/src/main/java/top/haijunit/web/core/I18nLocaleResolver.java

@@ -0,0 +1,30 @@
+package top.haijunit.web.core;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.web.servlet.LocaleResolver;
+
+import java.util.Locale;
+
+/**
+ * @author zhanghaijun
+ * @date 2023/11/21 17:35
+ * @description 获取请求头国际化信息
+ */
+public class I18nLocaleResolver implements LocaleResolver {
+    @Override
+    public Locale resolveLocale(HttpServletRequest request) {
+        String language = request.getHeader("content-language");
+        Locale locale = Locale.getDefault();
+        if (language != null && language.length() > 0) {
+            String[] split = language.split("_");
+            locale = new Locale(split[0], split[1]);
+        }
+        return locale;
+    }
+
+    @Override
+    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
+
+    }
+}

+ 3 - 0
train-common/train-common-web/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -0,0 +1,3 @@
+top.haijunit.web.config.I18nConfig
+top.haijunit.web.config.ResourcesConfig
+top.haijunit.web.config.UndertowConfig