diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/annotation/TwoFactor.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/annotation/TwoFactor.java index ae03f13ad..1d3d6b98f 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/annotation/TwoFactor.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/annotation/TwoFactor.java @@ -55,5 +55,5 @@ public @interface TwoFactor { * @return 错误提示 * @since 3.0.6 */ - String message() default "需要进行双因子验证"; + String message() default "assert.verify_code_error"; } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java index 240ed822e..e9f118475 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java @@ -1,9 +1,12 @@ package org.hswebframework.web.authorization.exception; import lombok.Getter; +import org.hswebframework.web.exception.I18nSupportException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; +import java.util.Set; + /** * 权限验证异常 * @@ -11,7 +14,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; * @since 3.0 */ @ResponseStatus(HttpStatus.FORBIDDEN) -public class AccessDenyException extends RuntimeException { +public class AccessDenyException extends I18nSupportException { private static final long serialVersionUID = -5135300127303801430L; @@ -19,16 +22,21 @@ public class AccessDenyException extends RuntimeException { private String code; public AccessDenyException() { - this("权限不足,拒绝访问!"); + this("error.access_denied"); } public AccessDenyException(String message) { super(message); } + public AccessDenyException(String permission, Set actions) { + super("error.permission_denied", permission, actions); + } + public AccessDenyException(String message, String code) { this(message, code, null); } + public AccessDenyException(String message, Throwable cause) { this(message, "access_denied", cause); } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java index dce94a7bf..767166d5e 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AuthenticationException.java @@ -1,9 +1,10 @@ package org.hswebframework.web.authorization.exception; import lombok.Getter; +import org.hswebframework.web.exception.I18nSupportException; @Getter -public class AuthenticationException extends RuntimeException { +public class AuthenticationException extends I18nSupportException { public static String ILLEGAL_PASSWORD = "illegal_password"; @@ -13,6 +14,10 @@ public class AuthenticationException extends RuntimeException { private final String code; + public AuthenticationException(String code) { + this(code, "error." + code); + } + public AuthenticationException(String code, String message) { super(message); this.code = code; diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java index 025d72c72..75dd34fd6 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java @@ -19,6 +19,7 @@ package org.hswebframework.web.authorization.exception; import org.hswebframework.web.authorization.token.TokenState; +import org.hswebframework.web.exception.I18nSupportException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @@ -29,7 +30,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; * @since 3.0 */ @ResponseStatus(HttpStatus.UNAUTHORIZED) -public class UnAuthorizedException extends RuntimeException { +public class UnAuthorizedException extends I18nSupportException { private static final long serialVersionUID = 2422918455013900645L; private final TokenState state; diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java index b7ba54b1c..05e61807f 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java @@ -253,7 +253,7 @@ public class DefaultUserTokenManager implements UserTokenManager { .flatMap(this::checkTimeout) .filterWhen(t -> { if (t.isNormal()) { - return Mono.error(new AccessDenyException("该用户已在其他地方登陆")); + return Mono.error(new AccessDenyException("error.logged_in_elsewhere")); } return Mono.empty(); }) diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java index a35973dbc..6c9ac63af 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java @@ -13,30 +13,30 @@ public enum TokenState implements EnumDict { /** * 正常,有效 */ - normal("normal","正常"), + normal("normal","message.token_state_normal"), /** * 已被禁止访问 */ - deny("deny", "已被禁止访问"), + deny("deny", "message.token_state_deny"), /** * 已过期 */ - expired("expired", "用户未登录"), + expired("expired", "message.token_state_expired"), /** * 已被踢下线 * @see AllopatricLoginMode#offlineOther */ - offline("offline", "用户已在其他地方登录"), + offline("offline", "message.token_state_offline"), /** * 锁定 */ - lock("lock", "登录状态已被锁定"); + lock("lock", "message.token_state_lock"); - private String value; + private final String value; - private String text; + private final String text; } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisUserTokenManager.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisUserTokenManager.java index 4002379a1..a3def4cb4 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisUserTokenManager.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/redis/RedisUserTokenManager.java @@ -244,7 +244,7 @@ public class RedisUserTokenManager implements UserTokenManager { return userIsLoggedIn(userId) .flatMap(r -> { if (r) { - return Mono.error(new AccessDenyException("已在其他地方登录", TokenState.deny.getValue(), null)); + return Mono.error(new AccessDenyException("error.logged_in_elsewhere", TokenState.deny.getValue(), null)); } return doSign; }); diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/resources/i18n/messages_zh_CN.properties b/hsweb-authorization/hsweb-authorization-api/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 000000000..412d3de60 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1,14 @@ +error.access_denied=权限不足,拒绝访问! +error.permission_denied=当前用户无权限[{0}]:{1} +error.logged_in_elsewhere=该用户已在其他地方登陆 +# +message.token_state_normal=正常 +message.token_state_deny=已被禁止访问 +message.token_state_expired=用户未登录 +message.token_state_offline=用户已在其他地方登录 +message.token_state_lock=登录状态已被锁定 +# +assert.need_two_factor_verify=需要双因子验证 +assert.username_must_not_be_empty=用户名不能为空 +assert.password_must_not_be_empty=密码不能为空 +assert.verify_code_error=验证码错误 \ No newline at end of file diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/define/DefaultBasicAuthorizeDefinition.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/define/DefaultBasicAuthorizeDefinition.java index ca42b8184..d5c556493 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/define/DefaultBasicAuthorizeDefinition.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/define/DefaultBasicAuthorizeDefinition.java @@ -36,7 +36,7 @@ public class DefaultBasicAuthorizeDefinition implements AopAuthorizeDefinition { private ResourcesDefinition resources = new ResourcesDefinition(); private DimensionsDefinition dimensions = new DimensionsDefinition(); - private String message = "权限不足,拒绝访问"; + private String message = "error.access_denied"; private Phased phased; diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/twofactor/TwoFactorHandlerInterceptorAdapter.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/twofactor/TwoFactorHandlerInterceptorAdapter.java index d1eea30c6..5aa9efbcb 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/twofactor/TwoFactorHandlerInterceptorAdapter.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/twofactor/TwoFactorHandlerInterceptorAdapter.java @@ -22,7 +22,7 @@ import javax.servlet.http.HttpServletResponse; @AllArgsConstructor public class TwoFactorHandlerInterceptorAdapter extends HandlerInterceptorAdapter { - private TwoFactorValidatorManager validatorManager; + private final TwoFactorValidatorManager validatorManager; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { @@ -45,9 +45,9 @@ public class TwoFactorHandlerInterceptorAdapter extends HandlerInterceptorAdapte code = request.getHeader(factor.parameter()); } if (StringUtils.isEmpty(code)) { - throw new NeedTwoFactorException(factor.message(), factor.provider()); + throw new NeedTwoFactorException("assert.need_two_factor_verify", factor.provider()); } else if (!validator.verify(code, factor.timeout())) { - throw new NeedTwoFactorException("验证码错误", factor.provider()); + throw new NeedTwoFactorException(factor.message(), factor.provider()); } } return super.preHandle(request, response, handler); diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java index 4347c46f0..659fe9879 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/web/AuthorizationController.java @@ -85,8 +85,8 @@ public class AuthorizationController { String username_ = (String) parameters.get("username"); String password_ = (String) parameters.get("password"); - Assert.hasLength(username_, "用户名不能为空"); - Assert.hasLength(password_, "密码不能为空"); + Assert.hasLength(username_, "assert.username_must_not_be_empty"); + Assert.hasLength(password_, "assert.password_must_not_be_empty"); Function parameterGetter = parameters::get; return Mono.defer(() -> { @@ -101,7 +101,7 @@ public class AuthorizationController { .publish(eventPublisher) .then(authenticationManager .authenticate(Mono.just(new PlainTextUsernamePasswordAuthenticationRequest(username, password))) - .switchIfEmpty(Mono.error(() -> new AuthenticationException(AuthenticationException.ILLEGAL_PASSWORD,"密码错误"))) + .switchIfEmpty(Mono.error(() -> new AuthenticationException(AuthenticationException.ILLEGAL_PASSWORD))) .flatMap(auth -> { //触发授权成功事件 AuthorizationSuccessEvent event = new AuthorizationSuccessEvent(auth, parameterGetter); diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java index 673442ebf..9f9b44b98 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonErrorControllerAdvice.java @@ -1,7 +1,6 @@ package org.hswebframework.web.crud.web; import io.r2dbc.spi.R2dbcDataIntegrityViolationException; -import io.r2dbc.spi.R2dbcNonTransientException; import lombok.extern.slf4j.Slf4j; import org.hswebframework.web.authorization.exception.AccessDenyException; import org.hswebframework.web.authorization.exception.AuthenticationException; @@ -10,11 +9,11 @@ import org.hswebframework.web.authorization.token.TokenState; import org.hswebframework.web.exception.BusinessException; import org.hswebframework.web.exception.NotFoundException; import org.hswebframework.web.exception.ValidationException; +import org.hswebframework.web.i18n.LocaleUtils; import org.hswebframework.web.logger.ReactiveLogger; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.core.Ordered; +import org.springframework.context.MessageSource; import org.springframework.core.annotation.Order; -import org.springframework.core.codec.DecodingException; import org.springframework.http.HttpStatus; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; @@ -40,11 +39,20 @@ import java.util.stream.Collectors; @Order public class CommonErrorControllerAdvice { + + private final MessageSource messageSource; + + public CommonErrorControllerAdvice(MessageSource messageSource) { + this.messageSource = messageSource; + } + @ExceptionHandler @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Mono> handleException(BusinessException e) { - return Mono.just(ResponseMessage.error(e.getCode(), e.getMessage())) - .doOnEach(ReactiveLogger.onNext(r -> log.error(e.getMessage(), e))); + return LocaleUtils + .resolveThrowable(messageSource, + e, + (err, msg) -> ResponseMessage.error(err.getStatus(), err.getCode(), msg)); } @ExceptionHandler diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebFluxConfiguration.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebFluxConfiguration.java index af48922dd..755d8a915 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebFluxConfiguration.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebFluxConfiguration.java @@ -1,14 +1,22 @@ package org.hswebframework.web.crud.web; +import org.hswebframework.web.i18n.WebFluxLocaleFilter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.i18n.LocaleContext; +import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.web.reactive.accept.RequestedContentTypeResolver; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; @Configuration @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @@ -16,17 +24,31 @@ public class CommonWebFluxConfiguration { @Bean @ConditionalOnMissingBean - public CommonErrorControllerAdvice commonErrorControllerAdvice(){ - return new CommonErrorControllerAdvice(); + public CommonErrorControllerAdvice commonErrorControllerAdvice(MessageSource messageSource) { + return new CommonErrorControllerAdvice(messageSource); } @Bean - @ConditionalOnProperty(prefix = "hsweb.webflux.response-wrapper",name = "enabled",havingValue = "true",matchIfMissing = true) + @ConditionalOnProperty(prefix = "hsweb.webflux.response-wrapper", name = "enabled", havingValue = "true", matchIfMissing = true) @ConfigurationProperties(prefix = "hsweb.webflux.response-wrapper") public ResponseMessageWrapper responseMessageWrapper(ServerCodecConfigurer codecConfigurer, RequestedContentTypeResolver resolver, - ReactiveAdapterRegistry registry){ - return new ResponseMessageWrapper(codecConfigurer.getWriters(),resolver,registry); + ReactiveAdapterRegistry registry) { + return new ResponseMessageWrapper(codecConfigurer.getWriters(), resolver, registry); } + + @Bean + public MessageSource messageSource() { + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setBasenames("i18n/messages"); + messageSource.setDefaultEncoding("UTF-8"); + return messageSource; + } + + @Bean + public WebFilter localeWebFilter() { + return new WebFluxLocaleFilter(); + } + } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java index 4dea9edb3..b46301e58 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/BusinessException.java @@ -26,7 +26,7 @@ import lombok.Getter; * @author zhouhao * @since 2.0 */ -public class BusinessException extends RuntimeException { +public class BusinessException extends I18nSupportException { private static final long serialVersionUID = 5441923856899380112L; @Getter @@ -39,20 +39,20 @@ public class BusinessException extends RuntimeException { this(message, 500); } - public BusinessException(String message, String code) { - this(message, code, 500); + public BusinessException(String message, int status, Object... args) { + this(message, null, status, args); } - public BusinessException(String message, String code, int status) { - super(message); + public BusinessException(String message, String code, Object... args) { + this(message, code, 500, args); + } + + public BusinessException(String message, String code, int status, Object... args) { + super(message, args); this.code = code; this.status = status; } - public BusinessException(String message, int status) { - super(message); - this.status = status; - } public BusinessException(String message, Throwable cause) { super(message, cause); diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java new file mode 100644 index 000000000..12a180ebe --- /dev/null +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/I18nSupportException.java @@ -0,0 +1,27 @@ +package org.hswebframework.web.exception; + + +import lombok.Getter; + +@Getter +public class I18nSupportException extends RuntimeException { + + private final Object[] args; + + public I18nSupportException(String code, Object... args) { + super(code); + this.args = args; + } + + public I18nSupportException(String code, Throwable cause, Object... args) { + super(code, cause); + this.args = args; + } + + + @Override + public String getLocalizedMessage() { + // TODO: 2021/6/21 + return super.getLocalizedMessage(); + } +} diff --git a/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java b/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java index 819481e79..b005303e4 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/exception/NotFoundException.java @@ -24,11 +24,11 @@ import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_FOUND) public class NotFoundException extends BusinessException { - public NotFoundException(String message) { - super(message, 404); + public NotFoundException(String message, Object... args) { + super(message, 404, args); } public NotFoundException() { - this("记录不存在"); + this("error.not_found"); } } diff --git a/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java b/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java new file mode 100644 index 000000000..4a6137f66 --- /dev/null +++ b/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java @@ -0,0 +1,67 @@ +package org.hswebframework.web.i18n; + +import org.hswebframework.web.exception.I18nSupportException; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContext; +import org.springframework.context.i18n.SimpleLocaleContext; +import reactor.core.publisher.Mono; + +import java.util.Locale; +import java.util.function.BiFunction; +import java.util.function.Function; + +public class LocaleUtils { + + public static final LocaleContext DEFAULT_CONTEXT = new SimpleLocaleContext(Locale.getDefault()); + + public static Mono reactive() { + return Mono + .subscriberContext() + .map(ctx -> ctx + .getOrEmpty(LocaleContext.class) + .orElse(DEFAULT_CONTEXT)); + } + + + public static Mono resolveThrowable(MessageSource messageSource, + S source, + BiFunction mapper) { + return doWithReactive(messageSource, source, Throwable::getMessage, mapper, source.getArgs()); + } + + public static Mono resolveThrowable(MessageSource messageSource, + S source, + BiFunction mapper, + Object... args) { + return doWithReactive(messageSource, source, Throwable::getMessage, mapper, args); + } + + public static Mono doWithReactive(MessageSource messageSource, + S source, + Function message, + BiFunction mapper, + Object... args) { + return reactive() + .map(ctx -> { + String msg = message.apply(source); + String newMsg = resolveMessage(messageSource, msg, ctx.getLocale(), msg, args); + return mapper.apply(source, newMsg); + }); + } + + public static Mono reactiveMessage(MessageSource messageSource, + String code, + Object... args) { + return reactive() + .map(ctx -> resolveMessage(messageSource, code, ctx.getLocale(), code, args)); + } + + public static String resolveMessage(MessageSource messageSource, + String code, + Locale locale, + String defaultMessage, + Object... args) { + return messageSource.getMessage(code, args, defaultMessage, locale); + } + +} diff --git a/hsweb-core/src/main/java/org/hswebframework/web/i18n/WebFluxLocaleFilter.java b/hsweb-core/src/main/java/org/hswebframework/web/i18n/WebFluxLocaleFilter.java new file mode 100644 index 000000000..8b4347a6a --- /dev/null +++ b/hsweb-core/src/main/java/org/hswebframework/web/i18n/WebFluxLocaleFilter.java @@ -0,0 +1,18 @@ +package org.hswebframework.web.i18n; + +import org.springframework.context.i18n.LocaleContext; +import org.springframework.lang.NonNull; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +public class WebFluxLocaleFilter implements WebFilter { + @Override + @NonNull + public Mono filter(@NonNull ServerWebExchange exchange, WebFilterChain chain) { + return chain + .filter(exchange) + .subscriberContext(ctx -> ctx.put(LocaleContext.class, exchange.getLocaleContext())); + } +} diff --git a/hsweb-core/src/main/resources/i18n/messages_zh_CN.properties b/hsweb-core/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 000000000..a60689754 --- /dev/null +++ b/hsweb-core/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1,2 @@ +error.not_found=数据不存在 +error.cant_create_instance=无法创建实例:{0} \ No newline at end of file diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/configuration/PermissionProperties.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/configuration/PermissionProperties.java index 7f37d667f..e4765108d 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/configuration/PermissionProperties.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/configuration/PermissionProperties.java @@ -75,7 +75,7 @@ public class PermissionProperties { .map(Permission::getActions) .orElseGet(Collections::emptySet)); - throw new AccessDenyException("当前用户无权限:" + setting.getPermission() + "" +actions); + throw new AccessDenyException(setting.getPermission(), actions); } }; diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultDimensionUserService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultDimensionUserService.java index 20b0263f0..f3c8d7e7c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultDimensionUserService.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/java/org/hswebframework/web/system/authorization/defaults/service/DefaultDimensionUserService.java @@ -59,7 +59,7 @@ public class DefaultDimensionUserService extends GenericReactiveCrudService new BusinessException("重复的绑定请求")); + .onErrorMap(DuplicateKeyException.class, (err) -> new BusinessException("error.duplicate_key")); } @Override diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/messages_zh_CN.properties b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 000000000..0fade07c1 --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-default/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1 @@ +error.duplicate_key=重复的请求 \ No newline at end of file