use of com.itrus.portal.exception.TerminalServiceException in project portal by ixinportal.
the class RenewUserCertWebController method updateCert.
@RequestMapping(value = "/updateCert", method = RequestMethod.POST)
// TODO 更新证书
@ResponseBody
public Map<String, Object> updateCert(// 证书序列号
@RequestParam(value = "certSn", required = true) String certSn, // 旧的证书的请求,目前随便写:45564
@RequestParam(value = "csr", required = true) String csr, // 证书base64
@RequestParam(value = "crt", required = true) String crt, // 原证书所在容器生成的新的证书请求
@RequestParam(value = "newCSR", required = true) String newCSR, // 老证书对csr签名后的base64字符串
@RequestParam(value = "pkcs7", required = true) String pkcs7, // 订单id
@RequestParam(value = "billid", required = true) Long billId, // 证书所属的keysn,没有key则为""
@RequestParam(value = "keySn", required = false) String keySn, @RequestParam(value = "oldCertDn", required = false) String oldCertDn, Model uimModel, HttpSession session) {
// TODO,参数有效性验证
Map<String, Object> ret = new HashMap<String, Object>();
ret.put("status", 1);
UserInfo webuserInfo = (UserInfo) session.getAttribute("webuserInfo");
Enterprise webenterprise = (Enterprise) session.getAttribute("webenterprise");
if (null == webuserInfo || null == webenterprise) {
// 2标识登录已经失效,需跳转到登录页面
ret.put("retCode", 2);
ret.put("retMsg", "登录状态失效");
return ret;
}
Bill bill = billService.getBill(billId);
if (null == bill) {
ret.put("retMsg", "该订单不存在");
return ret;
}
// 订单是否为当前用户当前企业
if (!webuserInfo.getId().equals(bill.getUniqueId()) || !webenterprise.getId().equals(bill.getEnterprise())) {
ret.put("retMsg", "您不能操作该订单");
return ret;
}
// 判断是否是更新过了的订单,是的话直接从本地数据库获取证书的base64传递给页面
if ((bill.getBillStatus() == ComNames.BILL_STATUS_8 || bill.getBillStatus() == ComNames.BILL_STATUS_7 || bill.getBillStatus() == ComNames.BILL_STATUS_6) && null != bill.getOldUserCert()) {
// 根据订单号,找到订单更新后的证书信息
CertBuf certBuf = sqlSession.selectOne("com.itrus.portal.db.CertBufMapper.selectNewCertBufByBillId", billId);
// 证书更新信息发给浏览器,写入证书
// 用户证书,Base64编码 (公钥证书),因为记录的时候,存的都是公钥证书,所以这个证书链也是个单证书而已
ret.put("certChain", certBuf.getCertBuf());
// 用户证书,Base64编码 (公钥证书)
ret.put("certBase64", certBuf.getCertBuf());
// 用户证书,Base64编码 (公钥证书)
ret.put("certSignBuf", certBuf.getCertsignBuf());
// KMC协议响应1
ret.put("certKmcRep1", certBuf.getCertKmcRep1());
// KMC协议响应2
ret.put("certKmcRep2", certBuf.getCertKmcRep2());
// //加密证书
// 用户加密证书
ret.put("encUserCert", certBuf.getEncUserCert());
ret.put("status", 0);
List<UserCert> userCert2 = userCertService.getUserCertByBill(bill.getId(), null);
ret.put("certId", userCert2.get(0).getId());
return ret;
}
String accountHash = "";
// 根据订单ID从本地查找ra哈希值
accountHash = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectRaHashByBillId", billId);
if (!StringUtils.isNotBlank(accountHash)) {
// 根据证书序列号查找RA哈希值
try {
accountHash = getRaInfo.getRaInfoByCertSerialNumber(certSn);
} catch (UserInfoServiceException e) {
ret.put("message", e.getMessage());
return ret;
}
}
// 查找raAccount
RaAccountExample rae = new RaAccountExample();
RaAccountExample.Criteria criteria = rae.or();
criteria.andAccountHashEqualTo(accountHash);
rae.setLimit(1);
RaAccount raAccount = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByExample", rae);
CertUtlis certUtlis = new CertUtlis();
CertInfo certInfo = new CertInfo();
UserCert oldUsercert = null;
try {
if (!bill.getBillStatus().equals(ComNames.BILL_STATUS_12)) {
ret.put("message", "该订单下的证书未处于可更新状态");
ret.put("status", 1007);
return ret;
}
// 解密ra密码
if (null != raAccount.getAaPassword()) {
raAccount.setAaPassword(AESencrp.decrypt(raAccount.getAaPassword(), dbEncKey));
}
// 判断证书是否过期并且处于90天内的有效期
oldUsercert = userCertService.getUserCertByCertSn(certSn);
Date certEndTime = oldUsercert.getCertEndTime();
logger.error("************过期时间********certEndTime" + certEndTime);
int result = compareDate(certEndTime, new Date());
logger.error("************过期状态********result" + result);
// 用户证书剩余的天数
Integer surplusDay = 0;
surplusDay = differenceDate(certEndTime, new Date());
Integer certValidity = null;
// 判断订单中是否配了产品规格且有期限
if (null != bill.getProductSpec() && 0 != bill.getProductSpec()) {
ProductSpec productSpec = productSpecService.getProductSpec(bill.getProductSpec());
Integer day = Integer.parseInt(productSpec.getProductValid());
certValidity = 365 * day + 1 + (day / 4);
}
logger.error("产品规格期限************certValidity" + certValidity);
if (result == 2) {
logger.error("************证书过期更新********result=" + result);
// 证书已经过期了,直接进行新证书申请
if (null != certValidity) {
logger.error("****给走申请证书接口的用户补充上剩余天数****certValidity=" + certValidity);
// 给走申请证书接口的用户补充上剩余天数,因为用户指定的更新期限,可能跟原证书的年限不一致
certValidity = certValidity + surplusDay;
certInfo = renewUserService.applyCert(newCSR, productService.getProductById(bill.getProduct()), oldCertDn, certValidity);
} else {
logger.error("****证书更新方法****certSn=" + certSn);
certInfo = certUtlis.renewAA(certSn, crt, raAccount.getAaPassword(), accountHash, raAccount.getAaPassword(), "", newCSR, pkcs7, raAccount);
}
// certInfo = renewUserService.applyCert(newCSR,
// productService.getProductById(bill.getProduct()),
// oldCertDn, certValidity);
} else if (result == 3) {
// 当证书过期,且超过90天
ret.put("status", 1008);
ret.put("message", "该证书已经过期超过了90天,无法更新");
return ret;
} else if (result == 1) {
logger.error("****证书更新方法2****result=" + result);
if (null != certValidity) {
// 给走申请证书接口的用户补充上剩余天数,因为用户指定的更新期限,可能跟原证书的年限不一致
certValidity = certValidity + surplusDay;
certInfo = renewUserService.applyCert(newCSR, productService.getProductById(bill.getProduct()), oldCertDn, certValidity);
} else {
// 判断是否为passcord模式
CaPasscode passcode = new CaPasscode();
if (raAccount.getCertSignType() == 2) {
// 获取对应ra账号的passcode
RaAccountInfoExample raInfoExample = new RaAccountInfoExample();
RaAccountInfoExample.Criteria raInfoCriteria = raInfoExample.createCriteria();
raInfoCriteria.andHashValEqualTo(raAccount.getAccountHash());
raInfoExample.setOrderByClause("create_time desc");
raInfoExample.setLimit(1);
RaAccountInfo raAccountInfo = sqlSession.selectOne("com.itrus.portal.db.RaAccountInfoMapper.selectByExample", raInfoExample);
if (raAccountInfo == null) {
LogUtil.syslog(sqlSession, "制作证书", "不存在指定RA帐号");
}
// 获取对应passcode
try {
passcode = codeService.IssuedCode4Cert(raAccountInfo);
if (passcode == null) {
LogUtil.syslog(sqlSession, "制作证书", "没有有效授权码,请联系管理员");
}
} catch (TerminalServiceException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 进行证书更新,直接在原证书的基础上,签发一张新证书,需要有
certInfo = certUtlis.renewAA(certSn, crt, raAccount.getAaPassword(), accountHash, raAccount.getAaPassword(), passcode.getPasscode(), newCSR, pkcs7, raAccount);
} else {
// 进行证书更新,直接在原证书的基础上,签发一张新证书,需要有
certInfo = certUtlis.renewAA(certSn, crt, raAccount.getAaPassword(), accountHash, raAccount.getAaPassword(), "", newCSR, pkcs7, raAccount);
}
}
}
UserCert userCert = updateUserCertAndCertBuf(certSn, certInfo, billId, raAccount.getId());
UserLog userLog = new UserLog();
userLog.setType("更新证书成功");
userLog.setInfo("成功更新证书:" + certSn);
userLog.setHostId("未知");
userLog.setProject(bill.getProject());
LogUtil.userlog(sqlSession, userLog);
// 证书更新信息发给浏览器,写入证书
// 用户证书PKCS7格式(含证书链)
ret.put("certChain", certInfo.getCertSignBufP7());
// 用户证书,Base64编码 (公钥证书)
ret.put("certBase64", certInfo.getCertSignBuf());
ret.put("certId", userCert.getId());
// 用户证书,Base64编码 (公钥证书)
ret.put("certSignBuf", certInfo.getCertSignBuf());
// KMC协议响应1
ret.put("certKmcRep1", certInfo.getCertKmcRep1());
// KMC协议响应2
ret.put("certKmcRep2", certInfo.getCertKmcRep2());
// 加密证书
// 用户加密证书
ret.put("encUserCert", certInfo.getCertSignBufKmc());
ret.put("status", 0);
// 更新usercert和cerbuf
return ret;
} catch (MalformedURLException | RaServiceUnavailable_Exception e) {
logger.error("更新证书:" + certSn + "出现异常,异常信息:" + e.getMessage());
UserLog userLog = new UserLog();
userLog.setType("更新证书失败");
userLog.setInfo("url:updateCert,出错的证书:" + certSn + ",详细错误:" + e.getMessage());
userLog.setHostId("未知");
userLog.setProject(bill.getProject());
LogUtil.userlog(sqlSession, userLog);
ret.put("status", 1004);
ret.put("message", "出现未知异常,请联系管理员处理");
return ret;
} catch (EncDecException e) {
// TODO Auto-generated catch block
logger.error("更新证书:" + certSn + "出现异常,异常信息:" + e.getMessage());
UserLog userLog = new UserLog();
userLog.setType("更新证书失败");
userLog.setInfo("url:updateCert,出错的证书:" + certSn + ",详细错误:" + e.getMessage());
userLog.setHostId("未知");
userLog.setProject(bill.getProject());
LogUtil.userlog(sqlSession, userLog);
ret.put("status", 1005);
ret.put("message", "出现未知异常,请联系管理员处理");
return ret;
} catch (Exception e) {
if (StringUtils.isNotBlank(certInfo.getCertSerialNumber())) {
// 新证书已签发,但未保存到数据库时 需打印新证书序列号
logger.error("旧证书序列号:" + oldUsercert.getCertSn() + ",异常的新证书序列号:" + certInfo.getCertSerialNumber());
}
logger.error("更新证书:" + certSn + "出现异常,异常信息:" + e.getMessage());
UserLog userLog = new UserLog();
userLog.setType("更新证书失败");
userLog.setInfo("url:updateCert,出错的证书:" + certSn + ",详细错误:" + e.getMessage());
userLog.setHostId("未知");
userLog.setProject(bill.getProject());
LogUtil.userlog(sqlSession, userLog);
ret.put("status", 1006);
ret.put("message", "出现未知异常,请联系管理员处理");
return ret;
}
}
use of com.itrus.portal.exception.TerminalServiceException in project portal by ixinportal.
the class UserInfoWebController method downloadEnclosure.
/**
* 下载附加文件
* @param productId
* @param request
* @param response
* @return
*/
@RequestMapping("/downloadEnclosure")
@ResponseBody
public synchronized Map<String, Object> downloadEnclosure(@RequestParam(value = "productId", required = true) Long productId, HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> retMap = new HashMap<String, Object>();
// 0标识失败,1标识成功
retMap.put("retCode", 0);
HttpSession session = request.getSession();
Boolean verifyCodeStatus = (Boolean) session.getAttribute("webverifyCodeStatus");
UserInfo webuserInfo = (UserInfo) session.getAttribute("webuserInfo");
if (null == verifyCodeStatus || !verifyCodeStatus || null == webuserInfo) {
// 登录状态失效,跳转到注册页面
// 2表示登录失效
retMap.put("retCode", 2);
retMap.put("retMsg", "登录已经失效,请重新登录");
return retMap;
}
try {
Product product = sqlSession.selectOne("com.itrus.portal.db.ProductMapper.selectByPrimaryKey", productId);
if (product != null && product.getIsEnclosure() != null && product.getIsEnclosure() == 1) {
String url = null;
try {
url = systemConfigService.getEnclosureUrl();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String fileName = product.getEnclosureName();
fileName = encodeFilename(fileName, request);
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes("utf-8"), "iso-8859-1"));
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String base64 = Base64Util.getBase64FromFile(url + "/" + product.getEnclosureName());
File file1 = new File(url + "/" + fileName);
if (base64 == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] bytes = decoder.decodeBuffer(base64);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
// 调整异常数据
bytes[i] += 256;
}
}
// 生成文件的路径
OutputStream out = /*new FileOutputStream(url+"/123.jpg");// 生成文件的路径*/
response.getOutputStream();
out.write(bytes);
out.flush();
out.close();
return null;
} else {
throw new TerminalServiceException("未上传附加");
}
// retMap.put("retCode", 1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TerminalServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
use of com.itrus.portal.exception.TerminalServiceException in project portal by ixinportal.
the class CaPasscodeService method IssuedCode4Cert.
/**
* 获得有效passcode,并将其设置为已使用
*
* @param accountInfo
* @param cert
* @return
* @throws TerminalServiceException
*/
public CaPasscode IssuedCode4Cert(RaAccountInfo accountInfo) throws /*, UserCert cert*/
TerminalServiceException {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// 防止一个code赋给两个用户
def.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
TransactionStatus status = transactionManager.getTransaction(def);
CaPasscode code = null;
try {
CaPasscodeExample passcodeExample = new CaPasscodeExample();
CaPasscodeExample.Criteria codeCriteria = passcodeExample.createCriteria();
codeCriteria.andRaAccountInfoEqualTo(accountInfo.getId());
codeCriteria.andStatusEqualTo(CODE_STATUS_VALID);
codeCriteria.andUseTimeIsNull();
codeCriteria.andEndTimeGreaterThan(new Date());
passcodeExample.setOrderByClause("create_time desc");
passcodeExample.setLimit(1);
code = sqlSession.selectOne("com.itrus.portal.db.CaPasscodeMapper.selectByExample", passcodeExample);
// 设置为已使用
if (code != null) {
code.setStatus(CODE_STATUS_USED);
code.setUseTime(new Date());
// code.setCertId(cert.getId());
sqlSession.update("com.itrus.portal.db.CaPasscodeMapper.updateByPrimaryKeySelective", code);
} else {
LogUtil.adminlog(sqlSession, "获取授权码", "RA账号ID:" + accountInfo.getId() + ",O:" + accountInfo.getOrganization() + ",OU:" + accountInfo.getOrgUnit() + ",没有有效passcode");
}
if (!status.isCompleted())
transactionManager.commit(status);
} catch (Exception e) {
if (!status.isCompleted())
transactionManager.rollback(status);
logger.error("", e);
throw new TerminalServiceException("发生未知错误,请稍后重试");
}
return code;
}
use of com.itrus.portal.exception.TerminalServiceException in project portal by ixinportal.
the class CertUtlis method enrollCertByWS.
public CertInfo enrollCertByWS(String csr, RaAccount raAccount, UserInfo userInfo, Integer certValidity) throws MalformedURLException, RaServiceUnavailable_Exception, TerminalServiceException {
String json = "{\"certValidity\":" + certValidity + "}";
CertInfo certInfo = null;
UserAPIService service = new UserAPIService(new URL(raAccount.getServiceUrl()));
UserAPIServicePortType client = service.getUserAPIServicePort();
// 用户信息
try {
logger.error("***判断是什么模式***" + raAccount.getCertSignType());
// 判断是什么模式
if (raAccount.getCertSignType() == null || (raAccount.getCertSignType() != null && raAccount.getCertSignType() == 1)) {
// AA模式
logger.error("*****userInfo=" + userInfo + "***csr***=" + csr + "***raAccount.getAccountHash()**=" + raAccount.getAccountHash() + "***raAccount.getAaPassword()**=" + raAccount.getAaPassword() + "**json**=" + json);
certInfo = client.enrollCertAA(userInfo, csr, raAccount.getAccountHash(), raAccount.getAaPassword(), "", json);
} else {
// passcord模式
logger.error("输出hash*****raAccount.getAccountHash()========" + raAccount.getAccountHash());
// 判断是否为passcord模式
CaPasscode passcode = new CaPasscode();
// 获取对应ra账号的passcode
RaAccountInfoExample raInfoExample = new RaAccountInfoExample();
RaAccountInfoExample.Criteria raInfoCriteria = raInfoExample.createCriteria();
raInfoCriteria.andHashValEqualTo(raAccount.getAccountHash());
raInfoExample.setOrderByClause("create_time desc");
raInfoExample.setLimit(1);
RaAccountInfo raAccountInfo = raAccountInfoService.getRaAccountInfo(raInfoExample);
// 获取对应passcode
try {
passcode = codeService.IssuedCode4Cert(raAccountInfo);
if (passcode == null) {
logger.error("******passcode为空***********");
throw new TerminalServiceException("passcode为空");
}
} catch (TerminalServiceException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw e1;
}
logger.error("******passcode=" + passcode.getPasscode());
certInfo = client.enrollCertAA(userInfo, csr, raAccount.getAccountHash(), raAccount.getAaPassword(), passcode.getPasscode(), json);
}
} catch (RaServiceUnavailable_Exception e) {
logger.error("userInfo:" + ToStringBuilder.reflectionToString(userInfo));
logger.error("csr:" + csr);
logger.error("raAccount:" + ToStringBuilder.reflectionToString(raAccount));
logger.error("json:" + json);
throw e;
}
return certInfo;
}
Aggregations