优化这段代码if (ResMeta.PROTOCOL_HTTPS.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_HTTP.equals(token.getLoginProtocol())) { ssoTicket.setHostDomainName(token.getTargetIp()); ssoTicket.setWebappUrl(token.getLoginProtocol() + "://" + token.getTargetIp()); ssoTicket.setAgreementType(token.getLoginProtocol()); if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) { String err = "加载应用发布错误"; rv.setDetail(err); rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR); logger.error(err); return null; } } else if (ResMeta.RESTYPE_CSAPP.equals(token.getResType())){ if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) { String err = "加载应用发布错误"; rv.setDetail(err); rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR); logger.error(err); return null; } } else if (ResMeta.PROTOCOL_RDP.equals(token.getLoginProtocol())) { if (ssoAcc != null && ssoAcc.getWinAccType() == AccMeta.WIN_ACC_DOMAIN_TYPE) { this.fillHostDomain(ssoAcc, ssoTicket, token.getAccName()); } else if (resInfo.getHostName() != null && resInfo.getHostName().length() > 0) { ssoTicket.setAcc(resInfo.getHostName() + "\\" + ssoTicket.getAcc()); } } else if (ResMeta.PROTOCOL_SSH2.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_SSH1.equals(token.getLoginProtocol()) || ResMeta.PROTOCOL_TELNET.equals(token.getLoginProtocol())) { CMDSSOTicket cmdTicket = (CMDSSOTicket) ssoTicket; /* 设置提权(原5.0的扩展策略)如果存在工单号 按工单处理 规则授权暂时不支持设置提权 */ if (token.getWorkId() != null && token.getWorkId().length() > 0) { try { SSOWorkOrderAuthz authz = new SSOWorkOrderAuthz(); authz.setWorkOrderId(token.getWorkId()); authz.setResId(token.getResId()); int suPermission = ssoWorkOrderRepository.findWorkOrderAuthcSuPermission(authz); if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) { cmdTicket.setSu_cmd(resInfo.getSuCmd()); cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc())); cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt()); } } catch (Exception e) { } } else if (token.getAuthzId() != null && token.getAuthzId().length() > 0) { try { int suPermission = ssoResroleRepository.findResroleAuthcSuPermission(token.getAuthzId()); if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) { cmdTicket.setSu_cmd(resInfo.getSuCmd()); cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc())); cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt()); } } catch (Exception e) { } }
时间: 2024-04-09 22:33:36 浏览: 225
这段代码可以进行如下优化:
1. 将重复的代码块提取出来,避免代码重复:
```java
if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) {
String err = "加载应用发布错误";
rv.setDetail(err);
rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR);
logger.error(err);
return null;
}
```
可以将其提取为一个方法,减少代码冗余。
2. 使用 switch-case 替代多个 if-else if 语句:
```java
switch (token.getLoginProtocol()) {
case ResMeta.PROTOCOL_HTTPS:
case ResMeta.PROTOCOL_HTTP:
ssoTicket.setHostDomainName(token.getTargetIp());
ssoTicket.setWebappUrl(token.getLoginProtocol() + "://" + token.getTargetIp());
ssoTicket.setAgreementType(token.getLoginProtocol());
break;
case ResMeta.RESTYPE_CSAPP:
if (!this.fillIssuerInfo(ssoTicket, token.getIssuerId(), token.getIssuerToolId(), token.getResType(), sid)) {
String err = "加载应用发布错误";
rv.setDetail(err);
rv.setStatus(DataResult.STA_SSO_REGISTER_TICKET_ERR);
logger.error(err);
return null;
}
break;
case ResMeta.PROTOCOL_RDP:
if (ssoAcc != null && ssoAcc.getWinAccType() == AccMeta.WIN_ACC_DOMAIN_TYPE) {
this.fillHostDomain(ssoAcc, ssoTicket, token.getAccName());
} else if (resInfo.getHostName() != null && resInfo.getHostName().length() > 0) {
ssoTicket.setAcc(resInfo.getHostName() + "\\" + ssoTicket.getAcc());
}
break;
case ResMeta.PROTOCOL_SSH2:
case ResMeta.PROTOCOL_SSH1:
case ResMeta.PROTOCOL_TELNET:
CMDSSOTicket cmdTicket = (CMDSSOTicket) ssoTicket;
if (token.getWorkId() != null && token.getWorkId().length() > 0) {
try {
SSOWorkOrderAuthz authz = new SSOWorkOrderAuthz();
authz.setWorkOrderId(token.getWorkId());
authz.setResId(token.getResId());
int suPermission = ssoWorkOrderRepository.findWorkOrderAuthcSuPermission(authz);
if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) {
cmdTicket.setSu_cmd(resInfo.getSuCmd());
cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc()));
cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt());
}
} catch (Exception e) {
}
} else if (token.getAuthzId() != null && token.getAuthzId().length() > 0) {
try {
int suPermission = ssoResroleRepository.findResroleAuthcSuPermission(token.getAuthzId());
if (SSORegisterMeta.SU_PERMISSION_YES_VALUE == suPermission && ResMeta.SU_PRIVILEGE_YES_VALUE == resInfo.getSuPrivilege()) {
cmdTicket.setSu_cmd(resInfo.getSuCmd());
cmdTicket.setSu_pwd(this.getPasswd(resInfo.getId(), resInfo.getSuManagerAcc()));
cmdTicket.setSu_pwd_prompt(resInfo.getSuPwdPrompt());
}
} catch (Exception e) {
}
}
break;
}
```
使用 switch-case 可以使代码更加简洁和易读。
这些优化可以提高代码的可维护性和可读性。
阅读全文