튜토리얼
튜토리얼
Java 개발환경에서 바로써트 SDK를 추가하여 본인인증 요청(requestIdentity) 함수를 구현하는 예시입니다.
1. BaroCert SDK 추가
바로써트 Java SDK를 추가하기 위해 Spring 프로젝트 "pom.xml" 파일에 바로써트 Java SDK dependency 정보를 추가하고 Maven 업데이트합니다.
<dependency>
<groupId>kr.co.linkhub</groupId>
<artifactId>barocert-sdk</artifactId>
<version>1.2.4</version>
</dependency>
2. BaroCert SDK 설정
카카오써트 클래스를 Spring 빈으로 추가합니다. 아래의 코드를 참조하여 "servlet-context.xml" 파일을 수정합니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.barocert.example" />
<!--
LinkID : 링크허브에서 발급받은 파트너 링크아이디.
SecretKey : 링크허브에서 발급받는 비밀키.
ClientCode : 이용기관 코드, 파트너가 등록한 이용기관의 코드, (파트너 사이트에서 확인가능)
IPRestrictOnOff : 인증토큰 발급 IP 제한 On/Off, true-사용, false-미사용, 기본값(true)
UseStaticIP : 카카오써트 API 서비스 고정 IP 사용여부, true-사용, false-미사용, 기본값(false)
-->
<util:properties id="EXAMPLE_CONFIG">
<beans:prop key="LinkID">TESTER</beans:prop>
<beans:prop key="SecretKey">SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=</beans:prop>
<beans:prop key="ClientCode">023030000004</beans:prop>
<beans:prop key="IsIPRestrictOnOff">true</beans:prop>
<beans:prop key="UseStaticIP">false</beans:prop>
</util:properties>
<beans:beans>
<beans:bean id="kakaocertService" class="com.barocert.kakaocert.KakaocertServiceImp">
<beans:property name="linkID" value="#{EXAMPLE_CONFIG.LinkID}"/>
<beans:property name="secretKey" value="#{EXAMPLE_CONFIG.SecretKey}"/>
<beans:property name="IPRestrictOnOff" value="#{EXAMPLE_CONFIG.IsIPRestrictOnOff}"/>
<beans:property name="useStaticIP" value="#{EXAMPLE_CONFIG.UseStaticIP}"/>
</beans:bean>
</beans:beans>
</beans:beans>
3. RequestIdentity 기능 구현
① 서비스 클래스 빈 객체 추가를 위해 @Autowired 어노테이션과 RequestIdentity 함수 코드를 추가합니다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.barocert.BarocertException;
import com.barocert.kakaocert.KakaocertService;
import com.barocert.kakaocert.identity.Identity;
import com.barocert.kakaocert.identity.IdentityReceipt;
import com.barocert.kakaocert.identity.IdentityStatus;
import com.barocert.kakaocert.identity.IdentityResult;
@Controller
public class KakaocertServiceExample {
@Autowired
private KakaocertService kakaocertService;
@Value("#{EXAMPLE_CONFIG.ClientCode}")
private String ClientCode;
/*
* 카카오톡 이용자에게 본인인증을 요청합니다.
* https://developers.barocert.com/reference/kakao/java/identity/api#RequestIdentity
*/
@RequestMapping(value = "kakaocert/requestIdentity", method = RequestMethod.GET)
public String requestIdentity(Model m) throws BarocertException {
// 본인인증 요청 정보 객체
Identity identity = new Identity();
// 수신자 정보(휴대폰번호, 성명, 생년월일)
identity.setReceiverHP(kakaocertService.encrypt("010111112222"));
identity.setReceiverName(kakaocertService.encrypt("홍길동"));
identity.setReceiverBirthday(kakaocertService.encrypt("19700101"));
// 인증요청 메시지 제목 - 최대 40자
identity.setReqTitle("본인인증 요청 메시지 제목");
// 인증요청 만료시간 - 최대 1,000(초)까지 입력 가능
identity.setExpireIn(1000);
// 서명 원문 - 최대 40자 까지 입력가능
identity.setToken(kakaocertService.encrypt("본인인증 요청 원문"));
// AppToApp 인증요청 여부
// true - AppToApp 인증방식, false - Talk Message 인증방식
identity.setAppUseYN(false);
// App to App 방식 이용시, 호출할 URL
// identity.setReturnURL("https://www.kakaocert.com");
try {
IdentityReceipt result = kakaocertService.requestIdentity(ClientCode, identity);
m.addAttribute("result", result);
} catch (BarocertException ke) {
// 예외 발생 시, e.getCode() 로 오류 코드를 확인하고, e.getMessage()로 오류 메시지를 확인합니다.
System.out.println("오류 코드" + e.getCode());
System.out.println("오류 메시지" + e.getMessage());
}
return "response";
}
}
② 함수 호출결과 코드와 메시지를 출력하는 response.jsp 파일을 추가합니다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="/resources/main.css" media="screen"/>
<title>Barocert Service SpringMVC Example</title>
</head>
<body>
<div id="content">
<p class="heading1">카카오 본인인증 요청 API SDK SpringMVC Example</p>
<br/>
<fieldset class="fieldset1">
<legend>${requestScope['javax.servlet.forward.request_uri']}</legend>
<ul>
<li>ReceiptID (접수아이디) : ${result.receiptID}</li>
<li>Scheme (앱스킴) : ${result.scheme}</li>
</ul>
</fieldset>
</div>
</body>
</html>
4. 결과 확인
함수 호출이 정상적으로 처리되면 "채널 메시지 인증" 방식은 접수아이디(32자리 숫자)가 반환되며, "앱투앱 인증" 방식은 접수아이디와 AppScheme 이 함께 반환됩니다. 실패인 경우 BarocertException으로 오류코드("-"로 시작하는 8자리 숫자값)와 오류메시지가 반환됩니다. [오류코드] 바로가기