阿里云身份证实名认证

配置文件:

identify:
  url: https://idcardcheck2.hzylgs.com
  path: api-mall/api/id_card_v2/check
  appKey: 204249435
  appSecret: OFzW3LkejFemF9hVeUTqvcL9dpmjnbbQ
  appCode: 415c6a5afece47f0a89c1b5600e44c64

加载Properties属性:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "identify")
@Data
public class IdProperties {
    // 身份认证的URL地址
    private String url;
    
    private String path;

    // 你购买的appKey
    private String appKey;

    // 你购买的appSecret
    private String appSecret;

    // 你购买的appCode
    private String appCode;
}

方式一:通过RestTemplate连接


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Component
@EnableConfigurationProperties(IdProperties.class)
public class IdAutoConfiguration {

    private static IdProperties idProperties;

    // 发请求的工具
    private static RestTemplate restTemplate = new RestTemplate();

    public IdAutoConfiguration(IdProperties idProperties) {
        IdAutoConfiguration.idProperties = idProperties;
    }

    public static boolean check(String realName, String idCard) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpHeaders.add("Authorization", "APPCODE " + idProperties.getAppCode());

        MultiValueMap params = new LinkedMultiValueMap<>();
        params.add("idcard", idCard);
        params.add("name", realName);

        HttpEntity> entity = new HttpEntity<>(params, httpHeaders);

        ResponseEntity responseEntity = restTemplate.exchange(
                idProperties.getUrl() + "/" + idProperties.getPath(), // url 地址 + path 全路径
                HttpMethod.POST,
                entity,
                String.class
        );

        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            String body = responseEntity.getBody();
            JSONObject jsonObject = JSON.parseObject(body);

            JSONObject data = jsonObject.getJSONObject("data");
            if (data != null) {
                Integer result = data.getInteger("result");
                return 0 == result;
            }
        }
        return false;
    }
}

方式二:通过Feign连接

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "identifyService", url = "${identify.url}")
public interface IdentifyService {

    @PostMapping(value = "${identify.path}",
            headers = {"Authorization=APPCODE ${identify.appCode}"},
            consumes = "application/x-www-form-urlencoded; charset=UTF-8")
    String check(@RequestBody MultiValueMap multiValueMap);
}

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class CollectionUtil {

    public static MultiValueMap toIdCardMap(String realName, String idCard) {
        MultiValueMap idCardMap = new LinkedMultiValueMap<>();
        idCardMap.add("name", realName);
        idCardMap.add("idcard", idCard);
        return idCardMap;
    }
}

测试:


@SpringBootTest
public class IdentifyTest {


    @Autowired
    private IdentifyService identifyService;


    @Test
    public void idPropertiesTest() throws Exception {

        String realName = "王*";
        String idCard = "34122219850110357*";
        MultiValueMap user = new LinkedMultiValueMap<>();
        user.add("name", realName);
        user.add("idcard", idCard);

        System.out.println(this.identifyService.check(CollectionUtil.toIdCardMap(realName, idCard)));
    }
}

文献:

SpringBoot实现身份证实名认证(阿里云实现)_springboot 居民身份证教验_zhz小白的博客-CSDN博客

你可能感兴趣的:(阿里云,云计算)