diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java index ee90112a..31ebbc97 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/user/service/impl/UserServiceImpl.java @@ -16,6 +16,8 @@ import lombok.val; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.HttpStatus; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; @@ -40,16 +42,17 @@ public class UserServiceImpl extends ServiceImpl implements UserService { private final BCryptPasswordEncoder bCryptPasswordEncoder; private final JwtService jwtService; + private final MessageSource messageSource; @Override @Cacheable public GetUserByLoginTokenResponse getUserByLoginToken(@NotBlank String loginToken) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.and(queryWrapper -> queryWrapper.eq(UserPersistence::getUsername, loginToken) - .or() - .eq(UserPersistence::getEmail, loginToken) - .or() - .eq(UserPersistence::getCellphone, loginToken)); + .or() + .eq(UserPersistence::getEmail, loginToken) + .or() + .eq(UserPersistence::getCellphone, loginToken)); val userPersistence = this.getBaseMapper().selectOne(wrapper); if (ObjectUtil.isNull(userPersistence)) { return null; @@ -87,6 +90,7 @@ public LoginResponse login(@Valid LoginPayload payload) throws SecurityException if (matched) { String jwt = jwtService.createJwt(payload.getRememberMe(), user.getId(), user.getUsername(), null, null); val response = new LoginResponse(); + response.setGreeting(messageSource.getMessage(("greeting"), null, LocaleContextHolder.getLocale())); response.setJwt(jwt); return response; } diff --git a/auth-center/src/main/resources/messages.properties b/auth-center/src/main/resources/messages.properties new file mode 100644 index 00000000..39490033 --- /dev/null +++ b/auth-center/src/main/resources/messages.properties @@ -0,0 +1 @@ +greeting=Hello! diff --git a/auth-center/src/main/resources/messages_zh_CN.properties b/auth-center/src/main/resources/messages_zh_CN.properties new file mode 100644 index 00000000..75f72bdd --- /dev/null +++ b/auth-center/src/main/resources/messages_zh_CN.properties @@ -0,0 +1 @@ +greeting=你好! diff --git a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/LoginResponse.java b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/LoginResponse.java index 2b359988..6e4b874a 100644 --- a/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/LoginResponse.java +++ b/common/src/main/java/com/jmsoftware/maf/common/domain/authcenter/user/LoginResponse.java @@ -9,5 +9,6 @@ **/ @Data public class LoginResponse { + private String greeting; private String jwt; } diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java index 7087c867..b0adaaec 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/MafAutoConfiguration.java @@ -48,6 +48,7 @@ @ConditionalOnWebApplication @EnableConfigurationProperties({MafConfiguration.class, MafProjectProperty.class}) @Import({ + WebMvcConfiguration.class, MyBatisPlusConfiguration.class, RedisConfiguration.class, Swagger2Configuration.class, @@ -104,13 +105,6 @@ public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafPro return new SpringBootStartupHelper(mafProjectProperty, ipHelper); } - @Bean - @ConditionalOnMissingBean - public WebMvcConfiguration webMvcConfiguration() { - log.warn("Initial bean: '{}'", WebMvcConfiguration.class.getSimpleName()); - return new WebMvcConfiguration(); - } - @Bean @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) public GlobalErrorController globalErrorController(ErrorAttributes errorAttributes, diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java index ec978a77..82767bf8 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/JwtConfiguration.java @@ -13,6 +13,8 @@ *

* Ignored request configuration. * + * TODO: make prefix under "maf." + * * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com * @date 5/2/20 11:41 PM **/ diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/WebMvcConfiguration.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/WebMvcConfiguration.java index 1ff00531..9cfe1ab3 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/WebMvcConfiguration.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/configuration/WebMvcConfiguration.java @@ -1,9 +1,11 @@ package com.jmsoftware.maf.springcloudstarter.configuration; -import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; /** *

WebMvcConfiguration

@@ -14,12 +16,15 @@ * @date 1/23/20 9:02 AM **/ @Configuration -@RequiredArgsConstructor public class WebMvcConfiguration implements WebMvcConfigurer { /** * Max age: 3600 seconds (1 hour) */ private static final long MAX_AGE_SECS = 3600; + /** + * Default name of the locale specification parameter: "lang". + */ + private static final String DEFAULT_PARAM_NAME = "lang"; /** * Configure cross origin requests processing. @@ -33,4 +38,25 @@ public void addCorsMappings(CorsRegistry registry) { .allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE") .maxAge(MAX_AGE_SECS); } + + /** + * An interceptor bean that will switch to a new locale based on the value of the lang parameter appended to a + * request. + * + * @return the locale change interceptor + * @see + * LocaleChangeInterceptor + */ + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); + localeChangeInterceptor.setParamName(DEFAULT_PARAM_NAME); + return localeChangeInterceptor; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // In order to take effect, this bean needs to be added to the application's interceptor registry. + registry.addInterceptor(localeChangeInterceptor()); + } }