All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.sdk.impl.FsiServerImpl Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.sdk.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.founder.config.ChisFsiConfig;
import com.founder.core.log.MyLog;
import com.founder.sdk.AbstractFsiServer;
import com.founder.sdk.exception.FuncRetCode;
import com.founder.sdk.exception.ServerException;
import com.founder.sdk.utils.RSAEncodeUtil;
import com.founder.sdk.utils.SHACoder;
import com.founder.sdk.utils.UniqueUtil;
import com.founder.sdk.vopackage.VoFsiRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.time.Instant;

@Service
public class FsiServerImpl extends AbstractFsiServer {

    private static final MyLog _log = MyLog.getLog(FsiServerImpl.class);

    @Autowired
    ChisFsiConfig chisFsiConfig;

    /**
     * 河北国家医保封装服务
     *
     * @param request
     * @param headers
     * @return
     */
    @Override
    public String callFsiService(String url, VoFsiRequest request, HttpHeaders headers) {
        HttpHeaders httpHeaders = buildHeaders();
        VoFsiRequest voFsiRequest = buildRequest(request, headers);
        return request(url, voFsiRequest, httpHeaders);
    }

    /**
     * 构建httpheader
     * @return
     */
    private HttpHeaders buildHeaders() {
        HttpHeaders headers = new HttpHeaders();
        String fsi_eap_flag = chisFsiConfig.getFsi_eap_flag();
        if ("eap".equals(fsi_eap_flag)) {
            _log.warn("模式eap下不需要消息头加密");
            return headers;
        }
        String token = chisFsiConfig.getFsi_token();
        String paasid = chisFsiConfig.getFsi_passid();
        _log.info("打印paasid参数:" + paasid);
        String nonce = UniqueUtil.createUniqueId();//随机字符串
        _log.info("打印nonce参数:" + nonce);
        long timestamp = Instant.now().getEpochSecond();//当前时间戳
        _log.info("打印timestamp参数:" + timestamp);
        String signature = "";//价签结果
        try {
            signature = SHACoder.encodeSHA256(StringUtils.join(timestamp, token, nonce, timestamp).getBytes());
            _log.info("打印价签结果:" + signature);
        } catch (Exception e) {
            e.printStackTrace();
            _log.error("签名异常");
            throw new ServerException(FuncRetCode.ERROR_CODE_NET_ERROR);
        }
        String fsi_sign_type = chisFsiConfig.getFsi_sign_type();
        _log.info("打印签名验证方式:" + fsi_sign_type + "(1、tif 0、rio)");
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        if ("1".equals(fsi_sign_type)) {
            headers.add("x-tif-paasid", paasid);
            headers.add("x-tif-signature", signature);
            headers.add("x-tif-timestamp", String.valueOf(timestamp));
            headers.add("x-tif-nonce", nonce);
        } else {
            headers.add("x-rio-paasid", paasid);
            headers.add("x-rio-signature", signature);
            headers.add("x-rio-timestamp", String.valueOf(timestamp));
            headers.add("x-rio-nonce", nonce);
        }
        _log.info("获取报文消息头>>>" + JSON.toJSONString(headers));
        return headers;
    }


    /**
     * 根据客户端传入的报文消息头重新组织公共参数
     * @param fsiRequest
     * @param header
     * @return
     */
    private VoFsiRequest buildRequest(VoFsiRequest fsiRequest, HttpHeaders header) {
        _log.debug("打印请求头:" + JSONObject.toJSONString(header));
        Object input = fsiRequest.getInput();
        _log.info("打印入參:" + JSON.toJSONString(input));
        String password = header.getFirst("password");
        String signtype = header.getFirst("signtype");//request.getSigntype();
        String cainfo = fsiRequest.getCainfo();
        if (StringUtils.isBlank(signtype)) {
            _log.warn("消息头未配置signtype,系统不验签");
        } else {
            String infno = fsiRequest.getInfno();
            if ("2207".equals(infno) || "2207A".equals(infno) || "2304".equals(infno) || "2304A".equals(infno)) {
                _log.info("結算業務:" + infno + "开始結算嚴密。");
                cainfo = sign(signtype, password, cainfo);
                _log.info("密文:" + cainfo);
                fsiRequest.setCainfo(cainfo);
                fsiRequest.setSigntype(signtype);
            }
        }
        return fsiRequest;
    }

    /**
     * 验密算法
     * @param signtype
     * @param password
     * @param cainfo
     * @return
     */
    private String sign(String signtype, String password, String cainfo) {
        if ("eap".equals(chisFsiConfig.getFsi_eap_flag())) {
            _log.info("模式eap无需验密");
            return cainfo;
        }
        String text = password + chisFsiConfig.getFsi_token();
        _log.debug("待加密串串:" + text);
        try {
            _log.info("驗密方式:" + signtype);
            if ("RFID".equals(signtype)){
                _log.info("开始验证RFID电子凭证密码");
                cainfo = RSAEncodeUtil.encrypt(text, RSAEncodeUtil.getPublicKey(chisFsiConfig.getFsi_public_key()));
            } else if ("PRCSSC".equals(signtype)){
                _log.info("开始验证PRCSSC医保卡密码");
                cainfo = RSAEncodeUtil.encrypt(text, RSAEncodeUtil.getPublicKey(chisFsiConfig.getFsi_public_key()));
            } else if ("MOCK".equals(signtype)){
                _log.warn("MOCK不验密(只在测试环境生效)");
                cainfo = "MOCK";
            } else {
                _log.error("無效的驗密方式:" + signtype);
                cainfo = "無效的驗密方式:" + signtype;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return cainfo;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy