第三方对接-CA对接

package cc.ewell.mem.prescription.api.ca;

import cc.ewell.mem.prescription.comm.ApolloGener;
import cc.ewell.mem.prescription.jdbc.sign.CaSignYw;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import co.faao.plugin.esb.GogApi;
import co.faao.plugin.key.CommonKeyUtil;
import co.faao.plugin.starter.properties.PropertiesValue;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@GogApi("CA签名API类")
@Slf4j
public class CaVerification {

    //卫生计生组织机构代码,每个医院均有独立编码  测试请使用""
    String businessOrgCode = PropertiesValue.getProperties("businessOrgCode");
//    String businessOrgCode = ApolloGener.businessOrgCode;
    //CA 业务系统编码
    String businessSystemCode = PropertiesValue.getProperties("businessSystemCode");
//    String businessSystemCode = ApolloGener.businessSystemCode;
    //业务系统应用 ID,业务系统的唯一标识号   ,测试请使用""
    String businessSystemAppID = PropertiesValue.getProperties("businessSystemAppID");
//    String businessSystemAppID = ApolloGener.businessSystemAppID;
    //CA 服务地址 http://localhost:55668/
    //http://localhost:55668/CaApi2/Tools/SZWJ_VerifyEncryptedToken
    String caUrl = PropertiesValue.getProperties("caUrl");
//    String caUrl = ApolloGener.caurl;


    /**
     * 开始签名
     *
     * @param response HttpServletResponse response,
     * @param staffCode
     * @param signType     分为1免密,2二维码,3本地用户密码: 免密为以前流程,二维码需要前端请求getSignTxt获取扫码签名状态
     * @param type
     * @param businContent
     */
    @GogApi("CA数据签名")
    @RequestMapping(value = "/caSign/signTxt", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffCode", value = "签名医生工号(长工号)", paramType = "query", required = true),
            @ApiImplicitParam(name = "patId", value = "患者id", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessTypeCode", value = "CA 业务类型编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "signType", value = "签名方式 1,免密签章 2,口令登录 3,本地用户名密码签章", paramType = "query", allowableValues = "1,2,3", allowEmptyValue = true),
            @ApiImplicitParam(name = "type", value = "业务类型", paramType = "query", allowableValues = "病历保存,病历删除、处方提交、检验提交,检查提交,处方撤销"),
            @ApiImplicitParam(name = "businContent", value = "内容", paramType = "query"),
            @ApiImplicitParam(name = "encryptedToken", value = "加密令牌", paramType = "query")})
    @ResponseBody
    public String signTxt( String staffCode, String patId, String businessTypeCode,String signType, String type,String businContent,String encryptedToken) {
        if (ApolloGener.caignore) {
            String bizSn = CommonKeyUtil.generString();
            CaSignYw.inserCaSign(bizSn, "{\"" + staffCode + "\":\"" + "签名开关关闭" + "\"}", businContent,null);
            return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
        }else{
            if (StrUtil.equals("3", signType)){//本地登录
                String bizSn = CommonKeyUtil.generString();
                CaSignYw.inserCaSign(bizSn, "{\"" + staffCode + "\":\"" + "本地验证,不走签名服务器" + "\"}", businContent, null);
                return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
            }else if (StrUtil.equals("2",signType)) {//口令登录 账号+验证码登录
                //校验证书是否过期
                Boolean verifyEncryptedToken = VerifyEncryptedToken(encryptedToken);
                if (verifyEncryptedToken){//证书未过期
                    //3.20、 数据签名(关联患者) SZWJ_SignDataWithPatient
                    //http://localhost:55668/CaApi2/SZWJ_SignDataWithPatient
                    String bizSn = CommonKeyUtil.generString();
                    Boolean signDataWithPatient = SignDataWithPatient(bizSn,encryptedToken, patId, businContent,businessTypeCode);
                    if (signDataWithPatient){
                        return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
                    }
                    return "{\"state\":\"fail\",\"data\":\"获取签名失败\"}";
                }
                //过期返回失败
                return "{\"state\":\"fail\",\"data\":\"证书已过期\"}";
            }
            return "{\"state\":\"fail\",\"data\":\"签名失败\"}";
        }
    }


    /**
     * 数据签名(关联患者)
     * @param bizSn caId 存库主键
     * @param encryptedToken 加密令牌 门户前端获取
     * @param patientId  患者id
     * @param data 签名数据 需要进行base64编码
     * @return Boolean
     * bizId 业务系统id 第三方自定义的
     *
     */
    public Boolean SignDataWithPatient(String bizSn,String encryptedToken,String patientId,String data,String businessTypeCode){
        try {
            //对签名原文进行base64编码
            String base64Value = Base64Encode(data);
            //判断编码是否成功
            if(StringUtils.isEmpty(base64Value)){
                return false;
            }
            //深圳市 CA 业务类型编码
            //值 值含义
            //001 登录 002 处方  003 医嘱  004 检验  005 检查  006 病历 007 审批 998 测试  999 其他
//            String businessTypeCode="002";

            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.20、数据签名
            String url=caUrl+"CaApi2/SZWJ_SignDataWithPatient";
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("businessSystemAppID", businessSystemAppID);
            jsonObject.addProperty("businessSystemCode", businessSystemCode);
            jsonObject.addProperty("businessOrgCode", businessOrgCode);
            jsonObject.addProperty("businessTypeCode", businessTypeCode);
            jsonObject.addProperty("encryptedToken", encryptedToken);
            jsonObject.addProperty("patientId", patientId);//患者id
            jsonObject.addProperty("bizId", "");//暂时没有值
            jsonObject.addProperty("data", base64Value);//需要进行base64编码 请求315接口进行编码
            jsonObject.addProperty("withTsa", true);//是否进行时间戳签名
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity requestEntity = new StringEntity(jsonObject.toString(), "utf-8");
            log.info(jsonObject.toString());
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("数据签名(关联患者)结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {
                    String signedData = JSONUtil.parse(result).getByPath("eventValue.signedData").toString();
                    String timestamp = JSONUtil.parse(result).getByPath("eventValue.timestamp").toString();
                    CaSignYw.inserCaSign(bizSn,signedData, data,timestamp);
                    return true;
                }
            }
            return false;
        }catch (Exception e) {
            log.error("调用数据签名(关联患者)接口异常:", e);
            return false;
        }
    }

    /**
     *  3.15、 Base64 编码
     *  http://localhost:55668/CaApi2/Tools/SZWJ_Base64Encode
     * @param data 签名原文
     * @return base64编码后的签名原文
     */
    public String Base64Encode(String data){
        try{
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.15、Base64 编码
            //http://localhost:55668/CaApi2/Tools/SZWJ_Base64Encode
            String url=caUrl+"CaApi2/Tools/SZWJ_Base64Encode";
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "text/html");
            StringEntity requestEntity = new StringEntity(data, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("Base64 编码结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {
                    //返回base64Value
                    return JSONUtil.parse(result).getByPath("eventValue.base64Value").toString();
                }
            }
            return null;
        }catch(Exception e){
            log.error("调用Base64 编码接口异常:", e);
            return null;
        }
    }




    /**
     * 动态令牌校验 判断encryptedToken是否过期
     * @param encryptedToken 加密令牌 门户获取
     * @return Boolean
     */
    public Boolean VerifyEncryptedToken(String encryptedToken){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.13、动态令牌验证
            String url=caUrl+"CaApi2/Tools/SZWJ_VerifyEncryptedToken";
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("businessSystemAppID", businessSystemAppID);
            jsonObject.addProperty("businessSystemCode", businessSystemCode);
            jsonObject.addProperty("businessOrgCode", businessOrgCode);
            jsonObject.addProperty("encryptedToken", encryptedToken);
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity requestEntity = new StringEntity(jsonObject.toString(), "utf-8");
            log.info(jsonObject.toString());
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("动态令牌校验结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {//0表示成功 1表示失败
                    return true;
                }
            }
            return false;
        } catch (Exception e){
            log.error("调用动态令牌校验接口异常:", e);
            return false;
        }
    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 第三方系统对接CAS(Central Authentication Service)RESTful接口是指将第三方系统与CAS系统进行集成,使得第三方系统可以通过调用CAS的RESTful接口来实现用户认证和授权功能。 第三方系统与CAS系统对接的过程通常包括以下几个步骤: 1. 配置CAS服务器:在CAS服务器上进行相关配置,包括定义用户认证的方式(例如用户名密码、单点登录等)和认证成功后的返回数据格式等。 2. 接入CAS客户端:在第三方系统中集成CAS客户端,通过CAS客户端与CAS服务器建立连接。 3. 请求认证:当用户访问第三方系统时,第三方系统将用户请求重定向到CAS服务器的认证接口,进行用户认证。 4. 获取票据:用户在CAS服务器上成功认证后,CAS服务器会返回一个票据(ticket),第三方系统将该票据作为参数发送给CAS服务器的票据校验接口。 5. 校验票据:CAS服务器收到票据后,通过票据校验接口验证票据的有效性,并返回相应的认证结果给第三方系统。 6. 授权访问:验证成功后,第三方系统可以根据CAS服务器返回的用户信息来进行授权访问,如获取用户的角色、权限等。 7. 注销认证:当用户退出第三方系统时,第三方系统需要调用CAS的注销接口来注销用户的认证信息。 通过以上步骤,第三方系统可以通过CAS的RESTful接口进行用户认证和授权,实现了统一的登录认证和单点登录功能,提升了系统的安全性和用户体验。 ### 回答2: 第三方系统对接CAS RESTful接口,首先需要了解CAS(Central Authentication Service)是什么。CAS是一种单点登录(Single Sign-On)协议,提供了认证和授权的功能,可以实现不同系统之间的用户身份单点登录和安全交互。 对接CAS RESTful接口的过程一般包括以下几个步骤: 1.了解CAS RESTful接口文档:首先需要仔细阅读CAS RESTful接口的文档,了解接口的功能、参数及返回值。 2.注册第三方系统:在CAS系统中注册第三方系统的信息,包括系统名称、系统URL等。注册后会获得一个唯一的系统凭证。 3.获取CAS登录凭证:第三方系统需要通过CAS RESTful接口CAS系统发送登录请求,包括用户名和密码等信息。CAS系统会验证用户身份,并返回给第三方系统一个登录凭证,通常是一个token或者ticket。 4.验证CAS登录凭证:第三方系统拿到登录凭证后,需要将凭证作为参数发送给CAS RESTful接口进行验证。如果凭证有效,CAS系统会返回相应的用户信息给第三方系统。 5.其他接口调用:一旦用户身份验证成功,第三方系统可以调用CAS RESTful提供的其他接口,进行用户授权、访问受限资源等操作。 6.处理CAS回调:CAS系统会通过回调机制通知第三方系统用户的登录状态变化等重要事件。第三方系统需要相应地处理这些回调,确保与CAS系统的同步更新。 总的来说,对接CAS RESTful接口需要进行注册、登录凭证获取和验证、其他接口调用等步骤。通过正确地使用CAS RESTful接口第三方系统可以实现与CAS系统的安全交互和用户身份认证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值