튜토리얼

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.4.1</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" />

	<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">023040000001</beans:prop>
		<beans:prop key="IsIPRestrictOnOff">true</beans:prop>
		<beans:prop key="UseStaticIP">false</beans:prop>
	</util:properties>

	<beans:beans>
		<beans:bean id="navercertService" class="com.barocert.navercert.NavercertServiceImp">
			<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.navercert.NavercertService;
import com.barocert.navercert.identity.Identity;
import com.barocert.navercert.identity.IdentityReceipt;
import com.barocert.navercert.identity.IdentityStatus;
import com.barocert.navercert.identity.IdentityResult;

@Controller
public class NavercertServiceExample {

	@Autowired
	private NavercertService navercertService;

	@Value("#{EXAMPLE_CONFIG.ClientCode}")
	private String ClientCode;

	/*
	 * 네이버 이용자에게 본인인증을 요청합니다.
	 * https://developers.barocert.com/reference/naver/java/identity/api#RequestIdentity
	 */
	@RequestMapping(value = "navercert/requestIdentity", method = RequestMethod.GET)
	public String requestIdentity(Model m) throws BarocertException {

		// 본인인증 요청 정보 객체
		Identity identity = new Identity();

		// 수신자 휴대폰번호 - 11자 (하이픈 제외)
		identity.setReceiverHP(navercertService.encrypt("01012341234"));
		// 수신자 성명 - 80자
		identity.setReceiverName(navercertService.encrypt("홍길동"));
		// 수신자 생년월일 - 8자 (yyyyMMdd)
		identity.setReceiverBirthday(navercertService.encrypt("19700101"));

		// 고객센터 연락처 - 최대 12자
		identity.setCallCenterNum("1600-9854");
		// 인증요청 만료시간 - 최대 1,000(초)까지 입력 가능
		identity.setExpireIn(1000);

		// AppToApp 인증요청 여부
		// true - AppToApp 인증방식, false - 푸시(Push) 인증방식
		identity.setAppUseYN(false);

		try {
			IdentityReceipt result = navercertService.requestIdentity(ClientCode, identity);
			m.addAttribute("result", result);
		} catch (BarocertException ne) {
			m.addAttribute("Exception", ne);
			return "exception";
		}

		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 SpringMVC Example</title>
	</head>
	<body>
		<div id="content">
			<p class="heading1">Response</p>
			<br/>
			<fieldset class="fieldset1">
				<legend>${requestScope['javax.servlet.forward.request_uri']}</legend>
				<ul>
					<li>접수아이디 (ReceiptID) : ${result.receiptID}</li>
					<li>앱스킴 (Scheme) : ${result.scheme}</li>
					<li>앱다운로드URL (MarketURL) : ${result.marketUrl}</li>
				</ul>
			</fieldset>
		</div>
	</body>
</html>

4. 결과 확인

함수 호출이 정상적으로 처리되면 "푸시(Push) 인증" 방식은 접수아이디(32자리 숫자)가 반환되며, "앱투앱 인증" 방식은 접수아이디와 AppScheme 이 함께 반환됩니다. 실패인 경우 BarocertException으로 오류코드("-"로 시작하는 8자리 숫자값)와 오류메시지가 반환됩니다. [오류코드]