Merge pull request #5451 from dataease/pr@dev@perf_cas_admin

perf(cas): cas切换管理员步骤繁琐
This commit is contained in:
fit2cloud-chenyw 2023-06-14 22:29:47 +08:00 committed by GitHub
commit 2ab1e625c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,19 +6,19 @@ import io.dataease.auth.entity.TokenInfo;
import io.dataease.auth.service.AuthUserService; import io.dataease.auth.service.AuthUserService;
import io.dataease.auth.util.JWTUtils; import io.dataease.auth.util.JWTUtils;
import io.dataease.commons.constants.SysLogConstants; import io.dataease.commons.constants.SysLogConstants;
import io.dataease.commons.utils.CodingUtil;
import io.dataease.commons.utils.DeLogUtils; import io.dataease.commons.utils.DeLogUtils;
import io.dataease.commons.utils.LogUtil; import io.dataease.commons.utils.LogUtil;
import io.dataease.commons.utils.ServletUtils; import io.dataease.commons.utils.ServletUtils;
import io.dataease.controller.ResultHolder; import io.dataease.controller.ResultHolder;
import io.dataease.i18n.Translator; import io.dataease.i18n.Translator;
import io.dataease.service.sys.SysUserService; import io.dataease.service.sys.SysUserService;
import io.dataease.service.system.SystemParameterService; import io.dataease.service.system.SystemParameterService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.client.authentication.AttributePrincipal; import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AssertionHolder; import org.jasig.cas.client.util.AssertionHolder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -33,12 +33,16 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.List;
@ApiIgnore @ApiIgnore
@RequestMapping("/cas") @RequestMapping("/cas")
@Controller @Controller
public class CasServer { public class CasServer {
@Value("#{'${dataease.cas-admin-users:admin}'.split(',')}")
private List<String> adminUserList;
@Autowired @Autowired
private AuthUserService authUserService; private AuthUserService authUserService;
@ -55,8 +59,13 @@ public class CasServer {
AttributePrincipal principal = AssertionHolder.getAssertion().getPrincipal(); AttributePrincipal principal = AssertionHolder.getAssertion().getPrincipal();
String name = principal.getName(); String name = principal.getName();
SysUserEntity sysUserEntity = null;
try { try {
SysUserEntity sysUserEntity = authUserService.getCasUserByName(name); if (CollectionUtils.isNotEmpty(adminUserList) && adminUserList.contains(name)) {
sysUserEntity = authUserService.getUserById(1L);
} else {
sysUserEntity = authUserService.getCasUserByName(name);
}
if (null == sysUserEntity) { if (null == sysUserEntity) {
String s = RandomUtil.randomString(6); String s = RandomUtil.randomString(6);
String email = s + "@xxx.com"; String email = s + "@xxx.com";
@ -68,7 +77,8 @@ public class CasServer {
TokenInfo tokenInfo = TokenInfo.builder().userId(sysUserEntity.getUserId()).username(sysUserEntity.getUsername()).build(); TokenInfo tokenInfo = TokenInfo.builder().userId(sysUserEntity.getUserId()).username(sysUserEntity.getUsername()).build();
String token = JWTUtils.sign(tokenInfo, realPwd); String token = JWTUtils.sign(tokenInfo, realPwd);
ServletUtils.setToken(token); ServletUtils.setToken(token);
Cookie cookie_token = new Cookie("Authorization", token);cookie_token.setPath("/"); Cookie cookie_token = new Cookie("Authorization", token);
cookie_token.setPath("/");
response.addCookie(cookie_token); response.addCookie(cookie_token);
DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, sysUserEntity.getUserId(), null, null, null); DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, sysUserEntity.getUserId(), null, null, null);