Search in sources :

Example 11 with DigitalCert

use of com.itrus.portal.db.DigitalCert in project portal by ixinportal.

the class MakeCertController method downloadPfxCert.

/**
 * 后台管理员下载pfx证书的接口
 *
 * @param id,订单id
 * @param request
 * @param response
 * @return
 */
@RequestMapping("/pfx/{id}")
public String downloadPfxCert(@PathVariable(value = "id") Long id, HttpServletRequest request, HttpServletResponse response) {
    Bill bill = billService.getBill(id);
    if (null == bill) {
        return null;
    }
    Integer uidIdx = 1;
    UserInfo webuserInfo = userInfoService.getUserInfoByBillId(id);
    Enterprise webenterprise = enterpriseService.getEnterpriseByBillId(id);
    boolean billFlag = webuserInfo.getId().equals(bill.getUniqueId()) && webenterprise.getId().equals(bill.getEnterprise());
    if (!billFlag) {
        return null;
    }
    // 用户已经下载过了,再次下载
    boolean downLoadFlag = bill.getBillStatus().equals(ComNames.BILL_STATUS_6) || bill.getBillStatus().equals(ComNames.BILL_STATUS_7) || bill.getBillStatus().equals(ComNames.BILL_STATUS_8);
    if (downLoadFlag) {
        // 根据订单号,找到订单对应的证书信息
        CertBuf certBuf = sqlSession.selectOne("com.itrus.portal.db.CertBufMapper.selectPfxCertByBillId", bill.getId());
        Date date = new Date();
        // 获取证书第一次下载时间和当前时间比较,如果超过了十五天,则不允许下载
        int day = DateUtils.daysOfTwo(date, certBuf.getCreateTime());
        if (day > 16) {
            return null;
        }
        // 从数据库中取出数据,返回给客户端.
        // 重置response对象中的缓冲区,该方法可以不写,但是你要保证response缓冲区没有其他数据,否则导出可能会出现问题,建议加上
        response.reset();
        String filename = webenterprise.getEnterpriseName() + "功能证书.pfx";
        filename = encodeFilename(filename, request);
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        response.setCharacterEncoding("utf-8");
        // 由于导出格式是pfx的文件,设置导出文件的响应头部信息
        response.setContentType("application/x-pkcs12");
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            // 清理刷新缓冲区,将缓存中的数据将数据导出excel
            byte[] byteCert = Base64.decode(certBuf.getPfxCert());
            os.write(byteCert);
            os.flush();
            // 关闭os
            if (os != null) {
                os.close();
            }
            certBuf.setLastDownloadTime(new Date());
            certBuf.setCertDownloadNumber(certBuf.getCertDownloadNumber() + 1);
            downLoadCertService.updatePfxCert(certBuf);
            // 记录日志
            LogUtil.adminlog(sqlSession, "下载pfx证书", "企业名称:" + webenterprise.getEnterpriseName());
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LogUtil.adminlog(sqlSession, "下载pfx证书", "下载失败,错误信息:" + e.getMessage());
        }
    } else {
        // 用户未下载过,第一次下载
        if (bill.getBillStatus().equals(ComNames.BILL_STATUS_13)) {
            // 查询项目产品
            Product product = productService.getProduct(bill.getProduct());
            // 企业
            Enterprise enterprise = enterpriseService.getEnterpriseById(bill.getEnterprise());
            // 获取产品、RA配置
            RaAccount ra = raAccountService.getRaAccount(product.getRa());
            // 证书配置
            DigitalCert digitalcert = digitalCertService.getDigitalCert(product.getCert());
            // 下载证书
            String autoidType = "";
            Integer autoidValue = 0;
            String pfxCert = "";
            // 用户ID,用来最终匹配公钥证书和密钥对,一个用户id,只能使用一次,所以考虑使用订单号来作为用户id,避免一个用户只能下载一个证书.
            String userid = bill.getBillId() + (Math.random() * 1000 + 9000);
            // TODO
            String certPass = product.getPassword();
            // 20170410pfx私钥证书保护密码:需要根据产品配置的密码或获取
            // 产生CSR证书请求
            String certReqBuf = "";
            // 算法
            String algorithm = digitalCertService.getAlgorithm(digitalcert);
            // 下载证书
            CertInfo racertinfo = null;
            try {
                certReqBuf = GenUtil.GenP10(userid, "", algorithm);
                racertinfo = downLoadCertService.downLoadCert(product, ra, bill, digitalcert, uidIdx, certReqBuf, autoidType, autoidValue);
                pfxCert = GenUtil.GenPFX(userid, certPass, racertinfo.getCertSignBuf(), false, enterprise.getEnterpriseName());
                // 保存证书
                downLoadCertService.savePfxCertInfo(racertinfo, bill, ra.getId(), uidIdx, "", autoidType, autoidValue, pfxCert);
                // 从数据库中取出数据,返回给客户端.
                // 重置response对象中的缓冲区,该方法可以不写,但是你要保证response缓冲区没有其他数据,否则导出可能会出现问题,建议加上
                response.reset();
                String filename = webenterprise.getEnterpriseName() + "通讯证书.pfx";
                filename = encodeFilename(filename, request);
                response.setHeader("Content-disposition", "attachment;filename=" + filename);
                response.setCharacterEncoding("utf-8");
                // 由于导出格式是pfx的文件,设置导出文件的响应头部信息
                response.setContentType("application/x-pkcs12");
                OutputStream os = null;
                os = response.getOutputStream();
                // 清理刷新缓冲区,将缓存中的数据将数据导出excel
                byte[] byteCert = Base64.decode(pfxCert);
                os.write(byteCert);
                os.flush();
                // 关闭os
                if (os != null) {
                    os.close();
                }
                // 记录日志
                LogUtil.adminlog(sqlSession, "下载pfx证书", "下载成功,企业名称:" + webenterprise.getEnterpriseName());
                return null;
            } catch (Exception e) {
                // TODO: handle exception
                LogUtil.adminlog(sqlSession, "下载pfx证书", "下载失败,错误信息:" + e.getMessage());
            }
        }
    }
    return null;
}
Also used : CertInfo(cn.topca.tca.ra.service.CertInfo) OutputStream(java.io.OutputStream) Product(com.itrus.portal.db.Product) UserInfo(com.itrus.portal.db.UserInfo) IOException(java.io.IOException) Date(java.util.Date) EncDecException(com.itrus.portal.exception.EncDecException) IOException(java.io.IOException) RaServiceUnavailable_Exception(cn.topca.tca.ra.service.RaServiceUnavailable_Exception) BigInteger(java.math.BigInteger) DigitalCert(com.itrus.portal.db.DigitalCert) RaAccount(com.itrus.portal.db.RaAccount) Bill(com.itrus.portal.db.Bill) Enterprise(com.itrus.portal.db.Enterprise) CertBuf(com.itrus.portal.db.CertBuf) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with DigitalCert

use of com.itrus.portal.db.DigitalCert in project portal by ixinportal.

the class MakeCertController method update.

// 显示制证详情
@RequestMapping(value = "/update/{id}", produces = "text/html")
public String update(@PathVariable("id") Long id, Model uiModel) throws EncDecException, Exception {
    List<Map> makecerts = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectBillByMakecert", id);
    uiModel.addAttribute("makecerts", makecerts);
    uiModel.addAttribute("enterpriseSn", makecerts.get(0).get("enterprise_sn"));
    DigitalCert digitalcert = null;
    ProductSpec productSpec = null;
    Product product = null;
    Map<String, Object> params = new HashMap<String, Object>();
    // 添加组合产品
    if (makecerts.get(0).get("is_combined") != null && makecerts.get(0).get("is_combined").equals(1)) {
        try {
            uiModel.addAttribute("billStr", jsonTool.writeValueAsString(makecerts.get(0)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 得到三条组合产品的产品信息
        for (int i = 1; i < 4; i++) {
            if (makecerts.get(0).get("product" + i) == null) {
                uiModel.addAttribute("usercertallStr" + i, "{}");
                continue;
            }
            product = sqlSession.selectOne("com.itrus.portal.db.ProductMapper.selectByPrimaryKey", makecerts.get(0).get("product" + i));
            uiModel.addAttribute("product" + i, product);
            digitalcert = sqlSession.selectOne("com.itrus.portal.db.DigitalCertMapper.selectByPrimaryKey", product.getCert());
            uiModel.addAttribute("digitalcert" + i, digitalcert);
            params.put("id", id);
            params.put("pid", product.getId());
            List<Map<String, Object>> makecertexall = sqlSession.selectList("com.itrus.portal.db.UserCertMapper.selectByPrimaryBillAndProductKey", params);
            uiModel.addAttribute("makecertexall" + i, makecertexall);
            // 获取产品规格
            if (makecerts.get(0).containsKey("product_spec" + i) && !"0".equals(makecerts.get(0).get("product_spec" + i))) {
                productSpec = productSpecService.getProductSpec((Long) makecerts.get(0).get("product_spec" + i));
            }
            uiModel.addAttribute("productSpec" + i, productSpec);
            try {
                uiModel.addAttribute("digitalcertStr" + i, jsonTool.writeValueAsString(digitalcert));
                uiModel.addAttribute("productStr" + i, jsonTool.writeValueAsString(product));
                uiModel.addAttribute("productSpecStr" + i, jsonTool.writeValueAsString(productSpec));
                uiModel.addAttribute("usercertallStr" + i, jsonTool.writeValueAsString(makecertexall));
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 解析项目产品中,certinfo配置信息
            JSONArray certinfo = JSONArray.parseArray(product.getCertinfo());
            for (int j = 0; certinfo != null && j < certinfo.size(); j++) {
                JSONObject obj = certinfo.getJSONObject(j);
                String autoid = obj.getString("autoid");
                if (autoid == null)
                    continue;
                String autoidType = obj.getString("autoidType");
                String autoidPrev = obj.getString("autoidPrev");
                String autoidPrevDate = obj.getString("autoidPrevDate");
                String autoidLength = obj.getString("autoidLength");
                // 从user_cert表查询,该autoidType的最大值,如果没有最大值,则设置为0
                Map param = new HashMap();
                String enterpriseId = makecerts.get(0).get("enterprise").toString();
                param.put("enterpriseId", makecerts.get(0).get("enterprise"));
                param.put("type", autoidType);
                Integer autoidValue = null;
                if (autoidPrevDate == null)
                    autoidValue = sqlSession.selectOne("com.itrus.portal.db.UserCertMapper.selectByConditon", param);
                else
                    autoidValue = sqlSession.selectOne("com.itrus.portal.db.UserCertMapper.selectByConditonDate", param);
                if (autoidValue == null)
                    autoidValue = 0;
                uiModel.addAttribute("enterpriseId" + i, enterpriseId);
                uiModel.addAttribute("autoidType" + i, autoidType);
                uiModel.addAttribute("autoidPrev" + i, autoidPrev);
                uiModel.addAttribute("autoidLength" + i, autoidLength);
                uiModel.addAttribute("autoidValue" + i, autoidValue);
                break;
            }
            // 签章服务配置
            List<MakeSealConfig> makeSealConfigs = sqlSession.selectList("com.itrus.portal.db.MakeSealConfigMapper.selectByExample");
            if (!makeSealConfigs.isEmpty()) {
                MakeSealConfig makeSealConfig = makeSealConfigs.get(0);
                makeSealConfig.setAddressKey(AESencrp.decrypt(makeSealConfig.getAddressKey(), dbEncKey));
                uiModel.addAttribute("makeSealConfig", makeSealConfig);
            }
            MakeSealServer makeSealServer = sqlSession.selectOne("com.itrus.portal.db.MakeSealServerMapper.selectByPrimaryKey", product.getMakeSealServer());
            if (null != makeSealServer) {
                // 替换-印章名称
                if (StringUtils.isNotBlank(makeSealServer.getSealName())) {
                    UIDInfoUtils uidutils = new UIDInfoUtils();
                    uidutils.initService(businessService, orgCodeService, taxCertService, identityCardService, userInfoService, enterpriseService);
                    makeSealServer.setSealName(uidutils.getUidInfo(id, makeSealServer.getSealName()));
                }
                uiModel.addAttribute("makeSealServer" + i, makeSealServer);
            }
        }
        return "makecert/update1";
    }
    List<Map> makecertexall = sqlSession.selectList("com.itrus.portal.db.UserCertMapper.selectByPrimaryBillKey", id);
    uiModel.addAttribute("makecertexall", makecertexall);
    digitalcert = sqlSession.selectOne("com.itrus.portal.db.DigitalCertMapper.selectByPrimaryKey", makecerts.get(0).get("cert"));
    uiModel.addAttribute("digitalcert", digitalcert);
    if (makecerts.get(0).containsKey("product_spec") && !"0".equals(makecerts.get(0).get("product_spec"))) {
        productSpec = productSpecService.getProductSpec((Long) makecerts.get(0).get("product_spec"));
    }
    uiModel.addAttribute("productSpec", productSpec);
    product = sqlSession.selectOne("com.itrus.portal.db.ProductMapper.selectByPrimaryKey", makecerts.get(0).get("product"));
    uiModel.addAttribute("product", product);
    try {
        uiModel.addAttribute("billStr", jsonTool.writeValueAsString(makecerts.get(0)));
        uiModel.addAttribute("usercertallStr", jsonTool.writeValueAsString(makecertexall));
        uiModel.addAttribute("digitalcertStr", jsonTool.writeValueAsString(digitalcert));
        uiModel.addAttribute("productStr", jsonTool.writeValueAsString(product));
        uiModel.addAttribute("productSpecStr", jsonTool.writeValueAsString(productSpec));
    } catch (Exception e) {
        e.printStackTrace();
    }
    // System.out.println(makecerts.get(0).get("product_num"));
    // 处理autoid自动编号信息
    // 解析项目产品中,certinfo配置信息
    JSONArray certinfo = JSONArray.parseArray(product.getCertinfo());
    for (int i = 0; certinfo != null && i < certinfo.size(); i++) {
        JSONObject obj = certinfo.getJSONObject(i);
        String autoid = obj.getString("autoid");
        if (autoid == null)
            continue;
        String autoidType = obj.getString("autoidType");
        String autoidPrev = obj.getString("autoidPrev");
        String autoidPrevDate = obj.getString("autoidPrevDate");
        String autoidLength = obj.getString("autoidLength");
        // 从user_cert表查询,该autoidType的最大值,如果没有最大值,则设置为0
        Map param = new HashMap();
        String enterpriseId = makecerts.get(0).get("enterprise").toString();
        param.put("enterpriseId", makecerts.get(0).get("enterprise"));
        param.put("type", autoidType);
        Integer autoidValue = null;
        if (autoidPrevDate == null)
            autoidValue = sqlSession.selectOne("com.itrus.portal.db.UserCertMapper.selectByConditon", param);
        else
            autoidValue = sqlSession.selectOne("com.itrus.portal.db.UserCertMapper.selectByConditonDate", param);
        if (autoidValue == null)
            autoidValue = 0;
        uiModel.addAttribute("enterpriseId", enterpriseId);
        uiModel.addAttribute("autoidType", autoidType);
        uiModel.addAttribute("autoidPrev", autoidPrev);
        uiModel.addAttribute("autoidLength", autoidLength);
        uiModel.addAttribute("autoidValue", autoidValue);
        break;
    }
    // 签章服务配置
    List<MakeSealConfig> makeSealConfigs = sqlSession.selectList("com.itrus.portal.db.MakeSealConfigMapper.selectByExample");
    if (!makeSealConfigs.isEmpty()) {
        MakeSealConfig makeSealConfig = makeSealConfigs.get(0);
        makeSealConfig.setAddressKey(AESencrp.decrypt(makeSealConfig.getAddressKey(), dbEncKey));
        uiModel.addAttribute("makeSealConfig", makeSealConfig);
    }
    MakeSealServer makeSealServer = sqlSession.selectOne("com.itrus.portal.db.MakeSealServerMapper.selectByPrimaryKey", product.getMakeSealServer());
    if (null != makeSealServer) {
        // 替换-印章名称
        if (StringUtils.isNotBlank(makeSealServer.getSealName())) {
            UIDInfoUtils uidutils = new UIDInfoUtils();
            uidutils.initService(businessService, orgCodeService, taxCertService, identityCardService, userInfoService, enterpriseService);
            makeSealServer.setSealName(uidutils.getUidInfo(id, makeSealServer.getSealName()));
        }
        uiModel.addAttribute("makeSealServer", makeSealServer);
    }
    return "makecert/update";
}
Also used : HashMap(java.util.HashMap) JSONArray(com.alibaba.fastjson.JSONArray) Product(com.itrus.portal.db.Product) ProductSpec(com.itrus.portal.db.ProductSpec) EncDecException(com.itrus.portal.exception.EncDecException) IOException(java.io.IOException) RaServiceUnavailable_Exception(cn.topca.tca.ra.service.RaServiceUnavailable_Exception) BigInteger(java.math.BigInteger) UIDInfoUtils(com.itrus.portal.utils.UIDInfoUtils) DigitalCert(com.itrus.portal.db.DigitalCert) MakeSealConfig(com.itrus.portal.db.MakeSealConfig) MakeSealServer(com.itrus.portal.db.MakeSealServer) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with DigitalCert

use of com.itrus.portal.db.DigitalCert in project portal by ixinportal.

the class DigitalCertController method updateForm.

// 返回修改页面
@RequestMapping(value = "/{id}", params = "form", produces = "text/html")
public String updateForm(@PathVariable("id") Long id, Model uiModel) {
    DigitalCert digitalcert = sqlSession.selectOne("com.itrus.portal.db.DigitalCertMapper.selectByPrimaryKey", id);
    uiModel.addAttribute("digitalcert", digitalcert);
    return "digitalcert/update";
}
Also used : DigitalCert(com.itrus.portal.db.DigitalCert) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with DigitalCert

use of com.itrus.portal.db.DigitalCert in project portal by ixinportal.

the class DigitalCertController method delete.

// 删除
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
@ResponseBody
public String delete(@PathVariable("id") Long id, HttpServletRequest request, Model uiModel) {
    DigitalCert digitalcert = sqlSession.selectOne("com.itrus.portal.db.DigitalCertMapper.selectByPrimaryKey", id);
    if (digitalcert == null) {
        // uiModel.addAttribute("message", "未找到要删除快递");
        return "未找到要删除的数字证书";
    } else {
        try {
            sqlSession.delete("com.itrus.portal.db.DigitalCertMapper.deleteByPrimaryKey", id);
            String oper = "删除数字证书";
            String info = "数字证书名称: " + digitalcert.getName();
            LogUtil.adminlog(sqlSession, oper, info);
        } catch (Exception e) {
            /*uiModel.addAttribute("message", "要删除快递【" + digitalcert.getName()
						+ "】存在关联,无法删除");*/
            return "要删除数字证书【" + digitalcert.getName() + "】存在关联,无法删除";
        }
    }
    return null;
}
Also used : DigitalCert(com.itrus.portal.db.DigitalCert) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with DigitalCert

use of com.itrus.portal.db.DigitalCert in project portal by ixinportal.

the class RenewUserServiceImpl method applyCert.

/**
 * 申请指定产品下的证书
 *
 * @param product
 * @return
 * @throws Exception
 * @throws EncDecException
 */
public CertInfo applyCert(String csr, Product product, String oldCertDn, Integer certValidity) throws EncDecException, Exception {
    // 解析项目产品中,certinfo配置信息
    JSONArray certinfo = JSONArray.parseArray(product.getCertinfo());
    // 解析订单中uid信息{"ADDTIONAL_FIELD1_1":"","ADDTIONAL_FIELD3_1":"","ADDTIONAL_FIELD4_1":"G2016071301","inputName":"inputValue"}
    // JSONObject uid = JSONObject.parseObject(bill.getUid());
    // 获取产品、RA配置
    RaAccount ra = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", product.getRa());
    com.itrus.portal.utils.CertUtlis certutil = new com.itrus.portal.utils.CertUtlis();
    // 证书配置
    DigitalCert digitalcert = sqlSession.selectOne("com.itrus.portal.db.DigitalCertMapper.selectByPrimaryKey", product.getCert());
    // 组织RA参数
    cn.topca.tca.ra.service.UserInfo rauserinfo = new cn.topca.tca.ra.service.UserInfo();
    UIDInfoUtils uidutils = new UIDInfoUtils();
    uidutils.initService(businessService, orgCodeService, taxCertService, identityCardService, userInfoService, enterpriseService);
    logger.error("********************************************certValidity=" + certValidity);
    // 设置userInfo信息
    for (int i = 0; i < certinfo.size(); i++) {
        JSONObject obj = certinfo.getJSONObject(i);
        String raParaName = obj.getString("raParaName");
        if (raParaName == null)
            continue;
        String certName = obj.getString("certName");
        String constValue = obj.getString("constValue");
        String val = null;
        if (constValue != null) {
            val = constValue;
        } else if (certName != null) {
            val = uidutils.getCertSubjectInfo(certName, obj.getString("certNamePrefix"), oldCertDn);
        }
        certutil.setUserInfoVal(rauserinfo, raParaName, val);
    }
    logger.error("--------------------------------------certValidity=" + certValidity);
    if (null == certValidity) {
        certValidity = 5;
        if ("0".compareTo(digitalcert.getCertDeadline()) == 0) {
            certValidity = 365 * 1 + 1;
        } else if ("1".compareTo(digitalcert.getCertDeadline()) == 0) {
            certValidity = 365 * 2 + 1;
        } else if ("2".compareTo(digitalcert.getCertDeadline()) == 0) {
            certValidity = 365 * 3 + 1;
        } else if ("3".compareTo(digitalcert.getCertDeadline()) == 0) {
            certValidity = 365 * 5 + 2;
        } else if ("4".compareTo(digitalcert.getCertDeadline()) == 0) {
            certValidity = 365 * 10 + 2;
        }
    }
    // 调用RA
    CertInfo racertinfo = null;
    if (ra.getAaPassword() != null)
        ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
    racertinfo = certutil.enrollCertByWS(csr, ra, rauserinfo, certValidity);
    return racertinfo;
}
Also used : CertInfo(cn.topca.tca.ra.service.CertInfo) JSONArray(com.alibaba.fastjson.JSONArray) UIDInfoUtils(com.itrus.portal.utils.UIDInfoUtils) DigitalCert(com.itrus.portal.db.DigitalCert) JSONObject(com.alibaba.fastjson.JSONObject) RaAccount(com.itrus.portal.db.RaAccount)

Aggregations

DigitalCert (com.itrus.portal.db.DigitalCert)31 Product (com.itrus.portal.db.Product)24 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 HashMap (java.util.HashMap)20 Bill (com.itrus.portal.db.Bill)15 Enterprise (com.itrus.portal.db.Enterprise)14 JSONObject (com.alibaba.fastjson.JSONObject)11 ProductSpec (com.itrus.portal.db.ProductSpec)11 UserInfo (com.itrus.portal.db.UserInfo)10 Date (java.util.Date)10 IOException (java.io.IOException)9 OnPayInfo (com.itrus.portal.db.OnPayInfo)8 Map (java.util.Map)8 CertBuf (com.itrus.portal.db.CertBuf)7 ArrayList (java.util.ArrayList)7 CertInfo (cn.topca.tca.ra.service.CertInfo)6 OnlinePay (com.itrus.portal.db.OnlinePay)6 PayInfo (com.itrus.portal.db.PayInfo)6 RaAccount (com.itrus.portal.db.RaAccount)6 UserCert (com.itrus.portal.db.UserCert)6