fix(系统设置): i18n

This commit is contained in:
shiziyuan9527 2022-10-18 16:45:01 +08:00 committed by lyh
parent dd4cce692a
commit 860db097a1
6 changed files with 28 additions and 23 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Locale;
@RestController @RestController
@RequestMapping("/ldap") @RequestMapping("/ldap")
@ -21,8 +22,8 @@ public class LdapController {
@PostMapping(value = "/signin") @PostMapping(value = "/signin")
@MsAuditLog(module = OperLogModule.SYSTEM_PARAMETER_SETTING, type = OperLogConstants.LOGIN, title = "LDAP") @MsAuditLog(module = OperLogModule.SYSTEM_PARAMETER_SETTING, type = OperLogConstants.LOGIN, title = "LDAP")
public Mono<ResultHolder> login(@RequestBody LoginRequest request, WebSession session) { public Mono<ResultHolder> login(@RequestBody LoginRequest request, WebSession session, Locale locale) {
return Mono.just(ldapService.login(request, session)) return Mono.just(ldapService.login(request, session, locale))
.map(ResultHolder::success); .map(ResultHolder::success);
} }

View File

@ -25,6 +25,7 @@ import reactor.core.scheduler.Schedulers;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@ -64,8 +65,8 @@ public class LoginController {
} }
@PostMapping(value = "/signin") @PostMapping(value = "/signin")
public Mono<ResultHolder> login(@RequestBody LoginRequest request, WebSession session) { public Mono<ResultHolder> login(@RequestBody LoginRequest request, WebSession session, Locale locale) {
return Mono.defer(() -> userLoginService.login(request, session).map(Mono::just).orElseGet(Mono::empty)) return Mono.defer(() -> userLoginService.login(request, session, locale).map(Mono::just).orElseGet(Mono::empty))
.subscribeOn(Schedulers.boundedElastic()) .subscribeOn(Schedulers.boundedElastic())
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "Not found user info or invalid password"))) .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.BAD_REQUEST, "Not found user info or invalid password")))
.doOnNext(user -> session.getAttributes().put("user", user)) .doOnNext(user -> session.getAttributes().put("user", user))

View File

@ -12,6 +12,7 @@ import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.WebSession; import org.springframework.web.server.WebSession;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Locale;
@Controller @Controller
@RequestMapping("sso") @RequestMapping("sso")
@ -21,24 +22,24 @@ public class SSOController {
@GetMapping("callback/{authId}") @GetMapping("callback/{authId}")
@MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录") @MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录")
public Rendering callbackWithAuthId(@RequestParam("code") String code, @PathVariable("authId") String authId, WebSession session) throws Exception { public Rendering callbackWithAuthId(@RequestParam("code") String code, @PathVariable("authId") String authId, WebSession session, Locale locale) throws Exception {
ssoService.exchangeToken(code, authId, session); ssoService.exchangeToken(code, authId, session, locale);
return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId())) return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId()))
.build(); .build();
} }
@GetMapping("callback") @GetMapping("callback")
@MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录") @MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录")
public Rendering callback(@RequestParam("code") String code, @RequestParam("state") String authId, WebSession session) throws Exception { public Rendering callback(@RequestParam("code") String code, @RequestParam("state") String authId, WebSession session, Locale locale) throws Exception {
ssoService.exchangeToken(code, authId, session); ssoService.exchangeToken(code, authId, session, locale);
return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId())) return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId()))
.build(); .build();
} }
@GetMapping("/callback/cas/{authId}") @GetMapping("/callback/cas/{authId}")
@MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录") @MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录")
public Rendering casCallback(@RequestParam("ticket") String ticket, @PathVariable("authId") String authId, WebSession session) throws Exception { public Rendering casCallback(@RequestParam("ticket") String ticket, @PathVariable("authId") String authId, WebSession session, Locale locale) throws Exception {
ssoService.serviceValidate(ticket, authId, session); ssoService.serviceValidate(ticket, authId, session, locale);
return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId())) return Rendering.redirectTo("/?_token=" + CodingUtil.base64Encoding(session.getId()))
.build(); .build();
} }

View File

@ -30,10 +30,7 @@ import org.springframework.web.server.WebSession;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.naming.directory.DirContext; import javax.naming.directory.DirContext;
import java.util.Hashtable; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.springframework.ldap.query.LdapQueryBuilder.query; import static org.springframework.ldap.query.LdapQueryBuilder.query;
@ -71,7 +68,7 @@ public class LdapService {
return authenticate(dn, credentials, ldapTemplate); return authenticate(dn, credentials, ldapTemplate);
} }
public Optional<SessionUser> login(LoginRequest request, WebSession session) { public Optional<SessionUser> login(LoginRequest request, WebSession session, Locale locale) {
String isOpen = service.getValue(ParamConstants.LDAP.OPEN.getValue()); String isOpen = service.getValue(ParamConstants.LDAP.OPEN.getValue());
if (StringUtils.isBlank(isOpen) || StringUtils.equals(Boolean.FALSE.toString(), isOpen)) { if (StringUtils.isBlank(isOpen) || StringUtils.equals(Boolean.FALSE.toString(), isOpen)) {
MSException.throwException(Translator.get("ldap_authentication_not_enabled")); MSException.throwException(Translator.get("ldap_authentication_not_enabled"));
@ -118,7 +115,7 @@ public class LdapService {
LoginRequest loginRequest = new LoginRequest(); LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(userId); loginRequest.setUsername(userId);
loginRequest.setAuthenticate(UserSource.LDAP.name()); loginRequest.setAuthenticate(UserSource.LDAP.name());
return userLoginService.login(loginRequest, session); return userLoginService.login(loginRequest, session, locale);
} }
private boolean authenticate(String dn, String credentials, LdapTemplate ldapTemplate) throws AuthenticationException { private boolean authenticate(String dn, String credentials, LdapTemplate ldapTemplate) throws AuthenticationException {

View File

@ -42,6 +42,7 @@ import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -59,7 +60,7 @@ public class SSOService {
@Resource @Resource
private UserLoginService userLoginService; private UserLoginService userLoginService;
public void exchangeToken(String code, String authId, WebSession session) throws Exception { public void exchangeToken(String code, String authId, WebSession session, Locale locale) throws Exception {
AuthSource authSource = authSourceService.getAuthSource(authId); AuthSource authSource = authSourceService.getAuthSource(authId);
Map config = JSON.parseObject(authSource.getConfiguration(), Map.class); Map config = JSON.parseObject(authSource.getConfiguration(), Map.class);
String tokenUrl = (String) config.get("tokenUrl"); String tokenUrl = (String) config.get("tokenUrl");
@ -90,7 +91,7 @@ public class SSOService {
MSException.throwException(content); MSException.throwException(content);
} }
doOICDLogin(authSource, accessToken, session); doOICDLogin(authSource, accessToken, session, locale);
} }
private RestTemplate getRestTemplateIgnoreSSL() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException { private RestTemplate getRestTemplateIgnoreSSL() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
@ -116,7 +117,7 @@ public class SSOService {
return new RestTemplate(requestFactory); return new RestTemplate(requestFactory);
} }
private void doOICDLogin(AuthSource authSource, String accessToken, WebSession session) throws Exception { private void doOICDLogin(AuthSource authSource, String accessToken, WebSession session, Locale locale) throws Exception {
Map config = JSON.parseObject(authSource.getConfiguration(), Map.class); Map config = JSON.parseObject(authSource.getConfiguration(), Map.class);
String userInfoUrl = (String) config.get("userInfoUrl"); String userInfoUrl = (String) config.get("userInfoUrl");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -151,7 +152,7 @@ public class SSOService {
MSException.throwException("email already exists!"); MSException.throwException("email already exists!");
} }
} }
Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session); Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authenticate", authSource.getType());
session.getAttributes().put("authId", authSource.getId()); session.getAttributes().put("authId", authSource.getId());
session.getAttributes().put("user", userOptional.get()); session.getAttributes().put("user", userOptional.get());
@ -178,7 +179,7 @@ public class SSOService {
/** /**
* cas callback * cas callback
*/ */
public void serviceValidate(String ticket, String authId, WebSession session) throws Exception { public void serviceValidate(String ticket, String authId, WebSession session, Locale locale) throws Exception {
AuthSource authSource = authSourceService.getAuthSource(authId); AuthSource authSource = authSourceService.getAuthSource(authId);
Map config = JSON.parseObject(authSource.getConfiguration(), Map.class); Map config = JSON.parseObject(authSource.getConfiguration(), Map.class);
String redirectUrl = ((String) config.get("redirectUrl")).replace("${authId}", authId); String redirectUrl = ((String) config.get("redirectUrl")).replace("${authId}", authId);
@ -208,7 +209,7 @@ public class SSOService {
MSException.throwException("email already exists!"); MSException.throwException("email already exists!");
} }
} }
Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session); Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authenticate", authSource.getType());
session.getAttributes().put("authId", authSource.getId()); session.getAttributes().put("authId", authSource.getId());
session.getAttributes().put("user", userOptional.get()); session.getAttributes().put("user", userOptional.get());

View File

@ -17,6 +17,7 @@ import io.metersphere.request.LoginRequest;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.server.WebSession; import org.springframework.web.server.WebSession;
@ -37,8 +38,11 @@ public class UserLoginService {
@Resource @Resource
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
public Optional<SessionUser> login(LoginRequest request, WebSession session) { public Optional<SessionUser> login(LoginRequest request, WebSession session, Locale locale) {
UserDTO userDTO; UserDTO userDTO;
if (locale != null) {
LocaleContextHolder.setLocale(locale, true);
}
switch (request.getAuthenticate()) { switch (request.getAuthenticate()) {
case "OIDC": case "OIDC":
case "CAS": case "CAS":