튜토리얼

Java 개발환경에서 바로써트 SDK를 추가하여 간편로그인 검증(verifyLogin) 함수를 구현하는 예시입니다.

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="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. VerifyLogin 기능 구현

서비스 클래스 빈 객체 추가를 위해 @Autowired 어노테이션과 VerifyLogin 함수 코드를 추가합니다.

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.login.LoginResult;

@Controller
public class KakaocertServiceExample {

	@Autowired
	private KakaocertService kakaocertService;

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

	/*
	 * 완료된 전자서명을 검증하고 전자서명 데이터 전문(signedData)을 반환 받습니다.
	 * 카카오 보안정책에 따라 검증 API는 1회만 호출할 수 있습니다. 재시도시 오류가 반환됩니다.
	 * 전자서명 완료일시로부터 10분 이후에 검증 API를 호출하면 오류가 반환됩니다.
	 * https://developers.barocert.com/reference/kakao/java/login/api#VerifyLogin
	 */
	@RequestMapping(value = "kakaocert/verifyLogin", method = RequestMethod.GET)
	public String verifyLogin(Model m) {

		// 간편로그인 토큰받기 요청시 반환된 txID
		String txID = "01daa94d3f-5ac9-429c-8661-40d0ad9ce3e3";

		try {
			LoginResult result = kakaocertService.verifyLogin(ClientCode, txID);
			m.addAttribute("result", result);
		} catch (BarocertException ke) {
			m.addAttribute("Exception", ke);
			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>TxID (트랜잭션 아이디) : ${result.txID}</li>
					<li>State (상태) : ${result.state}</li>
					<li>SignedData (전자서명 데이터 전문) : ${result.signedData}</li>
					<li>Ci (연계정보) : ${result.ci}</li>
					<li>ReceiverName (수신자 성명) : ${result.receiverName}</li>
					<li>ReceiverYear (수신자 출생년도) : ${result.receiverYear}</li>
					<li>ReceiverDay (수신자 출생월일) : ${result.receiverDay}</li>
					<li>ReceiverHP (수신자 휴대폰번호) : ${result.receiverHP}</li>
				</ul>
			</fieldset>
		</div>
	</body>
</html>

4. 결과 확인

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