Search in sources :

Example 1 with EvidenceCertificateExample

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

the class CertificateController method update.

/**
 * 修改处理
 *
 * @param configuration
 * @param photoCodeImg
 * @param type
 *            新建类型 3是新建签章 2是新建证书1修改
 * @param uiModel
 * @return
 */
@RequestMapping(method = RequestMethod.POST, value = "/{type}", produces = "text/html")
public String update(@Valid EvidenceCertificate configuration, @RequestParam(value = "photoCodeImg", required = false) String photoCodeImg, @PathVariable("type") int type, Model uiModel) {
    if (type == 1) {
        // System.out.println("新建签章" + configuration.getSignatureType());
        // 获取证书信息
        EvidenceCertificate econfiguration = certificateService.selectById(configuration.getId());
        EvidenceSecretKey secretkey = new EvidenceSecretKey();
        try {
            if (StringUtils.isNotEmpty(photoCodeImg)) {
                // 获取密钥信息
                secretkey = sqlSession.selectOne("com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey", econfiguration.getEvidenceSecretKey());
                File imgDir = filePathUtils.getEnterpriseFile(secretkey.getAlias());
                File frontImg = filePathUtils.saveImg(imgDir, null, photoCodeImg, IMG_DEFAULT_TYPE, IMG_NAME_VERIFY);
                if (frontImg != null && frontImg.isFile()) {
                    configuration.setPhotoCodeImg(frontImg.getName());
                    configuration.setPhotoCodeImgHash(HMACSHA1.genSha1HashOfFile(frontImg));
                }
            }
            if (configuration.getId() == null) {
                certificateService.insert(configuration);
            } else {
                String s = String.valueOf(econfiguration.getEvidenceSecretKey());
                Long keyid = Long.valueOf(s);
                EvidenceSecretKey secret = secrtkey.selectById(keyid);
                boolean is = certificateService.setSeal(secret.getAlias(), photoCodeImg);
                if (is) {
                    certificateService.update(configuration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            String oper = "修改签章失败";
            String info = "错误原因:" + e.toString();
            LogUtil.evidencelog(sqlSession, null, oper, info);
            error = "修改签章失败";
            return "redirect:/certificate/" + configuration.getId() + "/" + 3;
        }
        if (econfiguration.getIsCertificate() == null || econfiguration.getIsCertificate() != 1) {
            JSONObject ret_data = null;
            ret_data = JSONObject.parseObject(configuration.getSubject());
            String cn = ret_data.getString("userName");
            String ou = ret_data.getString("userOrgunit");
            // String emali = ret_data.getString("EMAILADDRESS");
            String O = ret_data.getString("userOrganization");
            String dn = "CN=" + cn + "OU=" + ou + "O=" + O;
            String csr = certificateService.genCsr(secretkey.getAlias(), configuration.getArithmetic(), dn);
            if (csr != null) {
                RaAccount ra = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", configuration.getRaAccount());
                CertUtlis certutil = new CertUtlis();
                // UserInfo rauserinfo = new UserInfo();
                // Weibo类在下边定义
                UserInfo rauserinfo = JSON.parseObject(configuration.getSubject(), UserInfo.class);
                // System.out.println(rauserinfo.getUserAdditionalField1()+rauserinfo.getUserName());
                // UserInfo = (UserInfo)JSONObject.toBean(ret_data,UserInfo.class);//将建json对象转换为Person对象
                // System.out.println(configuration.getSubject());
                /*	rauserinfo.setUserName(cn);
					rauserinfo.setUserEmail(emali);
					rauserinfo.setUserAdditionalField1(ou);
					rauserinfo.setUserOrganization(O); */
                // 证书有效期
                Integer certValidity = 7;
                if ("0".equals(configuration.getPeriod())) {
                    certValidity = null;
                } else if (1 == configuration.getPeriod()) {
                    certValidity = 365 * 1 + 1;
                } else if (3 == configuration.getPeriod()) {
                    certValidity = 365 * 3 + 1;
                } else if (5 == configuration.getPeriod()) {
                    certValidity = 365 * 5 + 2;
                } else if (10 == configuration.getPeriod()) {
                    certValidity = 365 * 10 + 2;
                }
                // 调用RA
                CertInfo racertinfo = null;
                try {
                    if (ra.getAaPassword() != null) {
                        ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
                    }
                    racertinfo = certutil.enrollCertByWS(csr, ra, rauserinfo, certValidity);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    String oper = "RA调用失败";
                    String info = "错误原因:" + e.toString();
                    LogUtil.evidencelog(sqlSession, null, oper, info);
                    error = "RA调用失败";
                    return "redirect:/certificate/" + configuration.getId() + "/" + 3;
                }
                if (racertinfo != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
                    EvidenceCert cert = new EvidenceCert();
                    cert.setCertBase64(racertinfo.getCertSignBuf());
                    cert.setCreateTime(new Date());
                    cert.setCertSerialnumber(racertinfo.getCertSerialNumber());
                    cert.setIssuerdn(racertinfo.getCertIssuerDn());
                    cert.setSubjectdn(racertinfo.getCertSubjectDn());
                    try {
                        configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                        configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                        cert.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                        cert.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    cert.setCreateTime(new Date());
                    sqlSession.insert("com.itrus.portal.db.EvidenceCertMapper.insert", cert);
                    configuration.setCert(cert.getId().toString());
                    configuration.setCertp7(racertinfo.getCertSignBufP7());
                    configuration.setCertSerialnumber(racertinfo.getCertSerialNumber());
                    boolean is = certificateService.setCertitifate(secretkey.getAlias(), racertinfo.getCertSignBuf(), racertinfo.getCertSignBufP7());
                    if (is) {
                        if ("0".equals(configuration.getArithmetic())) {
                            configuration.setArithmetic(null);
                        }
                        if (StringUtils.isNotEmpty(configuration.getSubject())) {
                            configuration.setSubject(configuration.getSubject());
                        }
                        String s = String.valueOf(secretkey.getId());
                        int keyid = Integer.parseInt(s);
                        configuration.setEvidenceSecretKey(keyid);
                        configuration.setCreateTime(new Date());
                        configuration.setCreator(getAdmin().getName());
                        certificateService.update(configuration);
                        configuration.setSignatureType(2);
                        String oper = "证书产生成功";
                        String info = "证书名称:" + configuration.getCertificateName();
                        LogUtil.evidencelog(sqlSession, null, oper, info);
                    }
                }
            } else {
                error = "csr产生失败";
                return "redirect:/certificate/" + configuration.getId() + "/" + 3;
            }
        }
    } else if (type == 3) {
        EvidenceCertificate econfiguration = certificateService.selectById(configuration.getId());
        EvidenceSecretKey secretkey = new EvidenceSecretKey();
        try {
            if (StringUtils.isNotEmpty(photoCodeImg)) {
                // 获取密钥信息
                secretkey = sqlSession.selectOne("com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey", econfiguration.getEvidenceSecretKey());
                File imgDir = filePathUtils.getEnterpriseFile(secretkey.getAlias());
                File frontImg = filePathUtils.saveImg(imgDir, null, photoCodeImg, IMG_DEFAULT_TYPE, IMG_NAME_VERIFY);
                if (frontImg != null && frontImg.isFile()) {
                    configuration.setPhotoCodeImg(frontImg.getName());
                    configuration.setPhotoCodeImgHash(HMACSHA1.genSha1HashOfFile(frontImg));
                }
            }
            if (configuration.getId() == null) {
                certificateService.insert(configuration);
            } else {
                String s = String.valueOf(econfiguration.getEvidenceSecretKey());
                Long keyid = Long.valueOf(s);
                EvidenceSecretKey secret = secrtkey.selectById(keyid);
                boolean is = certificateService.setSeal(secret.getAlias(), photoCodeImg);
                if (is) {
                    certificateService.update(configuration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            String oper = "新建签章失败";
            String info = "错误原因:" + e.toString();
            LogUtil.evidencelog(sqlSession, null, oper, info);
            error = "新建签章失败";
            return "redirect:/certificate/" + configuration.getId() + "/" + 2;
        }
    } else {
        // System.out.println("新建证书");
        EvidenceSecretKey secret = new EvidenceSecretKey();
        // List<EvidenceSecretKey> list = secrtkey.selecttype(configuration.getKeyType());
        // log.error("list="+list.size());
        // if(econfiguration.getIsCertificate()!=null && econfiguration.getIsCertificate()==1){
        /*if (list != null && list.size() != 0) {
					for (EvidenceSecretKey e : list) {
						EvidenceCertificate certificate = certificateService.selectone(e.getId());
						if (certificate == null) {
							secret.setAlias(e.getAlias());
							secret.setId(e.getId());
							break;
						}
					}
					if (StringUtils.isEmpty(secret.getAlias())) {
						List<EvidenceSecretKey> secter = secrtkey.certufucateInterface(getAdmin().getName());
						//List<EvidenceSecretKey> lists = secrtkey.selecttype(configuration.getKeyType());
						if (secter != null) {
							for (EvidenceSecretKey e : secter) {
								EvidenceCertificate certificate = certificateService.selectone(e.getId());
								if (certificate == null) {
									secret.setAlias(e.getAlias());
									secret.setId(e.getId());
									break;
								}
							}
						}
					}
				} else */
        {
            /*EvidenceSecretKey*/
            secret = secrtkey.certufucateInterface(getAdmin().getName(), configuration.getKeyType());
        // List<EvidenceSecretKey> lists = secrtkey.selecttype(configuration.getKeyType());
        /*if (secter != null) {
						for (EvidenceSecretKey e : secter) {
							EvidenceCertificate certificate = certificateService.selectone(e.getId());
							if (certificate == null) {
								secret.setAlias(e.getAlias());
								secret.setId(e.getId());
								break;
							}
						}
					}*/
        }
        /*	}else{
				 secret = sqlSession.selectOne(
						"com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey",
						econfiguration.getEvidenceSecretKey());
			}*/
        JSONObject ret_data = null;
        ret_data = JSONObject.parseObject(configuration.getSubject());
        String cn = ret_data.getString("userName");
        String ou = ret_data.getString("userOrgunit");
        // String emali = ret_data.getString("EMAILADDRESS");
        String O = ret_data.getString("userOrganization");
        String dn = "CN=" + cn + "OU=" + ou + "O=" + O;
        log.error("Alias=" + secret.getAlias() + ",Arithmetic=" + configuration.getArithmetic());
        String csr = certificateService.genCsr(secret.getAlias(), configuration.getArithmetic(), dn);
        log.error("csr=" + csr);
        if (csr != null) {
            RaAccount ra = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", configuration.getRaAccount());
            CertUtlis certutil = new CertUtlis();
            // UserInfo rauserinfo = new UserInfo();
            // System.out.println(configuration.getSubject());
            // Weibo类在下边定义
            UserInfo rauserinfo = JSON.parseObject(configuration.getSubject(), UserInfo.class);
            // System.out.println(rauserinfo.getUserAdditionalField1()+rauserinfo.getUserName());
            /*rauserinfo.setUserName(cn);
				rauserinfo.setUserEmail(emali);
				rauserinfo.setUserAdditionalField1(ou);
				rauserinfo.setUserOrganization(O);*/
            // 证书有效期
            Integer certValidity = 7;
            if ("0".equals(configuration.getPeriod())) {
                certValidity = null;
            } else if (1 == configuration.getPeriod()) {
                certValidity = 365 * 1 + 1;
            } else if (3 == configuration.getPeriod()) {
                certValidity = 365 * 3 + 1;
            } else if (5 == configuration.getPeriod()) {
                certValidity = 365 * 5 + 2;
            } else if (10 == configuration.getPeriod()) {
                certValidity = 365 * 10 + 2;
            }
            // 调用RA
            CertInfo racertinfo = null;
            try {
                if (ra.getAaPassword() != null) {
                    ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
                }
                racertinfo = certutil.enrollCertByWS(csr, ra, rauserinfo, certValidity);
                log.error("racertinfo=" + racertinfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                String oper = "RA调用失败";
                String info = "错误原因:" + e.toString();
                LogUtil.evidencelog(sqlSession, null, oper, info);
                uiModel.addAttribute("error", "RA调用失败");
                return "certificate/create";
            }
            if (racertinfo != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
                EvidenceCert cert = new EvidenceCert();
                cert.setCertBase64(racertinfo.getCertSignBuf());
                cert.setCreateTime(new Date());
                cert.setCertSerialnumber(racertinfo.getCertSerialNumber());
                cert.setIssuerdn(racertinfo.getCertIssuerDn());
                cert.setSubjectdn(racertinfo.getCertSubjectDn());
                cert.setCreateTime(new Date());
                try {
                    configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                    configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                    cert.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                    cert.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                sqlSession.insert("com.itrus.portal.db.EvidenceCertMapper.insert", cert);
                configuration.setCert(cert.getId().toString());
                // configuration.setCert(racertinfo.getCertSignBuf());
                configuration.setCertp7(racertinfo.getCertSignBufP7());
                configuration.setCertSerialnumber(racertinfo.getCertSerialNumber());
                /*try {
						configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
						configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
                boolean is = certificateService.setCertitifate(secret.getAlias(), racertinfo.getCertSignBuf(), racertinfo.getCertSignBufP7());
                if (is) {
                    if ("0".equals(configuration.getArithmetic())) {
                        configuration.setArithmetic(null);
                    }
                    if (StringUtils.isNotEmpty(configuration.getSubject())) {
                        configuration.setSubject(configuration.getSubject());
                    }
                    String s = String.valueOf(secret.getId());
                    int keyid = Integer.parseInt(s);
                    configuration.setEvidenceSecretKey(keyid);
                    configuration.setCreateTime(new Date());
                    configuration.setCreator(getAdmin().getName());
                    certificateService.insert(configuration);
                    String oper = "证书产生成功";
                    String info = "证书名称:" + configuration.getCertificateName();
                    LogUtil.evidencelog(sqlSession, null, oper, info);
                    EvidenceCertificateExample certE = new EvidenceCertificateExample();
                    EvidenceCertificateExample.Criteria tificate = certE.createCriteria();
                    tificate.andEvidenceSecretKeyEqualTo(keyid);
                    configuration = sqlSession.selectOne("com.itrus.portal.db.EvidenceCertificateMapper.selectByExample", certE);
                }
            }
        } else {
            uiModel.addAttribute("error", "csr产生失败");
            return "certificate/create";
        }
    }
    return "redirect:/certificate/" + configuration.getId() + "/" + 1;
// return "redirect:/certificate/show/"+configuration.getId()+"/"+1;
// return "redirect:/certificate";
}
Also used : CertInfo(cn.topca.tca.ra.service.CertInfo) UserInfo(cn.topca.tca.ra.service.UserInfo) EvidenceSecretKey(com.itrus.portal.db.EvidenceSecretKey) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) CertUtlis(com.itrus.portal.utils.CertUtlis) EvidenceCert(com.itrus.portal.db.EvidenceCert) JSONObject(com.alibaba.fastjson.JSONObject) RaAccount(com.itrus.portal.db.RaAccount) EvidenceCertificateExample(com.itrus.portal.db.EvidenceCertificateExample) EvidenceCertificate(com.itrus.portal.db.EvidenceCertificate) ParseException(java.text.ParseException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with EvidenceCertificateExample

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

the class CertificateServiceImpl method selectone.

/**
 * 根据密钥id查寻证书
 *
 * @param id
 * @return
 */
public EvidenceCertificate selectone(Long id) {
    EvidenceCertificateExample certificate = new EvidenceCertificateExample();
    EvidenceCertificateExample.Criteria certificateex = certificate.createCriteria();
    String s = String.valueOf(id);
    int exid = Integer.parseInt(s);
    certificateex.andEvidenceSecretKeyEqualTo(exid);
    return sqlSession.selectOne("com.itrus.portal.db.EvidenceCertificateMapper.selectByExample", certificate);
}
Also used : EvidenceCertificateExample(com.itrus.portal.db.EvidenceCertificateExample)

Example 3 with EvidenceCertificateExample

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

the class CertificateServiceImpl method getSignatureList.

/**
 * 获取配置签章的数据
 * @return
 */
public List<EvidenceCertificate> getSignatureList() {
    EvidenceCertificateExample certificateExample = new EvidenceCertificateExample();
    EvidenceCertificateExample.Criteria cec = certificateExample.createCriteria();
    cec.andSignatureTypeIsNotNull();
    return sqlSession.selectList("com.itrus.portal.db.EvidenceCertificateMapper.selectByExample", certificateExample);
}
Also used : EvidenceCertificateExample(com.itrus.portal.db.EvidenceCertificateExample)

Example 4 with EvidenceCertificateExample

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

the class CertificateServiceImpl method evidenceCertificateMapById.

public Map<Long, EvidenceCertificate> evidenceCertificateMapById() {
    Map<Long, EvidenceCertificate> map = new HashMap<Long, EvidenceCertificate>();
    List<EvidenceCertificate> evidenceCertificates = sqlSession.selectList("com.itrus.portal.db.EvidenceCertificateMapper.selectByExample", new EvidenceCertificateExample());
    for (EvidenceCertificate certificate : evidenceCertificates) {
        map.put(certificate.getId(), certificate);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) EvidenceCertificateExample(com.itrus.portal.db.EvidenceCertificateExample) EvidenceCertificate(com.itrus.portal.db.EvidenceCertificate)

Aggregations

EvidenceCertificateExample (com.itrus.portal.db.EvidenceCertificateExample)4 EvidenceCertificate (com.itrus.portal.db.EvidenceCertificate)2 CertInfo (cn.topca.tca.ra.service.CertInfo)1 UserInfo (cn.topca.tca.ra.service.UserInfo)1 JSONObject (com.alibaba.fastjson.JSONObject)1 EvidenceCert (com.itrus.portal.db.EvidenceCert)1 EvidenceSecretKey (com.itrus.portal.db.EvidenceSecretKey)1 RaAccount (com.itrus.portal.db.RaAccount)1 CertUtlis (com.itrus.portal.utils.CertUtlis)1 File (java.io.File)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1