튜토리얼

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

	<!--
		업데이트 일자 : 2024-04-16
		연동 기술지원 연락처 : 1600-9854
		연동 기술지원 이메일 : code@linkhubcorp.com

		<테스트 연동개발 준비사항>
		1) API Key 변경 (연동신청 시 메일로 전달된 정보)
			- LinkID : 링크허브에서 발급한 링크아이디
			- SecretKey : 링크허브에서 발급한 비밀키
		2) ClientCode 변경 (연동신청 시 메일로 전달된 정보)
			- ClientCode : 이용기관코드 (파트너 사이트에서 확인가능)
		3) SDK 환경설정 필수 옵션 설정
			- IPRestrictOnOff : 인증토큰 IP 검증 설정, true-사용, false-미사용, (기본값:true)
			- UseStaticIP : 통신 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">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);

		// ApptoApp 인증방식에서 사용
		// 모바일장비 유형('ANDROID', 'IOS'), 대문자 입력(대소문자 구분)
		// identity.setDeviceOSType("ANDROID");

		// AppToApp 방식 이용시, 호출할 URL
		// "http", "https"등의 웹프로토콜 사용 불가
		// identity.setReturnURL("navercert://sign");

		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 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>
					<li>앱다운로드URL (MarketURL) : ${result.marketUrl}</li>
				</ul>
			</fieldset>
		</div>
	</body>
</html>

4. 결과 확인

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