Search in sources :

Example 1 with CertBuf

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

the class BindCertWebController method toCertLoginBindPage.

/**
 * 跳转到登陆证书绑定界面 szy 2016年8月18日 下午3:21:49
 *
 * @return String
 */
@RequestMapping("/toCertLoginBindPage")
public String toCertLoginBindPage(@RequestParam(value = "certSn", required = true) String certSn, @RequestParam(value = "productId", required = false) String productId, @RequestParam(value = "projectId", required = false) String projectId, HttpSession session, Model uiModel) {
    UserCert userCert = userCertService.getUserCertByCertSn(certSn);
    if (userCert != null) {
        uiModel.addAttribute("keySn", userCert.getKeySn());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String startDate = sdf.format(userCert.getCertStartTime());
        uiModel.addAttribute("startDate", startDate);
        String endDate = sdf.format(userCert.getCertEndTime());
        uiModel.addAttribute("endDate", endDate);
        String status = "";
        if (userCert.getCertStatus() != null && "1".equals(userCert.getCertStatus())) {
            status = "有效";
        } else {
            status = "过期";
        }
        CertBuf certBuf = sqlSession.selectOne("com.itrus.portal.db.CertBufMapper.selectByPrimaryKey", userCert.getCertBuf());
        String certBase64 = certBuf.getCertBuf().replaceAll("\n", "");
        uiModel.addAttribute("certBase64", certBase64);
        uiModel.addAttribute("status", status);
        uiModel.addAttribute("productId", productId);
        uiModel.addAttribute("projectId", projectId);
        if (userCert.getCertDn() != null) {
            String owner = "";
            List<String> dns = CertUtlis.certdn(userCert.getCertDn());
            for (String s : dns) {
                if (s.startsWith("CN=")) {
                    owner = s.substring(3);
                    break;
                }
            }
            uiModel.addAttribute("owner", owner);
        }
    }
    return "ixinweb/zhengshubangding";
}
Also used : CertBuf(com.itrus.portal.db.CertBuf) SimpleDateFormat(java.text.SimpleDateFormat) UserCert(com.itrus.portal.db.UserCert) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CertBuf

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

the class DownLoadCertWebController method downloadPfxCert.

/**
 * 用户下载pfx证书的接口
 * @param id,订单id
 * @param session
 * @param request
 * @param response
 * @return
 */
@RequestMapping("/pfx/{id}")
public String downloadPfxCert(@PathVariable(value = "id") Long id, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
    UserInfo webuserInfo = (UserInfo) session.getAttribute("webuserInfo");
    Enterprise webenterprise = (Enterprise) session.getAttribute("webenterprise");
    if (null == webuserInfo || null == webenterprise) {
        return null;
    }
    Integer uidIdx = 1;
    Bill bill = billService.getBill(id);
    if (null == bill) {
        return null;
    }
    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);
            // 记录日志
            UserLog userlog = new UserLog();
            userlog.setProject(webuserInfo.getProject());
            userlog.setType("用户下载证书pfx");
            userlog.setInfo(webenterprise.getEnterpriseName() + "下载证书成功");
            userlog.setHostId("未知");
            userlog.setSn(null == webuserInfo.getUniqueId() ? null : webuserInfo.getUniqueId());
            LogUtil.userlog(sqlSession, userlog);
            return null;
        } catch (IOException e) {
            UserLog userlog = new UserLog();
            userlog.setProject(bill.getProject());
            userlog.setType("用户下载证书pfx");
            userlog.setInfo(webenterprise.getEnterpriseName() + "失败,错误信息:" + e.getMessage());
            userlog.setHostId("未知");
            userlog.setSn(null == webuserInfo.getUniqueId() ? null : webuserInfo.getUniqueId());
            LogUtil.userlog(sqlSession, userlog);
        }
    } 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,用来最终匹配公钥证书和密钥对
            String userid = bill.getBillId() + (Math.random() * 1000 + 9000);
            // TODO 20170410pfx私钥证书保护密码:需要根据产品配置的密码或获取
            String certPass = product.getPassword();
            // 产生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);
                // pfxCert = GenUtil2.GenPFX(userid, certPass, racertinfo.getCertSignBuf(), pfxCert, false, enterprise.getEnterpriseName());
                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();
                }
                // 记录日志
                UserLog userlog = new UserLog();
                userlog.setProject(bill.getProject());
                userlog.setType("用户下载证书pfx");
                userlog.setInfo(webenterprise.getEnterpriseName() + "下载证书成功,企业名称:" + webenterprise.getEnterpriseName());
                userlog.setHostId("未知");
                userlog.setSn(null == webuserInfo.getUniqueId() ? null : webuserInfo.getUniqueId());
                LogUtil.userlog(sqlSession, userlog);
                return null;
            } catch (Exception e) {
                // 记录日志
                UserLog userlog = new UserLog();
                userlog.setProject(bill.getProject());
                userlog.setType("用户下载证书pfx");
                userlog.setInfo(webenterprise.getEnterpriseName() + "失败,错误信息:" + e.getMessage());
                userlog.setHostId("未知");
                userlog.setSn(null == webuserInfo.getUniqueId() ? null : webuserInfo.getUniqueId());
                LogUtil.userlog(sqlSession, userlog);
            }
        }
    }
    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) UserLog(com.itrus.portal.db.UserLog) IOException(java.io.IOException) Date(java.util.Date) GenP10Exception(com.itrus.Exception.GenP10Exception) IOException(java.io.IOException) DigitalCert(com.itrus.portal.db.DigitalCert) RaAccount(com.itrus.portal.db.RaAccount) Enterprise(com.itrus.portal.db.Enterprise) Bill(com.itrus.portal.db.Bill) CertBuf(com.itrus.portal.db.CertBuf) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with CertBuf

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

the class RenewUserCertClientController method reNewCertConfirm.

// 订单详情,待更新确认
@RequestMapping(value = "/reNewCertConfirm/{id}", produces = "text/html")
public String reNewCertConfirm(@PathVariable("id") Long id, HttpServletRequest request, Model uiModel) {
    HttpSession session = request.getSession();
    UserInfo userInfo = (UserInfo) session.getAttribute(ComNames.WEB_USER_INFO);
    Enterprise enterprise = (Enterprise) session.getAttribute(ComNames.WEB_ENTERPRISE);
    if (null == userInfo || null == enterprise) {
        return ComNames.DENG_LU_CLIENT;
    }
    // 审核记录
    ReviewLog reviewLog = reviewLogService.getReviewLog(id);
    if (reviewLog != null) {
        uiModel.addAttribute("reviewLog", reviewLog);
    }
    Map param = new HashMap();
    // 设置查询条件,选择属于当前用户,当前企业的订单
    param.put("id", id);
    // param.put("userinfoid", userInfo.getId());
    // param.put("enterpriseid", enterprise.getId());
    List<Map> billAll = sqlSession.selectList("com.itrus.portal.db.BillMapper.selectProductBillCertById", param);
    if (0 == billAll.size()) {
        return ComNames.DENG_LU_CLIENT;
    }
    uiModel.addAttribute("bills", billAll.get(0));
    // 获取数字证书
    Product product = productService.getProduct((Long) billAll.get(0).get("product"));
    DigitalCert digitalCert = digitalCertService.getDigitalCert(product.getCert());
    // 获取订单在线支付方式
    if (billAll.get(0).get("on_pay_info") != null) {
        Map<Long, OnPayInfo> opiMap = sqlSession.selectMap("com.itrus.portal.db.OnPayInfoMapper.selectByExample", "id");
        uiModel.addAttribute("opiMap", opiMap);
        Map<Long, OnlinePay> opMap = sqlSession.selectMap("com.itrus.portal.db.OnlinePayMapper.selectByExample", "id");
        uiModel.addAttribute("opMap", opMap);
    }
    PayInfoExample payInfoex = new PayInfoExample();
    Map<Long, PayInfo> payinfoMap = sqlSession.selectMap("com.itrus.portal.db.PayInfoMapper.selectByExample", payInfoex, "id");
    uiModel.addAttribute("payinfomap", payinfoMap);
    // 获取产品规格
    ProductSpec productSpec = null;
    if (null != billAll.get(0).get("product_spec") && !"0".equals(billAll.get(0).get("product_spec"))) {
        productSpec = productSpecService.getProductSpec((Long) billAll.get(0).get("product_spec"));
    }
    uiModel.addAttribute("productSpec", productSpec);
    uiModel.addAttribute("digitalCert", digitalCert);
    // 返回订单对应的老证书的base64
    CertBuf certBuf = sqlSession.selectOne("com.itrus.portal.db.CertBufMapper.selectCertBufByBillId", id);
    if (null != certBuf) {
        uiModel.addAttribute("oldCertB64", certBuf.getCertBuf().replaceAll("\n", ""));
    }
    // 判断是否有签章服务,有则显示授权
    if (null != product.getMakeSealServer()) {
        uiModel.addAttribute("makesealserver", product.getMakeSealServer());
        uiModel.addAttribute("billId", billAll.get(0).get("id"));
        // 签章服务配置
        List<MakeSealConfig> makeSealConfigs = sqlSession.selectList("com.itrus.portal.db.MakeSealConfigMapper.selectByExample");
        if (!makeSealConfigs.isEmpty()) {
            MakeSealConfig makeSealConfig = makeSealConfigs.get(0);
            try {
                makeSealConfig.setAddressKey(AESencrp.decrypt(makeSealConfig.getAddressKey(), dbEncKey));
            } catch (EncDecException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            uiModel.addAttribute("makeSealConfig", makeSealConfig);
        }
    }
    return "clientFW/dingdanxiangqing_gengxinqueren";
}
Also used : PayInfoExample(com.itrus.portal.db.PayInfoExample) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) Product(com.itrus.portal.db.Product) EncDecException(com.itrus.portal.exception.EncDecException) UserInfo(com.itrus.portal.db.UserInfo) ProductSpec(com.itrus.portal.db.ProductSpec) EncDecException(com.itrus.portal.exception.EncDecException) DigitalCert(com.itrus.portal.db.DigitalCert) MakeSealConfig(com.itrus.portal.db.MakeSealConfig) PayInfo(com.itrus.portal.db.PayInfo) OnPayInfo(com.itrus.portal.db.OnPayInfo) OnPayInfo(com.itrus.portal.db.OnPayInfo) Enterprise(com.itrus.portal.db.Enterprise) ReviewLog(com.itrus.portal.db.ReviewLog) CertBuf(com.itrus.portal.db.CertBuf) HashMap(java.util.HashMap) Map(java.util.Map) OnlinePay(com.itrus.portal.db.OnlinePay) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with CertBuf

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

the class MakeCertController method makeSealBaiRun.

/**
 * 百润制章
 *
 * @param billId
 *            订单id
 * @param csr
 *            证书base64编码
 * @param request
 * @param response
 * @return
 * @throws EncDecException
 * @throws Exception
 */
@RequestMapping(value = "/makeSealBaiRun", method = RequestMethod.POST)
public void makeSealBaiRun(@RequestParam(value = "billId", required = true) Long billId, @RequestParam(value = "sealpic", required = true) String sealpic, @RequestParam(value = "csr", required = true) String csr, @RequestParam(value = "productId", required = false) Long productId, HttpServletRequest request, HttpServletResponse response) throws EncDecException, Exception {
    // Map<String, Object> map = new HashMap<String, Object>();
    // map.put("retCode", 0);
    // 获取签章模版
    Bill bill = sqlSession.selectOne("com.itrus.portal.db.BillMapper.selectByPrimaryKey", billId);
    // 查询项目产品
    Product product = null;
    // 如为组合产品productId不为空
    if (null == productId) {
        product = sqlSession.selectOne("com.itrus.portal.db.ProductMapper.selectByPrimaryKey", bill.getProduct());
    } else {
        product = sqlSession.selectOne("com.itrus.portal.db.ProductMapper.selectByPrimaryKey", productId);
    }
    /*
		 * if (null == product.getMakeSealServer() ||
		 * product.getMakeSealServer() <= 0) { map.put("retMsg", "该产品未配置签章服务");
		 * return map; }
		 */
    MakeSealServer makeSealServer = sqlSession.selectOne("com.itrus.portal.db.MakeSealServerMapper.selectByPrimaryKey", product.getMakeSealServer());
    // 签章服务配置
    List<MakeSealConfig> makeSealConfigs = sqlSession.selectList("com.itrus.portal.db.MakeSealConfigMapper.selectByExample");
    /*
		 * if (makeSealConfigs.isEmpty()) { map.put("retMsg", "没有找到签章服务配置");
		 * return map; }
		 */
    Map<String, Object> param = new HashMap<>();
    param.put("id", bill.getId());
    UserCertExample userc = new UserCertExample();
    UserCertExample.Criteria us = userc.createCriteria();
    us.andIdNotEqualTo(bill.getId());
    userc.setOrderByClause("cert_start_time desc");
    userc.setLimit(1);
    UserCert usercert = sqlSession.selectOne("com.itrus.portal.db.UserCertMapper.selectByExample", userc);
    CertBufExample certbuf = new CertBufExample();
    CertBufExample.Criteria cert = certbuf.createCriteria();
    cert.andIdEqualTo(usercert.getCertBuf());
    CertBuf organ = sqlSession.selectOne("com.itrus.portal.db.CertBufMapper.selectByExample", certbuf);
    if (makeSealConfigs.get(1).getName().contains(makeSealServer.getFirm())) {
        MakeSealConfig makeSealConfig = makeSealConfigs.get(0);
        // 替换-印章名称
        if (StringUtils.isNotBlank(makeSealServer.getSealName())) {
            UIDInfoUtils uidutils = new UIDInfoUtils();
            uidutils.initService(businessService, orgCodeService, taxCertService, identityCardService, userInfoService, enterpriseService);
            makeSealServer.setSealName(uidutils.getUidInfo(billId, makeSealServer.getSealName()));
        }
        try {
            log.error("request.getLocalAddr : " + request.getLocalAddr() + ",CSR:" + organ.getCertBuf());
            boolean userbool = true;
            if (usercert.getIsRegister() == null || !usercert.getIsRegister().equals(1)) {
                usercert.setIsRegister(1);
                sqlSession.update("com.itrus.portal.db.UserCertMapper.updateByPrimaryKeySelective", usercert);
                // 注册用户
                userbool = UserSDK.addUser(null, makeSealConfig.getAddressKey(), organ.getCertBuf(), makeSealServer.getAccount(), Constant.APP, Constant.Token);
                log.error("makeCertController userbool : " + userbool);
            // log.info(makeSealConfig.getAddressKey());
            // log.info(String.valueOf(userbool));
            }
            // 制作印章
            Map<String, Object> result = SealSDK.makeSeal(makeSealServer.getSealName(), organ.getCertBuf(), sealpic, "公章", Constant.APP, Constant.Token);
            if (userbool && (Boolean) result.get("state")) {
                // 制章完成后的印章数据,返回到前台用作导章
                log.error("SealSDK : context");
            } else {
                log.error("SealSDK : ");
            // map.put("retMsg", "制章异常");
            }
            response.setCharacterEncoding("GBK");
            response.setContentType("text/html");
            OutputStream out = response.getOutputStream();
            out.write(result.get("data").toString().getBytes("GBK"));
            // map.put("bairun_ret",
            // result.get("data").toString().getBytes("GBK"));
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        // map.put("retMsg", e.getMessage());
        }
    } else {
        MakeSealConfig makeSealConfig = makeSealConfigs.get(1);
        // 替换-印章名称
        if (StringUtils.isNotBlank(makeSealServer.getSealName())) {
            UIDInfoUtils uidutils = new UIDInfoUtils();
            uidutils.initService(businessService, orgCodeService, taxCertService, identityCardService, userInfoService, enterpriseService);
            makeSealServer.setSealName(uidutils.getUidInfo(billId, makeSealServer.getSealName()));
        }
        try {
            log.error("request.getLocalAddr : " + request.getLocalAddr() + ",CSR:" + organ.getCertBuf());
            boolean userbool = true;
            if (usercert.getIsRegister() != null && !usercert.getIsRegister().equals(1)) {
                usercert.setIsRegister(1);
                sqlSession.update("com.itrus.portal.db.UserCertMapper.updateByPrimaryKeySelective", usercert);
                // 注册用户
                userbool = UserSDK.addUser(null, makeSealConfig.getAddressKey(), organ.getCertBuf(), Constant.TianWeiChenXin, Constant.APP, Constant.Token);
            }
            // 制作印章
            Map<String, Object> result = SealSDK.makeSeal(makeSealServer.getSealName(), organ.getCertBuf(), sealpic, "公章", Constant.APP, Constant.Token);
            String s = Constant.IP;
            if (userbool && (Boolean) result.get("state")) {
                // 制章完成后的印章数据,返回到前台用作导章
                log.error("SealSDK : " + result.get("data").toString());
            } else {
                log.error("SealSDK : ");
            // map.put("retMsg", "制章异常");
            }
            response.setCharacterEncoding("GBK");
            response.setContentType("text/html");
            OutputStream out = response.getOutputStream();
            out.write(result.get("data").toString().getBytes("GBK"));
            // map.put("bairun_ret",
            // result.get("data").toString().getBytes("GBK"));
            out.flush();
            out.close();
            System.out.println(result.get("data").toString().getBytes("GBK").toString() + ":JS");
        } catch (Exception e) {
            e.printStackTrace();
        // map.put("retMsg", e.getMessage());
        }
    }
// map.put("retCode", 1);
// return map;
}
Also used : HashMap(java.util.HashMap) UserCertExample(com.itrus.portal.db.UserCertExample) OutputStream(java.io.OutputStream) Product(com.itrus.portal.db.Product) EncDecException(com.itrus.portal.exception.EncDecException) IOException(java.io.IOException) RaServiceUnavailable_Exception(cn.topca.tca.ra.service.RaServiceUnavailable_Exception) UIDInfoUtils(com.itrus.portal.utils.UIDInfoUtils) MakeSealServer(com.itrus.portal.db.MakeSealServer) MakeSealConfig(com.itrus.portal.db.MakeSealConfig) Bill(com.itrus.portal.db.Bill) CertBuf(com.itrus.portal.db.CertBuf) JSONObject(com.alibaba.fastjson.JSONObject) CertBufExample(com.itrus.portal.db.CertBufExample) UserCert(com.itrus.portal.db.UserCert) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with CertBuf

use of com.itrus.portal.db.CertBuf 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)

Aggregations

CertBuf (com.itrus.portal.db.CertBuf)14 UserCert (com.itrus.portal.db.UserCert)9 Product (com.itrus.portal.db.Product)8 Date (java.util.Date)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 DigitalCert (com.itrus.portal.db.DigitalCert)7 Bill (com.itrus.portal.db.Bill)6 Enterprise (com.itrus.portal.db.Enterprise)6 UserInfo (com.itrus.portal.db.UserInfo)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 EncDecException (com.itrus.portal.exception.EncDecException)5 CertInfo (cn.topca.tca.ra.service.CertInfo)4 RaAccount (com.itrus.portal.db.RaAccount)4 SimpleDateFormat (java.text.SimpleDateFormat)4 RaServiceUnavailable_Exception (cn.topca.tca.ra.service.RaServiceUnavailable_Exception)3 JSONObject (com.alibaba.fastjson.JSONObject)3 MakeSealConfig (com.itrus.portal.db.MakeSealConfig)3 ProductSpec (com.itrus.portal.db.ProductSpec)3 UserCertExample (com.itrus.portal.db.UserCertExample)3