From 1b465578b6476f0fb230675f9252f10da26946a8 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Sat, 24 May 2025 18:41:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20LocaleUtils=20=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=8C=96=E8=AF=AD=E8=A8=80=E5=92=8C=E6=97=B6=E5=8C=BA?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=AE=A1=E7=90=86=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/utils/LocaleUtils.java | 93 +++++++++++++++++++ .../config/web/IpAddrFilterConfig.java | 6 +- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/com/jeesite/common/utils/LocaleUtils.java diff --git a/common/src/main/java/com/jeesite/common/utils/LocaleUtils.java b/common/src/main/java/com/jeesite/common/utils/LocaleUtils.java new file mode 100644 index 00000000..397330fa --- /dev/null +++ b/common/src/main/java/com/jeesite/common/utils/LocaleUtils.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. + */ +package com.jeesite.common.utils; + +import com.jeesite.common.io.PropertiesUtils; +import com.jeesite.common.web.http.ServletUtils; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.context.i18n.TimeZoneAwareLocaleContext; +import org.springframework.core.NamedThreadLocal; +import org.springframework.web.servlet.LocaleContextResolver; + +import java.util.Locale; +import java.util.TimeZone; + +/** + * 本地化工具 + * @author ThinkGem + * @version 2025-05-21 + */ +public class LocaleUtils { + + private static final Boolean LANG_ENABLED = PropertiesUtils.getInstance().getPropertyToBoolean("lang.enabled", "false"); + private static final ThreadLocal timeZoneAwareLocaleContext = new NamedThreadLocal<>("TimeZoneAwareLocaleContext"); + private static LocaleContextResolver localeResolver; + + /** + * 获取当前 Locale 对象,获取顺序:请求 -> 会话 -> Cookie -> lang.defaultLocale + */ + public static Locale getLocale() { + return getTimeZoneAwareLocaleContext().getLocale(); + } + + /** + * 获取当前 TimeZone 对象,获取顺序:请求 -> 会话 -> Cookie -> lang.defaultTimeZone + */ + public static TimeZone getTimeZone() { + return getTimeZoneAwareLocaleContext().getTimeZone(); + } + + /** + * 获取 TimeZoneAwareLocaleContext + */ + public static TimeZoneAwareLocaleContext getTimeZoneAwareLocaleContext() { + TimeZoneAwareLocaleContext context = timeZoneAwareLocaleContext.get(); + if (context != null){ + return context; + } + if (LANG_ENABLED && localeResolver != null){ + HttpServletRequest request = ServletUtils.getRequest(); + if (request != null){ + context = (TimeZoneAwareLocaleContext)localeResolver.resolveLocaleContext(request); + } + } + if (context == null){ + context = new TimeZoneAwareLocaleContext() { + @Override + public Locale getLocale() { + return Locale.getDefault(); + } + @Override + public TimeZone getTimeZone() { + return TimeZone.getDefault(); + } + }; + } + setTimeZoneAwareLocaleContext(context); + return context; + } + + /** + * 设置 TimeZoneAwareLocaleContext + */ + public static void setTimeZoneAwareLocaleContext(TimeZoneAwareLocaleContext context) { + timeZoneAwareLocaleContext.set(context); + } + + /** + * 清理本地线程对象(请求结束时调用) + */ + public static void removeTimeZoneAwareLocaleContext() { + timeZoneAwareLocaleContext.remove(); + } + + /** + * 设置 LocaleContextResolver + */ + public static void setLocaleResolver(LocaleContextResolver localeResolver) { + LocaleUtils.localeResolver = localeResolver; + } + +} diff --git a/modules/core/src/main/java/com/jeesite/modules/config/web/IpAddrFilterConfig.java b/modules/core/src/main/java/com/jeesite/modules/config/web/IpAddrFilterConfig.java index 3f10aa3e..e3fe80a7 100644 --- a/modules/core/src/main/java/com/jeesite/modules/config/web/IpAddrFilterConfig.java +++ b/modules/core/src/main/java/com/jeesite/modules/config/web/IpAddrFilterConfig.java @@ -6,6 +6,7 @@ package com.jeesite.modules.config.web; import com.jeesite.common.config.Global; import com.jeesite.common.lang.StringUtils; +import com.jeesite.common.utils.LocaleUtils; import com.jeesite.common.web.http.ServletUtils; import jakarta.servlet.Filter; import jakarta.servlet.ServletRequest; @@ -15,6 +16,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.web.servlet.LocaleContextResolver; /** * IP地址黑白名单过滤器配置 @@ -27,7 +29,7 @@ public class IpAddrFilterConfig { private static String[] denyPrefixes; @Bean - public FilterRegistrationBean ipAddrFilter() { + public FilterRegistrationBean ipAddrFilter(LocaleContextResolver localeResolver) { FilterRegistrationBean bean = new FilterRegistrationBean<>(); bean.setName("ipAddrFilter"); bean.setOrder(Ordered.HIGHEST_PRECEDENCE + 10); @@ -39,7 +41,9 @@ public class IpAddrFilterConfig { response.setStatus(403); ServletUtils.renderString(response, Global.getText("访问拒绝")); } + LocaleUtils.removeTimeZoneAwareLocaleContext(); }); + LocaleUtils.setLocaleResolver(localeResolver); bean.addUrlPatterns("/*"); return bean; }